Files
scadaproj/infra/otel-collector/otelidentity.py
T
Joseph Doherty f05ce80269 docs(observability): Telemetry 0.3.0 published, adopted by all four apps, logs over OTLP proven (#55)
Evidence doc gains the #55 section (root cause of no logs, 0.3.0 contents + SHA256,
per-app commits, collector output per app: service.version, site.id, node.role and
log records on OtOpcUa, HistorianGateway, ScadaBridge Site and MxAccessGateway).
GAPS, current-state, SPEC, the Telemetry CLAUDE.md and the index row updated;
infra/otel-collector gains otelidentity.py (Resource per signal + log samples).

Claude-Session: https://claude.ai/code/session_01GT67ACmpMR7b9LGRPLijMi
2026-11-12 21:28:50 -05:00

35 lines
2.3 KiB
Python

# Usage (collector host, in ~/zb-otel-collector/data): python3 ../otelidentity.py <service.name> [instance-id-substring, e.g. :12345]
# Prints the Resource each signal (metrics/traces/logs) arrived with, counts, and sample log records (scadaproj#55).
import json,sys,collections
svc=sys.argv[1]; inst=sys.argv[2] if len(sys.argv)>2 else None
def attrs(lst): return {a["key"]:(list(a["value"].values())[0] if a.get("value") else None) for a in lst or []}
KEEP=("service.name","service.version","service.instance.id","site.id","node.role","host.name")
out={}
counts=collections.Counter(); samples=[]; sev=collections.Counter()
for f,key in (("metrics.jsonl","resourceMetrics"),("traces.jsonl","resourceSpans"),("logs.jsonl","resourceLogs")):
try: lines=open(f)
except FileNotFoundError: continue
for l in lines:
d=json.loads(l)
for r in d.get(key,[]):
a=attrs(r["resource"].get("attributes"))
if a.get("service.name")!=svc: continue
if inst and inst not in str(a.get("service.instance.id")): continue
extra={k:v for k,v in a.items() if k not in KEEP and not k.startswith(("telemetry.sdk","process.","os."))}
out[key]={k:a.get(k) for k in KEEP}|({"extra":extra} if extra else {})
if key=="resourceMetrics":
counts["metric points"]+=sum(1 for sm in r["scopeMetrics"] for m in sm["metrics"])
elif key=="resourceSpans":
counts["spans"]+=sum(len(ss["spans"]) for ss in r["scopeSpans"])
else:
for sl in r["scopeLogs"]:
for lr in sl["logRecords"]:
counts["log records"]+=1; sev[lr.get("severityText","?")]+=1
la=attrs(lr.get("attributes"))
if len(samples)<4 or (lr.get("traceId") and not any(s[2] for s in samples)):
body=lr.get("body",{}).get("stringValue","")
samples.append((lr.get("severityText"),body[:110],lr.get("traceId",""),la.get("SourceContext") or la.get("message_template.text","")[:40]))
for k,v in out.items(): print("RESOURCE",k,json.dumps(v))
print("COUNTS",dict(counts),"SEVERITY",dict(sev))
for s in samples[:5]: print("LOG",json.dumps({"severity":s[0],"body":s[1],"traceId":s[2],"src":s[3]}))