Skip to content

Commit

Permalink
fix data race
Browse files Browse the repository at this point in the history
  • Loading branch information
Lifosmin Simon committed Oct 28, 2024
1 parent 0fdc4ec commit 06690ac
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
7 changes: 7 additions & 0 deletions pkg/opentelemetry/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
"go.opentelemetry.io/otel/exporters/stdout/stdouttrace"
"go.opentelemetry.io/otel/propagation"
sdkMetric "go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/resource"
Expand Down Expand Up @@ -106,10 +107,16 @@ func initGlobalTracer(ctx context.Context, res *resource.Resource, cfg Config) (
return nil, fmt.Errorf("create trace exporter: %w", err)
}

stdoutExporter, err := stdouttrace.New(stdouttrace.WithPrettyPrint())
if err != nil {
return nil, fmt.Errorf("create stdout trace exporter: %w", err)
}

tracerProvider := sdktrace.NewTracerProvider(
sdktrace.WithSampler(sdktrace.AlwaysSample()),
sdktrace.WithResource(res),
sdktrace.WithBatcher(exporter),
sdktrace.WithBatcher(stdoutExporter), // Stdout exporter
)

otel.SetTracerProvider(tracerProvider)
Expand Down
4 changes: 4 additions & 0 deletions pkg/opentelemetry/otelhttpclient/http_transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io"
"net/http"
"sync"
"time"

"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
Expand Down Expand Up @@ -136,12 +137,15 @@ type bodyWrapper struct {

read int
err error
mu sync.Mutex
}

func (w *bodyWrapper) Read(b []byte) (int, error) {
n, err := w.ReadCloser.Read(b)
w.mu.Lock()
w.read += n
w.err = err
w.mu.Unlock()
return n, err
}

Expand Down

0 comments on commit 06690ac

Please sign in to comment.