Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ type CB interface {
```

<a name="New"></a>
### func [New](<https://github.com/go-coldbrew/core/blob/main/core.go#L544>)
### func [New](<https://github.com/go-coldbrew/core/blob/main/core.go#L540>)

```go
func New(c config.Config) CB
Expand Down
7 changes: 4 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,10 @@ type Config struct {
// OTLPSamplingRatio is the ratio of traces to sample (0.0 to 1.0)
// 1.0 means sample all traces, 0.1 means sample 10% of traces
OTLPSamplingRatio float64 `envconfig:"OTLP_SAMPLING_RATIO" default:"0.2"`
// OTLPUseOpenTracingBridge determines whether to set up OpenTracing compatibility bridge
// This allows using existing OpenTracing instrumentation with OpenTelemetry
OTLPUseOpenTracingBridge bool `envconfig:"OTLP_USE_OPENTRACING_BRIDGE" default:"true"`
// Deprecated: OpenTracing bridge is provided for backwards compatibility only.
// New services should leave this false (the default). Set to true only if you
// have existing OpenTracing instrumentation that hasn't been migrated to OTEL.
OTLPUseOpenTracingBridge bool `envconfig:"OTLP_USE_OPENTRACING_BRIDGE" default:"false"`
}

// Validate checks the configuration for common misconfigurations and returns
Expand Down
52 changes: 24 additions & 28 deletions core.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ import (
"github.com/go-coldbrew/log"
"github.com/go-coldbrew/log/loggers"
"github.com/go-coldbrew/options"
grpc_opentracing "github.com/grpc-ecosystem/go-grpc-middleware/tracing/opentracing"
grpcprom "github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/klauspost/compress/gzhttp"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/propagation"
semconv "go.opentelemetry.io/otel/semconv/v1.12.0"
oteltrace "go.opentelemetry.io/otel/trace"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
Expand Down Expand Up @@ -146,7 +147,7 @@ func (c *cb) processConfig() {
ServiceVersion: c.config.ReleaseName,
SamplingRatio: c.config.OTLPSamplingRatio,
Compression: c.config.OTLPCompression,
UseOpenTracingBridge: c.config.OTLPUseOpenTracingBridge,
UseOpenTracingBridge: c.config.OTLPUseOpenTracingBridge, //nolint:staticcheck // reading deprecated field for backward compat
Insecure: c.config.OTLPInsecure,
}
if err := SetupOpenTelemetry(otlpConfig); err != nil {
Expand All @@ -166,33 +167,30 @@ func (c *cb) processConfig() {
}
}

// https://grpc-ecosystem.github.io/grpc-gateway/docs/operations/tracing/#opentracing-support
var grpcGatewayTag = opentracing.Tag{Key: string(ext.Component), Value: "grpc-gateway"}

// tracingWrapper is a middleware that creates a new span for each incoming request.
// It also adds the span to the context so it can be used by other middlewares or handlers to add additional tags.
// tracingWrapper is a middleware that creates a new OTEL span for each incoming HTTP request.
// It extracts any propagated trace context from the request headers and, for non-filtered
// methods, starts a server span that is attached to the request context.
func tracingWrapper(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
parentSpanContext, err := opentracing.GlobalTracer().Extract(
opentracing.HTTPHeaders,
opentracing.HTTPHeadersCarrier(r.Header))
if err == nil || err == opentracing.ErrSpanContextNotFound {
if interceptors.FilterMethodsFunc(r.Context(), r.URL.Path) {
serverSpan := opentracing.GlobalTracer().StartSpan(
"ServeHTTP",
// this is magical, it attaches the new span to the parent parentSpanContext, and creates an unparented one if empty.
ext.RPCServerOption(parentSpanContext),
grpcGatewayTag,
opentracing.Tag{Key: string(ext.HTTPUrl), Value: r.URL.Path},
opentracing.Tag{Key: string(ext.HTTPMethod), Value: r.Method},
)
r = r.WithContext(opentracing.ContextWithSpan(r.Context(), serverSpan))
defer serverSpan.Finish()
}
prop := otel.GetTextMapPropagator()
ctx := prop.Extract(r.Context(), propagation.HeaderCarrier(r.Header))
if interceptors.FilterMethodsFunc(ctx, r.URL.Path) {
var serverSpan oteltrace.Span
ctx, serverSpan = otel.Tracer("coldbrew-http").Start(ctx, "ServeHTTP",
oteltrace.WithSpanKind(oteltrace.SpanKindServer),
oteltrace.WithAttributes(
semconv.HTTPMethodKey.String(r.Method),
semconv.HTTPTargetKey.String(r.URL.RequestURI()),
),
)
r = r.WithContext(ctx)
defer serverSpan.End()
} else {
r = r.WithContext(ctx)
}
Comment thread
ankurs marked this conversation as resolved.
_, han := interceptors.NRHttpTracer("", h.ServeHTTP)
// add this info to log
ctx := r.Context()
ctx = r.Context()
ctx = options.AddToOptions(ctx, "", "")
ctx = loggers.AddToLogContext(ctx, "httpPath", r.URL.Path)
r = r.WithContext(ctx)
Expand Down Expand Up @@ -259,8 +257,6 @@ func (c *cb) initHTTP(ctx context.Context) (*http.Server, error) {
grpc.WithTransportCredentials(creds),
grpc.WithUnaryInterceptor(
interceptors.DefaultClientInterceptor(
grpc_opentracing.WithTraceHeaderName(c.config.TraceHeaderName),
grpc_opentracing.WithFilterFunc(interceptors.FilterMethodsFunc),
interceptors.WithoutHystrix(),
),
),
Expand Down
9 changes: 4 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ go 1.25.8

require (
github.com/afex/hystrix-go v0.0.0-20180502004556-fa1af6a1f4f5
github.com/go-coldbrew/errors v0.2.5
github.com/go-coldbrew/errors v0.2.6
github.com/go-coldbrew/hystrixprometheus v0.1.2
github.com/go-coldbrew/interceptors v0.1.12
github.com/go-coldbrew/interceptors v0.1.13
github.com/go-coldbrew/log v0.2.8
github.com/go-coldbrew/options v0.2.6
github.com/go-coldbrew/tracing v0.0.7
github.com/go-coldbrew/tracing v0.2.0
github.com/golang/protobuf v1.5.4
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0
github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus v1.1.0
github.com/grpc-ecosystem/grpc-gateway/v2 v2.28.0
github.com/klauspost/compress v1.18.5
Expand All @@ -23,6 +22,7 @@ require (
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.42.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.42.0
go.opentelemetry.io/otel/sdk v1.42.0
go.opentelemetry.io/otel/trace v1.42.0
go.uber.org/automaxprocs v1.6.0
google.golang.org/grpc v1.79.3
google.golang.org/protobuf v1.36.11
Expand Down Expand Up @@ -262,7 +262,6 @@ require (
go.augendre.info/fatcontext v0.9.0 // indirect
go.opentelemetry.io/auto/sdk v1.2.1 // indirect
go.opentelemetry.io/otel/metric v1.42.0 // indirect
go.opentelemetry.io/otel/trace v1.42.0 // indirect
go.opentelemetry.io/proto/otlp v1.10.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.1 // indirect
Expand Down
Loading
Loading