{"record":{"id":"6c97f8000c91ef7d","repo":"googleapis/mcp-toolbox","slug":"unable-to-create-s-metric-w","errorCode":null,"errorMessage":"unable to create %s metric: %w","messagePattern":"unable to create (.+?) metric: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/telemetry/instrumentation.go","lineNumber":60,"sourceCode":"\tMcpActiveSessions     metric.Int64UpDownCounter\n\tToolExecutionDuration metric.Float64Histogram\n}\n\nfunc CreateTelemetryInstrumentation(versionString string) (*Instrumentation, error) {\n\ttracer := otel.Tracer(\n\t\tTracerName,\n\t\ttrace.WithInstrumentationVersion(versionString),\n\t)\n\n\tmeter := otel.Meter(MetricName, metric.WithInstrumentationVersion(versionString))\n\n\tmcpOperationDuration, err := meter.Float64Histogram(\n\t\tmcpOperationDurationName,\n\t\tmetric.WithDescription(\"Duration of a single MCP JSON-RPC operation.\"),\n\t\tmetric.WithUnit(\"s\"),\n\t)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"unable to create %s metric: %w\", mcpOperationDurationName, err)\n\t}\n\n\tmcpSessionDuration, err := meter.Float64Histogram(\n\t\tmcpSessionDurationName,\n\t\tmetric.WithDescription(\"Duration of an MCP session.\"),\n\t\tmetric.WithUnit(\"s\"),\n\t)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"unable to create %s metric: %w\", mcpSessionDurationName, err)\n\t}\n\n\tmcpActiveSessions, err := meter.Int64UpDownCounter(\n\t\tmcpActiveSessionsName,\n\t\tmetric.WithDescription(\"Current count of active MCP sessions.\"),\n\t\tmetric.WithUnit(\"{session}\"),\n\t)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"unable to create %s metric: %w\", mcpActiveSessionsName, err)","sourceCodeStart":42,"sourceCodeEnd":78,"githubUrl":"https://github.com/googleapis/mcp-toolbox/blob/8cc6e09de2ad7b8bffc77751799585a1401a48eb/internal/telemetry/instrumentation.go#L42-L78","documentation":"OpenTelemetry meter creation failed for the 'mcp.op.duration' histogram. CreateTelemetryInstrumentation registers several metrics; if the OTel meter API cannot instantiate this Float64Histogram (invalid instrument config or a meter provider in a failed state), it returns this wrapped error and the whole instrumentation setup fails.","triggerScenarios":"Calling Setup/CreateTelemetryInstrumentation where meter.Float64Histogram for the MCP operation duration name returns an error — most commonly when the backing OTel SDK meter provider was not initialized or was already shut down.","commonSituations":"Telemetry set up twice or after Shutdown was called, an OTel SDK version mismatch producing an invalid instrument configuration, or a custom MeterProvider failing to create instruments.","solutions":["Ensure otel setup (SetupOTel) runs before CreateTelemetryInstrumentation and Shutdown has not been called first.","Check the wrapped error; if it says the instrument is invalid, verify the metric name and unit are valid in your OTel SDK version.","Avoid double-initializing telemetry; guard Setup with sync.Once or a flag.","Pin compatible versions of go.opentelemetry.io/otel and the metrics SDK in go.mod.","If telemetry is optional, log the error and continue with a no-op instrumentation instead of failing startup."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// Ensure telemetry is initialized before instruments are created\nif (!otelReady) { await setupOTel(); } // meter provider must be live","typeGuard":null,"tryCatchPattern":"try {\n  const instr = createTelemetryInstrumentation();\n} catch (e) {\n  if (String(e.message).includes(\"unable to create\") && String(e.message).includes(\"metric\")) {\n    // log and fall back to no-op instrumentation\n  }\n}","preventionTips":["Always run SetupOTel before creating instrumentation; never after Shutdown.","Guard telemetry init with sync.Once to avoid double initialization.","Keep otel API and sdk/metric versions aligned.","Log and degrade to no-op telemetry instead of failing startup when metrics are optional."],"tags":["telemetry","opentelemetry","metrics","instrumentation"],"backgroundTag":"otel-instrument-creation-failed","analyzedSha":"8cc6e09de2ad7b8bffc77751799585a1401a48eb","analyzedAt":"2026-09-05T01:10:36.887Z","contentChangedAt":"2026-09-05T01:10:36.887Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}