Gofmt the codebase.

This commit is contained in:
2025-02-26 00:47:41 +00:00
parent 66c8fef24d
commit 1b7890f322
20 changed files with 349 additions and 348 deletions
+8 -8
View File
@@ -68,8 +68,8 @@ func NewTracing(ctx context.Context, endpoint string) (*TracingSetup, error) {
semconv.DeploymentEnvironment("production"),
attribute.String("application.type", "proxy"),
),
resource.WithHost(), // Add host information
resource.WithOSType(), // Add OS information
resource.WithHost(), // Add host information
resource.WithOSType(), // Add OS information
resource.WithProcessPID(), // Add process information
)
if err != nil {
@@ -87,7 +87,7 @@ func NewTracing(ctx context.Context, endpoint string) (*TracingSetup, error) {
sdktrace.WithResource(res),
sdktrace.WithSampler(sdktrace.TraceIDRatioBased(0.1)), // Sample 10% of traces
)
// Set the global tracer provider and propagator
otel.SetTracerProvider(tracerProvider)
otel.SetTextMapPropagator(propagation.TraceContext{})
@@ -138,7 +138,7 @@ func (ts *TracingSetup) StartSpan(ctx context.Context, name string) (trace.Span,
// Return a no-op span if tracing is not configured
return trace.SpanFromContext(ctx), ctx
}
// Add common attributes to all spans
opts := []trace.SpanStartOption{
trace.WithAttributes(
@@ -146,7 +146,7 @@ func (ts *TracingSetup) StartSpan(ctx context.Context, name string) (trace.Span,
semconv.ServiceVersion("1.0"),
),
}
ctx, span := ts.tracer.Start(ctx, name, opts...)
return span, ctx
}
@@ -156,18 +156,18 @@ func (ts *TracingSetup) StartSpanWithAttributes(ctx context.Context, name string
if ts == nil || ts.tracer == nil {
return trace.SpanFromContext(ctx), ctx
}
// Convert string attributes to KeyValue pairs
attributes := make([]attribute.KeyValue, 0, len(attrs)+2)
attributes = append(attributes,
semconv.ServiceName("graphql-monitoring-proxy"),
semconv.ServiceVersion("1.0"),
)
for k, v := range attrs {
attributes = append(attributes, attribute.String(k, v))
}
ctx, span := ts.tracer.Start(ctx, name, trace.WithAttributes(attributes...))
return span, ctx
}
+22 -22
View File
@@ -27,7 +27,7 @@ func TestStartSpanWithAttributes(t *testing.T) {
span, newCtx := ts.StartSpanWithAttributes(ctx, "test-span", attrs)
assert.NotNil(t, span)
assert.NotNil(t, newCtx)
// We can't easily test the attributes were set since it's a noop tracer,
// but we can verify the function doesn't panic
span.End()
@@ -36,7 +36,7 @@ func TestStartSpanWithAttributes(t *testing.T) {
// Test with nil attributes
t.Run("with nil attributes", func(t *testing.T) {
ctx := context.Background()
span, newCtx := ts.StartSpanWithAttributes(ctx, "test-span", nil)
assert.NotNil(t, span)
assert.NotNil(t, newCtx)
@@ -47,7 +47,7 @@ func TestStartSpanWithAttributes(t *testing.T) {
t.Run("with nil tracer", func(t *testing.T) {
ctx := context.Background()
nilTS := &TracingSetup{tracer: nil}
span, newCtx := nilTS.StartSpanWithAttributes(ctx, "test-span", map[string]string{"key": "value"})
assert.NotNil(t, span)
assert.NotNil(t, newCtx)
@@ -58,19 +58,19 @@ func TestStartSpanWithAttributes(t *testing.T) {
func TestNewTracingWithInvalidEndpoint(t *testing.T) {
ctx := context.Background()
// Test with invalid endpoint format
t.Run("invalid endpoint format", func(t *testing.T) {
_, err := NewTracing(ctx, "invalid:endpoint:format")
assert.Error(t, err)
})
// Test with unreachable endpoint
t.Run("unreachable endpoint", func(t *testing.T) {
// Use a timeout to avoid long test times
ctx, cancel := context.WithTimeout(ctx, 100*time.Millisecond)
defer cancel()
_, err := NewTracing(ctx, "localhost:1") // Port 1 is typically unused
assert.Error(t, err)
})
@@ -80,28 +80,28 @@ func TestTracingSetupWithMockTracer(t *testing.T) {
// Create a mock tracer provider
mockTracerProvider := trace.NewNoopTracerProvider()
mockTracer := mockTracerProvider.Tracer("mock-tracer")
ts := &TracingSetup{
tracerProvider: nil, // We don't need the provider for these tests
tracer: mockTracer,
}
// Test StartSpan
t.Run("start span", func(t *testing.T) {
ctx := context.Background()
span, newCtx := ts.StartSpan(ctx, "test-span")
assert.NotNil(t, span)
assert.NotNil(t, newCtx)
// Add some attributes and events to ensure no panics
span.SetAttributes(attribute.String("test", "value"))
span.AddEvent("test-event")
// End the span
span.End()
})
// Test StartSpanWithAttributes
t.Run("start span with attributes", func(t *testing.T) {
ctx := context.Background()
@@ -109,12 +109,12 @@ func TestTracingSetupWithMockTracer(t *testing.T) {
"service": "test-service",
"version": "1.0.0",
}
span, newCtx := ts.StartSpanWithAttributes(ctx, "test-span-with-attrs", attrs)
assert.NotNil(t, span)
assert.NotNil(t, newCtx)
// End the span
span.End()
})
@@ -125,10 +125,10 @@ func TestShutdownWithNilProvider(t *testing.T) {
tracerProvider: nil,
tracer: trace.NewNoopTracerProvider().Tracer("test"),
}
ctx := context.Background()
err := ts.Shutdown(ctx)
assert.NoError(t, err)
}
@@ -136,13 +136,13 @@ func TestExtractSpanContextWithInvalidTraceParent(t *testing.T) {
ts := &TracingSetup{
tracer: trace.NewNoopTracerProvider().Tracer("test"),
}
// Test with invalid traceparent format
t.Run("invalid traceparent format", func(t *testing.T) {
spanInfo := &TraceSpanInfo{
TraceParent: "invalid-format",
}
_, err := ts.ExtractSpanContext(spanInfo)
assert.Error(t, err)
assert.Contains(t, err.Error(), "invalid span context")
@@ -155,16 +155,16 @@ func TestParseTraceHeaderWithEmptyHeader(t *testing.T) {
_, err := ParseTraceHeader("")
assert.Error(t, err)
})
// Test with invalid JSON
t.Run("invalid JSON", func(t *testing.T) {
_, err := ParseTraceHeader("{invalid json}")
assert.Error(t, err)
})
// Test with valid JSON but missing traceparent
t.Run("missing traceparent", func(t *testing.T) {
_, err := ParseTraceHeader(`{"other": "value"}`)
assert.NoError(t, err) // This should parse but the traceparent will be empty
})
}
}