diff --git a/cmd/otelfaker/client.go b/cmd/otelfaker/client.go index 3eb214b0..987d94c9 100644 --- a/cmd/otelfaker/client.go +++ b/cmd/otelfaker/client.go @@ -7,86 +7,53 @@ import ( "github.com/go-faster/sdk/app" "github.com/go-faster/sdk/zctx" - "github.com/prometheus/client_golang/prometheus" - "github.com/prometheus/client_golang/prometheus/promhttp" "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp" "go.uber.org/zap" - "golang.org/x/sync/errgroup" ) func client(ctx context.Context, lg *zap.Logger, m *app.Metrics) error { - g, ctx := errgroup.WithContext(ctx) - mux := http.NewServeMux() - registry := prometheus.NewRegistry() - registerer := prometheus.WrapRegistererWith(prometheus.Labels{ - "service_name": "client", - "service_namespace": "demo", - }, registry) - - // Expose /metrics HTTP endpoint using the created custom registry. - mux.Handle("/metrics", promhttp.HandlerFor(registry, promhttp.HandlerOpts{ - EnableOpenMetrics: true, - Registry: registerer, - })) - srv := &http.Server{ - ReadHeaderTimeout: time.Second, - Addr: "0.0.0.0:8080", - Handler: mux, + httpTransport := otelhttp.NewTransport(http.DefaultTransport, + otelhttp.WithTracerProvider(m.TracerProvider()), + otelhttp.WithMeterProvider(m.MeterProvider()), + ) + httpClient := &http.Client{ + Transport: httpTransport, + Timeout: time.Second * 10, } - g.Go(func() error { - lg.Info("server listening", zap.String("addr", srv.Addr)) - defer lg.Info("server stopped") - return srv.ListenAndServe() - }) - g.Go(func() error { - <-ctx.Done() - return srv.Shutdown(ctx) - }) - g.Go(func() error { - httpTransport := otelhttp.NewTransport(http.DefaultTransport, - otelhttp.WithTracerProvider(m.TracerProvider()), - otelhttp.WithMeterProvider(m.MeterProvider()), - ) - httpClient := &http.Client{ - Transport: httpTransport, - Timeout: time.Second * 10, - } - tracer := m.TracerProvider().Tracer("client") - sendRequest := func(ctx context.Context) { - ctx, cancel := context.WithTimeout(ctx, time.Second*2) - defer cancel() + tracer := m.TracerProvider().Tracer("client") + sendRequest := func(ctx context.Context) { + ctx, cancel := context.WithTimeout(ctx, time.Second*2) + defer cancel() - ctx, span := tracer.Start(ctx, "sendRequest") - defer span.End() + ctx, span := tracer.Start(ctx, "sendRequest") + defer span.End() - req, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://server:8080/api/hello", http.NoBody) - if err != nil { - lg.Error("create request", zap.Error(err)) - return - } - resp, err := httpClient.Do(req) - if err != nil { - lg.Error("send request", zap.Error(err)) - return - } - _ = resp.Body.Close() - - zctx.From(ctx).Info("got response", - zap.Int("status", resp.StatusCode), - zap.String("url", req.URL.String()), - ) + req, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://server:8080/api/hello", http.NoBody) + if err != nil { + lg.Error("create request", zap.Error(err)) + return } - sendRequest(ctx) - ticker := time.NewTicker(time.Second) - defer ticker.Stop() - for { - select { - case <-ctx.Done(): - return ctx.Err() - case <-ticker.C: - sendRequest(ctx) - } + resp, err := httpClient.Do(req) + if err != nil { + lg.Error("send request", zap.Error(err)) + return } - }) - return g.Wait() + _ = resp.Body.Close() + + zctx.From(ctx).Info("got response", + zap.Int("status", resp.StatusCode), + zap.String("url", req.URL.String()), + ) + } + sendRequest(ctx) + ticker := time.NewTicker(time.Second) + defer ticker.Stop() + for { + select { + case <-ctx.Done(): + return ctx.Err() + case <-ticker.C: + sendRequest(ctx) + } + } } diff --git a/cmd/otelfaker/server.go b/cmd/otelfaker/server.go index bc5e3e90..0ef22c25 100644 --- a/cmd/otelfaker/server.go +++ b/cmd/otelfaker/server.go @@ -27,17 +27,10 @@ func server(ctx context.Context, lg *zap.Logger, m *app.Metrics) error { }) registry := prometheus.NewRegistry() - registerer := prometheus.WrapRegistererWith(prometheus.Labels{ - "service_name": "client", - "service_namespace": "demo", - }, registry) - registerer.MustRegister( - requestDurations, - ) + registry.MustRegister(requestDurations) // Expose /metrics HTTP endpoint using the created custom registry. mux.Handle("/metrics", promhttp.HandlerFor(registry, promhttp.HandlerOpts{ - Registry: registerer, EnableOpenMetrics: true, })) tracer := m.TracerProvider().Tracer("server") diff --git a/dev/local/ch-demo/docker-compose.yml b/dev/local/ch-demo/docker-compose.yml index 054b0367..4fd52239 100644 --- a/dev/local/ch-demo/docker-compose.yml +++ b/dev/local/ch-demo/docker-compose.yml @@ -43,7 +43,7 @@ services: - OTEL_EXPORTER_OTLP_TRACES_PROTOCOL=grpc - OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=http://otelcol:4317 - OTEL_EXPORTER_PROMETHEUS_HOST=0.0.0.0 - - OTEL_RESOURCE_ATTRIBUTES=service.name=api,service.namespace=demo,oteldb.faker=true + - OTEL_RESOURCE_ATTRIBUTES=service.name=client server: restart: always command: ["server"] @@ -55,7 +55,7 @@ services: - OTEL_EXPORTER_OTLP_TRACES_PROTOCOL=grpc - OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=http://otelcol:4317 - OTEL_EXPORTER_PROMETHEUS_HOST=0.0.0.0 - - OTEL_RESOURCE_ATTRIBUTES=service.name=server,service.namespace=demo,oteldb.faker=true + - OTEL_RESOURCE_ATTRIBUTES=service.name=server oteldb: build: diff --git a/dev/local/ch-demo/otelcol.yml b/dev/local/ch-demo/otelcol.yml index e1bfaef3..8d9908d2 100644 --- a/dev/local/ch-demo/otelcol.yml +++ b/dev/local/ch-demo/otelcol.yml @@ -6,12 +6,10 @@ receivers: prometheus: config: scrape_configs: - - job_name: demo + - job_name: server scrape_interval: 1s static_configs: - - targets: - - api:8080 - - server:8080 + - targets: [server:8080] processors: batch: timeout: 5s @@ -21,15 +19,6 @@ processors: check_interval: 5s limit_mib: 256 spike_limit_mib: 500 - # normalize between prometheus and otel - resource/normalize: - attributes: - - key: service.name - from_attribute: service_name - action: insert - - key: service.namespace - from_attribute: service_namespace - action: insert exporters: otlp: @@ -55,7 +44,7 @@ service: exporters: [otlp] metrics/scrape: receivers: [prometheus] - processors: [resource/normalize, attributes/cleanup, batch] + processors: [batch] exporters: [otlp] logs: receivers: [otlp]