Extend OpenTelemetry instrumentation when new functionality is added to the MCP Gateway...
Automatically extend OpenTelemetry instrumentation for new functionality in the MCP Gateway, following established patterns documented in docs/telemetry/README.md.
Automatically apply when:
cmd/docker-mcp/pkg/Read project telemetry standards:
docs/telemetry/README.md "Development Guidelines" sectionpkg/telemetry/telemetry.go to understand existing patterns and metricsIdentify scope using git diff:
pkg/ and cmd/docker-mcp/pkg/foo/ → domain: foo)Categorize findings:
Present suggestions to user:
Execute in this order:
pkg/telemetry/telemetry.godocker logs otel-debug outputpkg/telemetry/telemetry_test.godocs/telemetry/README.mdmake test./docs/telemetry/testing/test-telemetry.shFollow the project's documented guidelines from docs/telemetry/README.md:
DOCKER_MCP_TELEMETRY_DEBUGmcp.<domain>.<field> patternALWAYS read these files before suggesting changes:
docs/telemetry/README.md - Source of truth for:
pkg/telemetry/telemetry.go - Understand:
pkg/telemetry/telemetry_test.go - See:
Use the simple defer pattern from cmd/docker-mcp/catalog/create.go:
func OperationName(ctx context.Context, identifier string, ...) error {
telemetry.Init()
start := time.Now()
var success bool
defer func() {
duration := time.Since(start)
telemetry.Record<Domain>Operation(ctx, "operation_name", identifier,
float64(duration.Milliseconds()), success)
}()
// ... operation logic ...
// Optional: Record resource counts
telemetry.Record<Domain><Resources>(ctx, identifier, int64(count))
success = true
return nil
}
Note: If identifier is generated during execution, the defer captures its final value.
When instrumentation is needed for a new domain (e.g., new package pkg/newdomain/):
pkg/telemetry/telemetry.goIn the Init() function, add global variables and create metric instruments:
var (
// ... existing metrics ...
// New domain metrics
newdomainOperations metric.Int64Counter
newdomainOperationDuration metric.Float64Histogram
newdomainResources metric.Int64Gauge // If managing resources
)
func Init() {
// ... existing init code ...
newdomainOperations, _ = meter.Int64Counter(
"mcp.newdomain.operations",
metric.WithDescription("New domain operations count"),
)
newdomainOperationDuration, _ = meter.Float64Histogram(
"mcp.newdomain.operation.duration",
metric.WithDescription("New domain operation duration in milliseconds"),
)
newdomainResources, _ = meter.Int64Gauge(
"mcp.newdomain.resources",
metric.WithDescription("Number of resources in new domain"),
)
}
pkg/telemetry/telemetry.goAfter the Init() function, add recording functions:
func RecordNewdomainOperation(ctx context.Context, operation, identifier string, durationMs float64, success bool) {
if newdomainOperations == nil || newdomainOperationDuration == nil {
return
}
attrs := []attribute.KeyValue{
attribute.String("mcp.newdomain.operation", operation),
attribute.String("mcp.newdomain.id", identifier), // or .name, .ref as appropriate
attribute.Bool("mcp.newdomain.success", success),
}
newdomainOperations.Add(ctx, 1, metric.WithAttributes(attrs...))
newdomainOperationDuration.Record(ctx, durationMs, metric.WithAttributes(attrs...))
}
// Optional: Add resource counting function if applicable
func RecordNewdomainResources(ctx context.Context, identifier string, count int64) {
if newdomainResources == nil {
return
}
attrs := []attribute.KeyValue{
attribute.String("mcp.newdomain.id", identifier),
}
newdomainResources.Record(ctx, count, metric.WithAttributes(attrs...))
}
pkg/telemetry/telemetry_test.goAdd test cases following existing patterns:
func TestRecordNewdomainOperation(t *testing.T) {
spanRecorder, metricReader := setupTestTelemetry(t)
Init()
ctx := context.Background()
// Test successful operation
RecordNewdomainOperation(ctx, "create", "test-id", 123.45, true)
// Collect and verify metrics
var rm metricdata.ResourceMetrics
err := metricReader.Collect(ctx, &rm)
require.NoError(t, err)
// Find and verify the counter metric
foundCounter := false
foundHistogram := false
for _, sm := range rm.ScopeMetrics {
for _, m := range sm.Metrics {
if m.Name == "mcp.newdomain.operations" {
foundCounter = true
sum := m.Data.(metricdata.Sum[int64])
require.Len(t, sum.DataPoints, 1)
assert.Equal(t, int64(1), sum.DataPoints[0].Value)
// Verify attributes
attrs := sum.DataPoints[0].Attributes
assert.Contains(t, attrs.ToSlice(), attribute.String("mcp.newdomain.operation", "create"))
assert.Contains(t, attrs.ToSlice(), attribute.String("mcp.newdomain.id", "test-id"))
assert.Contains(t, attrs.ToSlice(), attribute.Bool("mcp.newdomain.success", true))
}
if m.Name == "mcp.newdomain.operation.duration" {
foundHistogram = true
histogram := m.Data.(metricdata.Histogram[float64])
require.Len(t, histogram.DataPoints, 1)
assert.Equal(t, float64(123.45), histogram.DataPoints[0].Sum)
}
}
}
assert.True(t, foundCounter, "Counter metric not found")
assert.True(t, foundHistogram, "Histogram metric not found")
}
docs/telemetry/README.mdAdd a new section for the domain in the appropriate location. Follow the existing format:
### New Domain Operations
Operations for managing [description of what this domain does]:
- **`mcp.newdomain.operations`** - New domain operations (create, update, delete, etc.)
- **`mcp.newdomain.operation.duration`** - Duration of new domain operations
- **`mcp.newdomain.resources`** - Gauge showing number of resources in new domain
#### New Domain Attributes
- **`mcp.newdomain.operation`** - Type of operation (create, update, delete, etc.)
- **`mcp.newdomain.id`** - ID of the resource
- **`mcp.newdomain.success`** - Boolean indicating operation success
After making changes, verify telemetry output:
# Build
make docker-mcp
# Start OTEL collector
docker run --rm -d --name otel-debug \
-p 4317:4317 -p 4318:4318 \
-v $(pwd)/docs/telemetry/testing/otel-collector-config.yaml:/config.yaml \
otel/opentelemetry-collector:latest --config=/config.yaml
# Run with telemetry enabled
export DOCKER_MCP_TELEMETRY_DEBUG=1
export DOCKER_CLI_OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4317
docker mcp [command]
# Check collector output
docker logs otel-debug | grep "mcp.newdomain"
# Cleanup
docker stop otel-debug
Verify the output matches expectations before proceeding to write tests and docs.
When analyzing git diff:
Look for operation verbs in function names:
Look for state changes:
Infer domain from file path:
pkg/workingset/ → domain: profilepkg/catalog_next/ → domain: catalog_nextcmd/docker-mcp/server/ → domain: serverCheck if telemetry exists:
pkg/telemetry/telemetry.go for Record<Domain> functionsFollow this sequence:
docs/telemetry/README.mdpkg/telemetry/telemetry.go (if new domain)docker logs otel-debug - confirm output matches expectationspkg/telemetry/telemetry_test.godocs/telemetry/README.md with new metricsmake test - verify tests pass./docs/telemetry/testing/test-telemetry.sh - final verification