openclaw-grafana-lens
Version:
OpenClaw plugin that gives AI agents full Grafana access — 18 composable tools for PromQL/LogQL/TraceQL queries, dashboard creation, alerting, SRE investigation, security monitoring, data collection pipeline management via Grafana Alloy (29 recipes), and
48 lines (47 loc) • 1.78 kB
JavaScript
/**
* OTLP Traces Provider
*
* Central TracerProvider lifecycle module. Creates an OpenTelemetry
* BasicTracerProvider that pushes trace spans via OTLP HTTP/JSON
* to the configured collector (LGTM stack, Grafana Cloud, etc.).
*
* Mirrors the otel-metrics.ts pattern exactly:
* - Local provider only (NO global registration — avoids conflict with diagnostics-otel)
* - Fire-and-forget emission via BatchSpanProcessor
* - Same shared resource identity (service.name=openclaw, service.namespace=grafana-lens)
*
* Data flow:
* Diagnostic events / tool calls → OTel Span → OTLP exporter → Collector → Tempo → Grafana
*/
import { SpanKind, SpanStatusCode } from "@opentelemetry/api";
import { BasicTracerProvider, BatchSpanProcessor, } from "@opentelemetry/sdk-trace-base";
import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-http";
import { createOtelResource } from "./otel-resource.js";
export { SpanKind, SpanStatusCode };
const DEFAULT_ENDPOINT = "http://localhost:4318/v1/traces";
export function createOtelTraces(config) {
const exporter = new OTLPTraceExporter({
url: config.endpoint || DEFAULT_ENDPOINT,
headers: config.headers,
});
const resource = createOtelResource(config);
const provider = new BasicTracerProvider({
resource,
spanProcessors: [
new BatchSpanProcessor(exporter, {
scheduledDelayMillis: 5_000,
}),
],
});
// NO provider.register() — local provider only
const tracer = provider.getTracer("grafana-lens");
return {
tracer,
async forceFlush() {
await provider.forceFlush();
},
async shutdown() {
await provider.shutdown();
},
};
}