Examples¶
Reusable queries and client snippets. The SPARQL files ship in
sparql/.
SPARQL¶
Blast radius: what is affected if orders.v1 fails?¶
One transitive property path over producers, consumers, dependencies and hosting. In the sample data it returns five resources:
PREFIX bf: <https://blackfox.io/ontology/core#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT DISTINCT ?affected ?type ?label
WHERE {
GRAPH <https://blackfox.io/graph/demo/prod/current> {
<https://blackfox.io/resource/demo/prod/topic-orders-v1>
(^bf:producesTo|^bf:consumesFrom|bf:dependsOn|^bf:dependsOn|bf:leads|bf:hostedOn)+
?affected .
?affected a ?type .
OPTIONAL { ?affected rdfs:label ?label }
}
}
ORDER BY ?type ?label
Topic flow: producers and consumers of a topic¶
PREFIX bf: <https://blackfox.io/ontology/core#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT DISTINCT ?producer ?consumer
WHERE {
GRAPH <https://blackfox.io/graph/demo/prod/current> {
?topic a bf:Topic ; rdfs:label ?label .
FILTER(LCASE(STR(?label)) = "orders.v1")
OPTIONAL { ?producer bf:producesTo ?topic }
OPTIONAL { ?consumer bf:consumesFrom ?topic }
}
}
Cluster inventory¶
PREFIX bf: <https://blackfox.io/ontology/core#>
SELECT ?cluster (COUNT(DISTINCT ?broker) AS ?brokers) (COUNT(DISTINCT ?topic) AS ?topics)
WHERE {
GRAPH <https://blackfox.io/graph/demo/prod/current> {
?cluster a bf:KafkaCluster .
OPTIONAL { ?broker a bf:Broker ; bf:belongsToCluster ?cluster }
OPTIONAL { ?topic a bf:Topic ; bf:belongsToCluster ?cluster }
}
}
GROUP BY ?cluster
Guarded endpoint
The /api/v1/sparql endpoint accepts SELECT and ASK only. Update
keywords, SERVICE, FROM and FROM NAMED are rejected. Select a graph
with a GRAPH pattern, as these queries do.
Run a query over HTTP¶
Send data (ingest an observation)¶
Ingestion needs a session. See authentication. On a
local server started with AUTH_DEV_LOGIN=true AUTH_ALLOWLIST=dev-user, get a
session cookie first and send it with the observation. The contract is plain
JSON:
curl -s -c cookies.txt -X POST http://localhost:7860/auth/dev-login
curl -s -b cookies.txt -X POST http://localhost:7860/api/v1/observations \
-H 'Content-Type: application/json' \
-d '{
"tenantId": "demo", "environmentId": "prod",
"collectorType": "my-collector", "collectorInstance": "host-1",
"resource": { "type": "Topic", "externalId": "orders.v2", "label": "orders.v2" },
"attributes": { "partitionCount": 12, "replicationFactor": 3 },
"relationships": [
{ "type": "belongs_to_cluster",
"target": { "type": "KafkaCluster", "externalId": "production-eu" },
"confidence": 1.0, "evidenceSource": "Kafka Admin metadata" }
],
"metrics": [ { "name": "kafka.topic.bytes.in_per_second", "value": 1886000, "unit": "bytes/s" } ]
}'
Write a collector (Python)¶
plugins/sdk.py defines the observation contract as Python dataclasses and an
abstract CollectorPlugin. Run this from the repository root. There is no
plugin loader yet, so your own code calls collect() and posts the payloads:
from plugins.sdk import CollectorPlugin, Observation, Resource, Relationship
class MyCollector(CollectorPlugin):
def collect(self):
cluster = Resource("KafkaCluster", "production-eu", "production-eu")
yield Observation(
resource=Resource("Topic", "orders.v1", "orders.v1"),
relationships=[Relationship("belongs_to_cluster", cluster,
confidence=1.0, evidence_source="my-collector")],
metrics=[{"name": "kafka.topic.bytes.in_per_second", "value": 1886000, "unit": "bytes/s"}],
)
Each Observation.as_payload() is the JSON above, ready to POST to
/api/v1/observations.