fixup! Aligning struct fields for better memory management.

This commit is contained in:
2024-06-20 16:00:51 +01:00
parent a4f7e1690d
commit 3fb7c7e0b1
7 changed files with 21 additions and 95 deletions
+1 -1
View File
@@ -56,7 +56,7 @@ jobs:
apt-get install ca-certificates make -y apt-get install ca-certificates make -y
update-ca-certificates update-ca-certificates
go mod tidy go mod tidy
get -u -v ./... go get -u -v ./...
go mod tidy -v go mod tidy -v
- name: Run unit tests - name: Run unit tests
+1 -1
View File
@@ -298,7 +298,7 @@ If you prefer more control over the metrics purging - you can enable `PURGE_METR
#### Tracing #### 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: The value of X-Trace-Span should be in following format:
+4
View File
@@ -74,4 +74,8 @@ func cleanEvents() {
}) })
} }
} }
cfg.Logger.Info(&libpack_logger.LogMessage{
Message: "Old events cleaned up",
Pairs: nil,
})
} }
+2 -2
View File
@@ -12,7 +12,7 @@ import (
libpack_cache "github.com/lukaszraczylo/graphql-monitoring-proxy/cache" libpack_cache "github.com/lukaszraczylo/graphql-monitoring-proxy/cache"
libpack_config "github.com/lukaszraczylo/graphql-monitoring-proxy/config" libpack_config "github.com/lukaszraczylo/graphql-monitoring-proxy/config"
libpack_logging "github.com/lukaszraczylo/graphql-monitoring-proxy/logging" 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 var cfg *config
@@ -111,7 +111,7 @@ func parseConfig() {
once.Do(func() { once.Do(func() {
if cfg.Trace.Enable { if cfg.Trace.Enable {
var err error 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 { if err != nil {
cfg.Logger.Error(&libpack_logging.LogMessage{ cfg.Logger.Error(&libpack_logging.LogMessage{
Message: "Failed to start tracer", Message: "Failed to start tracer",
+6
View File
@@ -119,6 +119,12 @@ func proxyTheRequest(c *fiber.Ctx, currentEndpoint string) error {
if ifNotInTest() { if ifNotInTest() {
cfg.Monitoring.Increment(libpack_monitoring.MetricsFailed, nil) 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()) return fmt.Errorf("Received non-200 response from the GraphQL server: %d", c.Response().StatusCode())
} }
+7 -2
View File
@@ -15,7 +15,7 @@ import (
libpack_config "github.com/lukaszraczylo/graphql-monitoring-proxy/config" libpack_config "github.com/lukaszraczylo/graphql-monitoring-proxy/config"
libpack_logger "github.com/lukaszraczylo/graphql-monitoring-proxy/logging" libpack_logger "github.com/lukaszraczylo/graphql-monitoring-proxy/logging"
libpack_monitoring "github.com/lukaszraczylo/graphql-monitoring-proxy/monitoring" 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. // 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) ctx := libpack_trace.TraceContextExtract(context.Background(), traceHeaders)
_, span := libpack_trace.ContinueSpanFromContext(ctx, "processingGraphQLRequest") _, span := libpack_trace.ContinueSpanFromContext(ctx, "GraphQLRequest")
defer span.End() defer span.End()
} else {
cfg.Logger.Warning(&libpack_logger.LogMessage{
Message: "No trace header found",
Pairs: nil,
})
} }
} }
-89
View File
@@ -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...)
}