From 3fb7c7e0b17153d1a16d79b64b2b094945bf2d41 Mon Sep 17 00:00:00 2001 From: Lukasz Raczylo Date: Thu, 20 Jun 2024 16:00:51 +0100 Subject: [PATCH] fixup! Aligning struct fields for better memory management. --- .github/workflows/autoupdate.yaml | 2 +- README.md | 2 +- events.go | 4 ++ main.go | 4 +- proxy.go | 6 +++ server.go | 9 +++- trace/tracing.go | 89 ------------------------------- 7 files changed, 21 insertions(+), 95 deletions(-) delete mode 100644 trace/tracing.go diff --git a/.github/workflows/autoupdate.yaml b/.github/workflows/autoupdate.yaml index 2cc5f59..8e9886d 100644 --- a/.github/workflows/autoupdate.yaml +++ b/.github/workflows/autoupdate.yaml @@ -56,7 +56,7 @@ jobs: apt-get install ca-certificates make -y update-ca-certificates go mod tidy - get -u -v ./... + go get -u -v ./... go mod tidy -v - name: Run unit tests diff --git a/README.md b/README.md index c7bdbfd..fe8df96 100644 --- a/README.md +++ b/README.md @@ -298,7 +298,7 @@ If you prefer more control over the metrics purging - you can enable `PURGE_METR #### Tracing -Tracing can be enabled by setting `ENABLE_TRACE` to `true` and providing compatible with OTEL `TRACER_ENDPOINT` value ( default is `localhost:4317` ). From that moment you can include `X-Trace-Span` in your requests to the proxy. +Tracing can be enabled by setting `ENABLE_TRACE` to `true` and providing compatible with OTEL `TRACER_ENDPOINT` value ( default is `localhost:4317` ). From that moment you can include `X-Trace-Span` with content being json encoded in your requests to the proxy endpoint. The value of X-Trace-Span should be in following format: diff --git a/events.go b/events.go index b917db7..7714340 100644 --- a/events.go +++ b/events.go @@ -74,4 +74,8 @@ func cleanEvents() { }) } } + cfg.Logger.Info(&libpack_logger.LogMessage{ + Message: "Old events cleaned up", + Pairs: nil, + }) } diff --git a/main.go b/main.go index e99ba59..104ebc8 100644 --- a/main.go +++ b/main.go @@ -12,7 +12,7 @@ import ( libpack_cache "github.com/lukaszraczylo/graphql-monitoring-proxy/cache" libpack_config "github.com/lukaszraczylo/graphql-monitoring-proxy/config" libpack_logging "github.com/lukaszraczylo/graphql-monitoring-proxy/logging" - libpack_trace "github.com/lukaszraczylo/graphql-monitoring-proxy/trace" + libpack_trace "github.com/lukaszraczylo/graphql-monitoring-proxy/tracing" ) var cfg *config @@ -111,7 +111,7 @@ func parseConfig() { once.Do(func() { if cfg.Trace.Enable { var err error - cfg.Trace.Client, err = libpack_trace.NewClient(c.Trace.TraceEndpoint) + cfg.Trace.Client, err = libpack_trace.NewClient(cfg.Logger, cfg.Trace.TraceEndpoint) if err != nil { cfg.Logger.Error(&libpack_logging.LogMessage{ Message: "Failed to start tracer", diff --git a/proxy.go b/proxy.go index 93b6bd9..d2aff9b 100644 --- a/proxy.go +++ b/proxy.go @@ -119,6 +119,12 @@ func proxyTheRequest(c *fiber.Ctx, currentEndpoint string) error { if ifNotInTest() { cfg.Monitoring.Increment(libpack_monitoring.MetricsFailed, nil) } + cfg.Logger.Error(&libpack_logger.LogMessage{ + Message: "Received non-200 response from the GraphQL server", + Pairs: map[string]interface{}{ + "status_code": c.Response().StatusCode(), + }, + }) return fmt.Errorf("Received non-200 response from the GraphQL server: %d", c.Response().StatusCode()) } diff --git a/server.go b/server.go index 05ed401..9dc40a5 100644 --- a/server.go +++ b/server.go @@ -15,7 +15,7 @@ import ( libpack_config "github.com/lukaszraczylo/graphql-monitoring-proxy/config" libpack_logger "github.com/lukaszraczylo/graphql-monitoring-proxy/logging" libpack_monitoring "github.com/lukaszraczylo/graphql-monitoring-proxy/monitoring" - libpack_trace "github.com/lukaszraczylo/graphql-monitoring-proxy/trace" + libpack_trace "github.com/lukaszraczylo/graphql-monitoring-proxy/tracing" ) // StartHTTPProxy starts the HTTP and points it to the GraphQL server. @@ -123,8 +123,13 @@ func processGraphQLRequest(c *fiber.Ctx) error { }) } ctx := libpack_trace.TraceContextExtract(context.Background(), traceHeaders) - _, span := libpack_trace.ContinueSpanFromContext(ctx, "processingGraphQLRequest") + _, span := libpack_trace.ContinueSpanFromContext(ctx, "GraphQLRequest") defer span.End() + } else { + cfg.Logger.Warning(&libpack_logger.LogMessage{ + Message: "No trace header found", + Pairs: nil, + }) } } diff --git a/trace/tracing.go b/trace/tracing.go deleted file mode 100644 index 2496da5..0000000 --- a/trace/tracing.go +++ /dev/null @@ -1,89 +0,0 @@ -package libpack_trace - -import ( - "context" - "fmt" - "time" - - libpack_config "github.com/lukaszraczylo/graphql-monitoring-proxy/config" - "go.opentelemetry.io/otel" - "go.opentelemetry.io/otel/attribute" - "go.opentelemetry.io/otel/exporters/otlp/otlptrace" - "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc" - "go.opentelemetry.io/otel/propagation" - "go.opentelemetry.io/otel/sdk/resource" - "go.opentelemetry.io/otel/sdk/trace" - oteltrace "go.opentelemetry.io/otel/trace" - - semconv "go.opentelemetry.io/otel/semconv/v1.4.0" -) - -func NewClient(otelGRPCCollector string, attr ...attribute.KeyValue) (func(), error) { - attr = append(attr, semconv.ServiceNameKey.String(libpack_config.PKG_NAME)) - fmt.Printf("Starting OpenTelemetry tracer: otlp, configured with endpoint: %s\n", otelGRPCCollector) - - ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) - defer cancel() - - client := otlptracegrpc.NewClient( - otlptracegrpc.WithInsecure(), - otlptracegrpc.WithEndpoint(otelGRPCCollector), - ) - - exporter, err := otlptrace.New(ctx, client) - if err != nil { - fmt.Printf("ERROR: failed to create exporter: %v\n", err) - return nil, err - } - - tp := trace.NewTracerProvider( - trace.WithSampler(trace.AlwaysSample()), - trace.WithBatcher(exporter, trace.WithMaxExportBatchSize(1), trace.WithBatchTimeout(30*time.Second)), - trace.WithResource(resource.NewWithAttributes(semconv.SchemaURL, attr...)), - ) - - otel.SetTracerProvider(tp) - otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{})) - - shutdownFunc := func() { - shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 5*time.Second) - defer shutdownCancel() - fmt.Println("Shutting down otlp tracer") - if err := tp.Shutdown(shutdownCtx); err != nil { - fmt.Printf("ERROR: failed to shutdown tracer provider: %v\n", err) - } - } - - return shutdownFunc, nil -} - -func TraceContextInject(ctx context.Context) map[string]string { - carrier := propagation.MapCarrier{} - propagator := otel.GetTextMapPropagator() - propagator.Inject(ctx, carrier) - return map[string]string(carrier) -} - -func TraceContextExtract(ctx context.Context, traceContext map[string]string) context.Context { - carrier := propagation.MapCarrier(traceContext) - propagator := otel.GetTextMapPropagator() - return propagator.Extract(ctx, carrier) -} - -func StartSpanFromContext(ctx context.Context, operationName string) (context.Context, oteltrace.Span) { - tr := otel.GetTracerProvider().Tracer("") - return tr.Start(ctx, operationName, oteltrace.WithSpanKind(oteltrace.SpanKindServer)) -} - -func ContinueSpanFromContext(ctx context.Context, operationName string) (context.Context, oteltrace.Span) { - tr := otel.GetTracerProvider().Tracer("") - options := []oteltrace.SpanStartOption{ - oteltrace.WithSpanKind(oteltrace.SpanKindInternal), - oteltrace.WithAttributes(attribute.String("cont", "true")), - } - return tr.Start(ctx, operationName, options...) -} - -func AddAttributesToSpan(span oteltrace.Span, attributes ...attribute.KeyValue) { - span.SetAttributes(attributes...) -}