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

[confighttp]: add an option to add span formatter #11230

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
25 changes: 25 additions & 0 deletions .chloggen/add-new-server-option.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
component: confighttp

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Add an option to add prefix for span name for components

# One or more tracking issues or pull requests related to the change
issues: [9382]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: []
17 changes: 14 additions & 3 deletions config/confighttp/confighttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,7 @@ func (hss *ServerConfig) ToListener(ctx context.Context) (net.Listener, error) {
type toServerOptions struct {
errHandler func(w http.ResponseWriter, r *http.Request, errorMsg string, statusCode int)
decoders map[string]func(body io.ReadCloser) (io.ReadCloser, error)
spanPrefix string
}

// ToServerOption is an option to change the behavior of the HTTP server
Expand Down Expand Up @@ -410,6 +411,14 @@ func WithDecoder(key string, dec func(body io.ReadCloser) (io.ReadCloser, error)
})
}

// WithSpanPrefix creates a span prefixed with the specified name.
// Ideally, this prefix should be the component's ID.
func WithSpanPrefix(spanPrefix string) ToServerOption {
return toServerOptionFunc(func(opts *toServerOptions) {
opts.spanPrefix = spanPrefix
})
}

Choose a reason for hiding this comment

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

We should make it a bit more generic and accept otelhttp.Option or at least accept exactly same formatter? You may now want this, but later people may want other things, etc.

Choose a reason for hiding this comment

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

The problem though, is that otelhttp is not stable, and we are looking to mark this stable soon. So I think we may not be able to accept this until otelhttp is marked stable.

Copy link
Member

Choose a reason for hiding this comment

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

@dmathieu since you are around, any idea of whether otelhttp will be marked stable any time soon?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@sfc-gh-bdrutu @bogdandrutu can you take a look now? I've made it more generic.

Copy link
Member

Choose a reason for hiding this comment

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

any idea of whether otelhttp will be marked stable any time soon?

That is not on our radar at the moment. However, we are committed to keeping the API stable, or providing deprecation warnings for at least two versions.


// ToServer creates an http.Server from settings object.
func (hss *ServerConfig) ToServer(_ context.Context, host component.Host, settings component.TelemetrySettings, handler http.Handler, opts ...ToServerOption) (*http.Server, error) {
internal.WarnOnUnspecifiedHost(settings.Logger, hss.Endpoint)
Expand Down Expand Up @@ -462,15 +471,17 @@ func (hss *ServerConfig) ToServer(_ context.Context, host component.Host, settin
otelOpts := []otelhttp.Option{
otelhttp.WithTracerProvider(settings.TracerProvider),
otelhttp.WithPropagators(otel.GetTextMapPropagator()),
otelhttp.WithSpanNameFormatter(func(_ string, r *http.Request) string {
otelhttp.WithSpanNameFormatter(func(operation string, r *http.Request) string {
if len(operation) > 0 {
return fmt.Sprintf("%s:%s", operation, r.URL.Path)
}
return r.URL.Path
}),
otelhttp.WithMeterProvider(settings.LeveledMeterProvider(configtelemetry.LevelDetailed)),
}

// Enable OpenTelemetry observability plugin.
// TODO: Consider to use component ID string as prefix for all the operations.
handler = otelhttp.NewHandler(handler, "", otelOpts...)
handler = otelhttp.NewHandler(handler, serverOpts.spanPrefix, otelOpts...)
Copy link
Member

@dmathieu dmathieu Oct 11, 2024

Choose a reason for hiding this comment

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

This parameter is not a span prefix. It's the operation name.
See https://pkg.go.dev/go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp#NewHandler
How about naming the option WithOperationName instead of WithSpanPrefix?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@dmathieu I understand your perspective, but we use spanPrefix specifically as a prefix for an operation in this context, using otelhttp.WithSpanNameFormatter

otelhttp.WithSpanNameFormatter(func(operation string, r *http.Request) string {
if len(operation) > 0 {
return fmt.Sprintf("%s:%s", operation, r.URL.Path)
}
return r.URL.Path
}),

That's why I named it WithSpanPrefix.


Using WithOperationName might suggest that the option is meant for naming the operation itself, which isn’t accurate. Instead, it serves as a prefix.

For eg.
For example, if you specify WithSpanPrefix("foo") and make an HTTP request to http://host:port/path/bar, the tracer would be named foo:/path/bar.

Does that help clarify?


// wrap the current handler in an interceptor that will add client.Info to the request's context
handler = &clientInfoHandler{
Expand Down
91 changes: 91 additions & 0 deletions config/confighttp/confighttp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/trace"
"go.opentelemetry.io/otel/trace/embedded"
"go.opentelemetry.io/otel/trace/noop"
"go.uber.org/zap"
"go.uber.org/zap/zaptest/observer"

Expand Down Expand Up @@ -1534,3 +1537,91 @@ func TestDefaultHTTPServerSettings(t *testing.T) {
assert.Equal(t, time.Duration(0), httpServerSettings.ReadTimeout)
assert.Equal(t, 1*time.Minute, httpServerSettings.ReadHeaderTimeout)
}

// TracerProvider is an OpenTelemetry No-Op TracerProvider.
type TracerProvider struct {
embedded.TracerProvider
ch chan string
}

// NewTracerProvider returns a TracerProvider that does not record any telemetry.
func NewTracerProvider(ch chan string) TracerProvider {
return TracerProvider{ch: ch}
}

// Tracer returns an OpenTelemetry Tracer that does not record any telemetry.
func (t TracerProvider) Tracer(string, ...trace.TracerOption) trace.Tracer {
return Tracer{ch: t.ch}
}

// Tracer is an OpenTelemetry No-Op Tracer.
type Tracer struct {
embedded.Tracer
ch chan string
}

type Span struct {
noop.Span
name string
}

func (t Tracer) Start(ctx context.Context, name string, _ ...trace.SpanStartOption) (context.Context, trace.Span) {
t.ch <- name
return ctx, Span{name: name}
}

func TestOperationPrefix(t *testing.T) {
tests := []struct {
name string
prefix string
expectedSpanName string
}{
{
name: "componentID prefix",
prefix: "otlphttpreceiver",
expectedSpanName: "otlphttpreceiver:/",
},
{
name: "empty prefix",
prefix: "",
expectedSpanName: "/",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
nameChan := make(chan string, 1)
set := componenttest.NewNopTelemetrySettings()
set.TracerProvider = TracerProvider{ch: nameChan}
logger, _ := observer.New(zap.DebugLevel)
set.Logger = zap.New(logger)
setting := &ServerConfig{
Endpoint: "localhost:0",
}

ln, err := setting.ToListener(context.Background())
require.NoError(t, err)
s, err := setting.ToServer(
context.Background(),
componenttest.NewNopHost(),
set,
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
}),
WithSpanPrefix(tt.prefix),
)
require.NoError(t, err)
go func() {
_ = s.Serve(ln)
}()
status, err := http.Get(fmt.Sprintf("http://%s", ln.Addr().String()))
require.NoError(t, err)
require.Equal(t, http.StatusOK, status.StatusCode)
require.NoError(t, s.Close())

spanName := <-nameChan
require.Equal(t, tt.expectedSpanName, spanName)
require.False(t, strings.HasPrefix(spanName, ":"))
})
}

}
2 changes: 1 addition & 1 deletion config/confighttp/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ require (
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.55.0
go.opentelemetry.io/otel v1.30.0
go.opentelemetry.io/otel/metric v1.30.0
go.opentelemetry.io/otel/trace v1.30.0
go.uber.org/goleak v1.3.0
go.uber.org/zap v1.27.0
golang.org/x/net v0.29.0
Expand All @@ -39,7 +40,6 @@ require (
go.opentelemetry.io/collector/pipeline v0.110.0 // indirect
go.opentelemetry.io/otel/sdk v1.30.0 // indirect
go.opentelemetry.io/otel/sdk/metric v1.30.0 // indirect
go.opentelemetry.io/otel/trace v1.30.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/text v0.18.0 // indirect
Expand Down
2 changes: 1 addition & 1 deletion receiver/otlpreceiver/otlp.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func (r *otlpReceiver) startHTTPServer(ctx context.Context, host component.Host)
}

var err error
if r.serverHTTP, err = r.cfg.HTTP.ToServer(ctx, host, r.settings.TelemetrySettings, httpMux, confighttp.WithErrorHandler(errorHandler)); err != nil {
if r.serverHTTP, err = r.cfg.HTTP.ToServer(ctx, host, r.settings.TelemetrySettings, httpMux, confighttp.WithErrorHandler(errorHandler), confighttp.WithSpanPrefix(r.settings.ID.String())); err != nil {
return err
}

Expand Down
Loading