Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: one exporter to rule them all #243

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
22 changes: 17 additions & 5 deletions instrumentation/opentelemetry/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"crypto/tls"
"crypto/x509"
"fmt"
"go.uber.org/zap"
"log"
"net/http"
"os"
Expand Down Expand Up @@ -40,6 +39,7 @@ import (
semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
"go.opentelemetry.io/otel/trace"
"go.opentelemetry.io/otel/trace/noop"
"go.uber.org/zap"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/resolver"
)
Expand All @@ -51,6 +51,8 @@ var (
initialized = false
enabled = false
mu sync.Mutex
globalExporter sdktrace.SpanExporter
globalExporterErr error
exporterFactory func() (sdktrace.SpanExporter, error)
configFactory func() *config.AgentConfig
versionInfoAttributes = []attribute.KeyValue{
Expand Down Expand Up @@ -132,6 +134,11 @@ func makeMetricsExporterFactory(cfg *config.AgentConfig) func() (metric.Exporter
}

func makeExporterFactory(cfg *config.AgentConfig) func() (sdktrace.SpanExporter, error) {
if globalExporter != nil || globalExporterErr != nil {
return func() (sdktrace.SpanExporter, error) {
return globalExporter, globalExporterErr
}
}
switch cfg.Reporting.TraceReporterType {
case config.TraceReporterType_ZIPKIN:
client := &http.Client{
Expand All @@ -141,15 +148,17 @@ func makeExporterFactory(cfg *config.AgentConfig) func() (sdktrace.SpanExporter,
}

return func() (sdktrace.SpanExporter, error) {
return zipkin.New(
globalExporter, globalExporterErr = zipkin.New(
cfg.GetReporting().GetEndpoint().GetValue(),
zipkin.WithClient(client),
)
return globalExporter, globalExporterErr
}
case config.TraceReporterType_LOGGING:
return func() (sdktrace.SpanExporter, error) {
// TODO: Define if endpoint could be a filepath to write into a file.
return stdouttrace.New(stdouttrace.WithPrettyPrint())
globalExporter, globalExporterErr = stdouttrace.New(stdouttrace.WithPrettyPrint())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need to be thread safe? If yes, do we need to worry about races resulting in some dangling exporters or that’s not a big deal?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe not since it would set up in the Init method. Unless all the consumers do not call Init. But I will shelf this one for now since I did not observe any remarkable improvement with one exporter vs multiple exporters.

Copy link
Collaborator

@ryanericson ryanericson Dec 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not opposed to shelving it, but why no improvement? This should help to reduce number of export connections?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't have time to verify that using lsof or some other networking tool. I suspect reducing clients does not mean reducing connections as well. The clients maybe fewer but the connection count may still be the same. I would want to verify it next.

return globalExporter, globalExporterErr
}

case config.TraceReporterType_OTLP_HTTP:
Expand All @@ -167,7 +176,8 @@ func makeExporterFactory(cfg *config.AgentConfig) func() (sdktrace.SpanExporter,
}

return func() (sdktrace.SpanExporter, error) {
return otlphttp.New(context.Background(), opts...)
globalExporter, globalExporterErr = otlphttp.New(context.Background(), opts...)
return globalExporter, globalExporterErr
}

default:
Expand All @@ -194,10 +204,11 @@ func makeExporterFactory(cfg *config.AgentConfig) func() (sdktrace.SpanExporter,
}

return func() (sdktrace.SpanExporter, error) {
return otlptrace.New(
globalExporter, globalExporterErr = otlptrace.New(
context.Background(),
otlpgrpc.NewClient(opts...),
)
return globalExporter, globalExporterErr
}
}
}
Expand Down Expand Up @@ -408,6 +419,7 @@ func RegisterServiceWithSpanProcessorWrapper(key string, resourceAttributes map[
return nil, noop.NewTracerProvider(), fmt.Errorf("key %v is already used for initialization", key)
}

// TODO: Invoking this to get a different exporter for each registered service is the offending code I believe. We need to have a single traces exporter used by all the services
exporter, err := exporterFactory()
if err != nil {
log.Fatal(err)
Expand Down
Loading