Skip to content

Commit

Permalink
feat: config for custom batch span processor
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-mwangi committed May 23, 2024
1 parent eab5d40 commit 28a86a8
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 22 deletions.
3 changes: 3 additions & 0 deletions config/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,7 @@ var defaultConfig = agentconfig.AgentConfig{
StartupSpanEnabled: agentconfig.Bool(true),
MetricsEnabled: agentconfig.Bool(true),
},
Goagent: &agentconfig.GoAgent{
UseCustomBsp: agentconfig.Bool(true),
},
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/golang/protobuf v1.5.3
github.com/google/uuid v1.6.0
github.com/gorilla/mux v1.8.0
github.com/hypertrace/agent-config/gen/go v0.0.0-20230126205246-bd4d81e696a6
github.com/hypertrace/agent-config/gen/go v0.0.0-20240523173529-cc303141489a
github.com/json-iterator/go v1.1.12 // indirect
github.com/mattn/go-sqlite3 v1.14.4
github.com/ngrok/sqlmw v0.0.0-20200129213757-d5c93a81bec6
Expand All @@ -33,7 +33,7 @@ require (
go.opentelemetry.io/otel/trace v1.24.0
golang.org/x/net v0.23.0 // indirect
google.golang.org/grpc v1.62.1
google.golang.org/protobuf v1.33.0
google.golang.org/protobuf v1.34.1
)

require (
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -559,8 +559,8 @@ github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.0/go.mod h1:qmOFXW2epJhM0qSnUUYp
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/hypertrace/agent-config/gen/go v0.0.0-20230126205246-bd4d81e696a6 h1:MuiFiuigCk2NwMM5HOvI7FJUTZEsGeqA25c4acBjdEs=
github.com/hypertrace/agent-config/gen/go v0.0.0-20230126205246-bd4d81e696a6/go.mod h1:WRbKE44DNsSbRnHja1VpU+dUSrTIuduePGhZ+bXmvDw=
github.com/hypertrace/agent-config/gen/go v0.0.0-20240523173529-cc303141489a h1:Rlr7efDlI8bTecuuLdd2ZycYiM473xaRrP1O0XyAgHk=
github.com/hypertrace/agent-config/gen/go v0.0.0-20240523173529-cc303141489a/go.mod h1:91dQpeta5N46aAFdPGTr6qGCHxoTtMtvrhUOcPCS3B8=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
Expand Down Expand Up @@ -1188,8 +1188,8 @@ google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQ
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg=
google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
Expand Down
13 changes: 8 additions & 5 deletions instrumentation/opentelemetry/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
sdktrace "go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
"go.opentelemetry.io/otel/trace"
"go.opentelemetry.io/otel/trace/noop"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/resolver"
)
Expand Down Expand Up @@ -227,7 +228,7 @@ func InitWithSpanProcessorWrapper(cfg *config.AgentConfig, wrapper SpanProcessor
enabled = cfg.GetEnabled().Value
if !enabled {
initialized = true
otel.SetTracerProvider(trace.NewNoopTracerProvider())
otel.SetTracerProvider(noop.NewTracerProvider())
// even if the tracer isn't enabled, propagation is still enabled
// to not to break the full workflow of the tracing system. Even
// if this service will not report spans and the trace might look
Expand All @@ -249,8 +250,10 @@ func InitWithSpanProcessorWrapper(cfg *config.AgentConfig, wrapper SpanProcessor
log.Fatal(err)
}

useModifiedBsp := (cfg.GetGoagent() != nil && cfg.GetGoagent().GetUseCustomBsp().GetValue()) || // bsp enabled OR
(cfg.GetTelemetry() != nil && cfg.GetTelemetry().GetMetricsEnabled().GetValue()) // metrics enabled
sp := modbsp.CreateBatchSpanProcessor(
cfg.GetTelemetry() != nil && cfg.GetTelemetry().GetMetricsEnabled().GetValue(), // metrics enabled
useModifiedBsp,
exporter,
sdktrace.WithBatchTimeout(batchTimeout))
if wrapper != nil {
Expand Down Expand Up @@ -341,15 +344,15 @@ func RegisterServiceWithSpanProcessorWrapper(serviceName string, resourceAttribu
mu.Lock()
defer mu.Unlock()
if !initialized {
return nil, trace.NewNoopTracerProvider(), fmt.Errorf("hypertrace hadn't been initialized")
return nil, noop.NewTracerProvider(), fmt.Errorf("hypertrace hadn't been initialized")

Check warning on line 347 in instrumentation/opentelemetry/init.go

View check run for this annotation

Codecov / codecov/patch

instrumentation/opentelemetry/init.go#L347

Added line #L347 was not covered by tests
}

if !enabled {
return NoopStartSpan, trace.NewNoopTracerProvider(), nil
return NoopStartSpan, noop.NewTracerProvider(), nil
}

if _, ok := traceProviders[serviceName]; ok {
return nil, trace.NewNoopTracerProvider(), fmt.Errorf("service %v already initialized", serviceName)
return nil, noop.NewTracerProvider(), fmt.Errorf("service %v already initialized", serviceName)

Check warning on line 355 in instrumentation/opentelemetry/init.go

View check run for this annotation

Codecov / codecov/patch

instrumentation/opentelemetry/init.go#L355

Added line #L355 was not covered by tests
}

exporter, err := exporterFactory()
Expand Down
18 changes: 8 additions & 10 deletions instrumentation/opentelemetry/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,20 @@ package opentelemetry

import (
"context"
"google.golang.org/grpc/resolver"
"log"
"net/http"
"net/http/httptest"
"testing"
"time"

"github.com/hypertrace/goagent/config"

"go.opentelemetry.io/otel/propagation"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/trace"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/propagation"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/trace/noop"
"google.golang.org/grpc/resolver"
)

func ExampleInit() {
Expand Down Expand Up @@ -53,7 +51,7 @@ func TestInitDisabledAgent(t *testing.T) {

startSpan, tp, err := RegisterService("test_service", nil)
require.NoError(t, err)
assert.Equal(t, trace.NewNoopTracerProvider(), tp)
assert.Equal(t, noop.NewTracerProvider(), tp)
_, s, _ := startSpan(context.Background(), "test_span", nil)
require.NoError(t, err)
assert.True(t, s.IsNoop())
Expand Down Expand Up @@ -84,7 +82,7 @@ func TestOtlpService(t *testing.T) {
startSpan, tp, err := RegisterService("custom_service", map[string]string{"test1": "val1"})
_, s, _ := startSpan(context.Background(), "test_span", nil)
assert.False(t, s.IsNoop())
assert.NotEqual(t, trace.NewNoopTracerProvider(), tp)
assert.NotEqual(t, noop.NewTracerProvider(), tp)
assert.Len(t, s.GetAttributes().GetValue("service.instance.id"), 36)
if err != nil {
log.Fatalf("Error while initializing service: %v", err)
Expand All @@ -104,7 +102,7 @@ func TestGrpcLoadBalancingConfig(t *testing.T) {

assert.Equal(t, resolver.GetDefaultScheme(), "dns")
_, tp, err := RegisterService("custom_service", map[string]string{"test1": "val1"})
assert.NotEqual(t, trace.NewNoopTracerProvider(), tp)
assert.NotEqual(t, noop.NewTracerProvider(), tp)
if err != nil {
log.Fatalf("Error while initializing service: %v", err)
}
Expand Down Expand Up @@ -171,7 +169,7 @@ func TestMultipleTraceProviders(t *testing.T) {
assert.NotNil(t, startServiceSpan)
assert.True(t, initialized)
assert.Equal(t, 1, len(traceProviders))
assert.NotEqual(t, trace.NewNoopTracerProvider(), tp)
assert.NotEqual(t, noop.NewTracerProvider(), tp)

_, _, serviceSpanEnder := startServiceSpan(context.Background(), "my_span", nil)
serviceSpanEnder()
Expand Down
3 changes: 2 additions & 1 deletion instrumentation/opentelemetry/span_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ import (
"go.opentelemetry.io/otel/attribute"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/trace"
"go.opentelemetry.io/otel/trace/noop"
)

func newNoopSpan() trace.Span {
_, noopSpan := trace.NewNoopTracerProvider().Tracer("noop").Start(context.Background(), "test_name")
_, noopSpan := noop.NewTracerProvider().Tracer("noop").Start(context.Background(), "test_name")
return noopSpan
}

Expand Down

0 comments on commit 28a86a8

Please sign in to comment.