mirror of
https://github.com/lukaszraczylo/graphql-monitoring-proxy.git
synced 2026-10-20 06:28:13 +00:00
Configuration Management:
Optimized the getDetailsFromEnv function to reduce redundant lookups and improve type handling Added direct environment variable access for better performance Memory Cache Optimization: Implemented a size-based compression threshold (1KB) to avoid compressing small payloads Added cache size limits (10,000 entries) to prevent memory leaks Implemented efficient eviction strategies for the oldest entries Added atomic counter for thread-safe cache size tracking Improved cleanup routines with GC triggering for large caches Proxy Implementation: Refactored the proxy code into smaller, focused functions for better maintainability Optimized gzip handling for better performance Improved error handling and logging Enhanced tracing integration GraphQL Processing: Optimized introspection query checking with fast-path returns Improved object pool usage Added detailed comments for better code understanding Split complex functions into smaller, more focused ones Fixed test compatibility issues with introspection checking Request Processing: Refactored the request processing logic into smaller, focused functions Separated user extraction, caching, and request handling for better maintainability Improved error handling and response generation Tracing Enhancements: Added better span context management Implemented custom attributes for more detailed tracing Added sampling configuration to reduce overhead Improved resource attribution with host and process information Added timeout handling for tracing operations Application Lifecycle: Implemented graceful shutdown with proper signal handling Added goroutine management with wait groups Improved startup sequence with better error handling Added timeout handling for shutdown operations
This commit is contained in:
+65
-5
@@ -4,8 +4,10 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"go.opentelemetry.io/otel"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
|
||||
"go.opentelemetry.io/otel/propagation"
|
||||
"go.opentelemetry.io/otel/sdk/resource"
|
||||
@@ -34,36 +36,63 @@ func NewTracing(ctx context.Context, endpoint string) (*TracingSetup, error) {
|
||||
return nil, fmt.Errorf("endpoint cannot be empty")
|
||||
}
|
||||
|
||||
conn, err := grpc.DialContext(ctx, endpoint,
|
||||
// Create a timeout context for connection establishment
|
||||
dialCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
// Connect to the collector with improved options
|
||||
conn, err := grpc.DialContext(dialCtx, endpoint,
|
||||
grpc.WithTransportCredentials(insecure.NewCredentials()),
|
||||
grpc.WithBlock(),
|
||||
grpc.WithReturnConnectionError(),
|
||||
grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(16*1024*1024)), // 16MB max message size
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create gRPC connection to collector: %w", err)
|
||||
}
|
||||
|
||||
exporter, err := otlptracegrpc.New(ctx, otlptracegrpc.WithGRPCConn(conn))
|
||||
// Create the exporter
|
||||
exporter, err := otlptracegrpc.New(ctx,
|
||||
otlptracegrpc.WithGRPCConn(conn),
|
||||
otlptracegrpc.WithTimeout(5*time.Second),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create trace exporter: %w", err)
|
||||
}
|
||||
|
||||
// Create a resource with more detailed attributes
|
||||
res, err := resource.New(ctx,
|
||||
resource.WithAttributes(
|
||||
semconv.ServiceName("graphql-monitoring-proxy"),
|
||||
semconv.ServiceVersion("1.0"),
|
||||
semconv.DeploymentEnvironment("production"),
|
||||
attribute.String("application.type", "proxy"),
|
||||
),
|
||||
resource.WithHost(), // Add host information
|
||||
resource.WithOSType(), // Add OS information
|
||||
resource.WithProcessPID(), // Add process information
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create resource: %w", err)
|
||||
}
|
||||
|
||||
// Create the tracer provider with improved configuration
|
||||
tracerProvider := sdktrace.NewTracerProvider(
|
||||
sdktrace.WithBatcher(exporter),
|
||||
sdktrace.WithBatcher(exporter,
|
||||
// Configure batch processing
|
||||
sdktrace.WithMaxExportBatchSize(512),
|
||||
sdktrace.WithBatchTimeout(3*time.Second),
|
||||
sdktrace.WithMaxQueueSize(2048),
|
||||
),
|
||||
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{})
|
||||
|
||||
// Create a tracer
|
||||
tracer := tracerProvider.Tracer("graphql-monitoring-proxy")
|
||||
|
||||
return &TracingSetup{
|
||||
@@ -105,9 +134,40 @@ func (ts *TracingSetup) Shutdown(ctx context.Context) error {
|
||||
|
||||
// StartSpan starts a new span with the given name and parent context
|
||||
func (ts *TracingSetup) StartSpan(ctx context.Context, name string) (trace.Span, context.Context) {
|
||||
if ts.tracer == nil {
|
||||
if ts == nil || ts.tracer == nil {
|
||||
// Return a no-op span if tracing is not configured
|
||||
return trace.SpanFromContext(ctx), ctx
|
||||
}
|
||||
ctx, span := ts.tracer.Start(ctx, name)
|
||||
|
||||
// Add common attributes to all spans
|
||||
opts := []trace.SpanStartOption{
|
||||
trace.WithAttributes(
|
||||
semconv.ServiceName("graphql-monitoring-proxy"),
|
||||
semconv.ServiceVersion("1.0"),
|
||||
),
|
||||
}
|
||||
|
||||
ctx, span := ts.tracer.Start(ctx, name, opts...)
|
||||
return span, ctx
|
||||
}
|
||||
|
||||
// StartSpanWithAttributes starts a new span with custom attributes
|
||||
func (ts *TracingSetup) StartSpanWithAttributes(ctx context.Context, name string, attrs map[string]string) (trace.Span, context.Context) {
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user