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

feat: Support environment-service pair to map to traceproviders #240

Closed
wants to merge 4 commits into from
Closed
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
15 changes: 10 additions & 5 deletions instrumentation/opentelemetry/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,12 +351,12 @@ func InitWithSpanProcessorWrapperAndZap(cfg *config.AgentConfig, wrapper SpanPro
return func() {
mu.Lock()
defer mu.Unlock()
for serviceName, tracerProvider := range traceProviders {
for envServicePair, tracerProvider := range traceProviders {
err := tracerProvider.Shutdown(context.Background())
if err != nil {
log.Printf("error while shutting down tracer provider: %v\n", err)
}
delete(traceProviders, serviceName)
delete(traceProviders, envServicePair)
}
traceProviders = map[string]*sdktrace.TracerProvider{}
err := tp.Shutdown(context.Background())
Expand Down Expand Up @@ -405,8 +405,13 @@ func RegisterServiceWithSpanProcessorWrapper(serviceName string, resourceAttribu
return NoopStartSpan, noop.NewTracerProvider(), nil
}

if _, ok := traceProviders[serviceName]; ok {
return nil, noop.NewTracerProvider(), fmt.Errorf("service %v already initialized", serviceName)
environment, ok := resourceAttributes[environmentKey]
if !ok {
environment = defaultEnvironment
}
envServicePair := encodeEnvServicePair(environment, serviceName)
if _, ok := traceProviders[envServicePair]; ok {
return nil, noop.NewTracerProvider(), fmt.Errorf("service %v environment %v already initialized", serviceName, environment)
}

exporter, err := exporterFactory()
Expand Down Expand Up @@ -435,7 +440,7 @@ func RegisterServiceWithSpanProcessorWrapper(serviceName string, resourceAttribu
sdktrace.WithResource(resources),
)

traceProviders[serviceName] = tp
traceProviders[envServicePair] = tp
return startSpan(func() trace.TracerProvider {
return tp
}), tp, nil
Expand Down
9 changes: 9 additions & 0 deletions instrumentation/opentelemetry/init_additional.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ import (
"go.opentelemetry.io/otel/sdk/trace"
)

const (
environmentKey = "deployment.environment"
Copy link
Contributor

Choose a reason for hiding this comment

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

Since this is the key input from gateway agents, I think it should be prefixed with traceableai. I'd say traceableai.environment.name is a better key

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Wanted to keep "traceable" name out of hypertrace repos. Don't know the convention we follow though

Copy link
Contributor

Choose a reason for hiding this comment

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

I'd rather keep environment notion itself out of this repo. It'll be scattered if you add that notion here. Because initially we made it traceable specific one. ref, https://github.com/Traceableai/agent-config/blob/main/proto/ai/traceable/agent/config/v1/config.proto#L22

Copy link
Contributor

Choose a reason for hiding this comment

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

Now it's gonna be like hypertrace agent provides environment specific tracers, but traceable goagent is used for getting environment specific filters when you think about it from a tracer-filter pair pov

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree to keep environment notion out of this repo. Just thinking on lines of making traceable-goagent to have multi environment support as the TA, to register services there we mitigate that logic to RegisterServiceWithSpanProcessorWrapper() (here), the only problem is we assume a unique key for traceProviders here which service name used to do. But if we want to create a multi-env support for traceable-goagent, where a same service name can exist in different environments, this current setup fails due to this traceProviderMap. So here I only intend to use unique key to create tracer.
May be other way is to form a unique key in traceable-goagent itself, and use that to call the RegisterServiceWithSpanProcessorWrapper(unique_key, resourceAttributes), but as here we add resource attrs, didn't feel it generic enough and dropped that idea

Copy link
Contributor

Choose a reason for hiding this comment

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

I still dont understand why you can't add all of this logic in TPA itself?

Copy link
Contributor

@varkey98 varkey98 Oct 25, 2024

Choose a reason for hiding this comment

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

Is it that the argument name is service_name across our api, but actually we would be passing environment::sep::service_name?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I still dont understand why you can't add all of this logic in TPA itself?

Currently we have for each service in extcap a unique tracerfilterpair .The tracerSets of ext_cap(link) use goagent to create a tracer, which in current setup will fail for cases of same service name which can be case in multiple environments. So to avoid depending on only service name as key which can no longer be unique, formed the unique key here (encode) itself in PR. Although this could could come from TPA itself, then we should decode for place like here to get to add resource attrs, and get service name etc correctly.

Copy link
Contributor

Choose a reason for hiding this comment

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

why do you want to decode the value? Afair, the map itself would be coming from TPA right?

defaultEnvironment = "default"
)

// InitAsAdditional initializes opentelemetry tracing and returns a span processor and a shutdown
// function to flush data immediately on a termination signal.
// This is ideal for when we use goagent along with other opentelemetry setups.
Expand Down Expand Up @@ -151,3 +156,7 @@ func MakeRemoveGoAgentAttrs(attrsRemovalPrefixes []string) func(sp trace.SpanExp
return &attrsRemover{sp, attrsRemovalPrefixes}
}
}

func encodeEnvServicePair(environment string, serviceName string) string {
return environment + "::SEP::" + serviceName
}
31 changes: 31 additions & 0 deletions instrumentation/opentelemetry/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,3 +441,34 @@ func TestConfigFactory(t *testing.T) {
factory := makeConfigFactory(cfg)
assert.Same(t, cfg, factory())
}

func TestMultipleRegisterService(t *testing.T) {
cfg := config.Load()
cfg.ServiceName = config.String("my_example_svc")
cfg.DataCapture.HttpHeaders.Request = config.Bool(true)
cfg.Reporting.TraceReporterType = config.TraceReporterType_LOGGING

shutdown := Init(cfg)
defer shutdown()

_, _, err := RegisterService("custom_service_1", map[string]string{environmentKey: "env_1"})
assert.Nil(t, err)

_, _, err = RegisterService("custom_service_2", map[string]string{environmentKey: "env_1"})
assert.Nil(t, err)

_, _, err = RegisterService("custom_service_2", map[string]string{environmentKey: "env_2"})
assert.Nil(t, err)

_, _, err = RegisterService("custom_service_1", map[string]string{environmentKey: "env_1"})
assert.NotNil(t, err)

_, _, err = RegisterService("custom_service_1", map[string]string{})
assert.Nil(t, err)

_, _, err = RegisterService("custom_service_2", map[string]string{})
assert.Nil(t, err)

_, _, err = RegisterService("custom_service_1", map[string]string{})
assert.NotNil(t, err)
}
Loading