mirror of
https://github.com/hanzoai/telemetry.git
synced 2026-10-30 20:01:49 +00:00
#### Description Embed `confignet.AddrConfig` in `confighttp.ServerConfig`, replacing the `Endpoint` field. This introduces the "transport" config attribute, which can be set to "unix" for binding a Unix domain socket. #### Link to tracking issue Fixes #8752 Fixes #14187 #### Testing ```bash $ ./bin/otelcorecol_linux_amd64 \ --config examples/local/otel-config.yaml \ --set receivers::otlp::protocols::http::transport=unix \ --set receivers::otlp::protocols::http::endpoint=\"@otlphttp\" ``` ```bash $ curl --abstract-unix-socket otlphttp http://testing.invalid:1234/v1/traces 405 method not allowed, supported: [POST] ``` #### Documentation Updated README.md --------- Co-authored-by: Antoine Toulme <[email protected]>
77 lines
2.1 KiB
Go
77 lines
2.1 KiB
Go
// Copyright The OpenTelemetry Authors
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package xconfighttp
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
|
|
sdktrace "go.opentelemetry.io/otel/sdk/trace"
|
|
"go.opentelemetry.io/otel/sdk/trace/tracetest"
|
|
"go.opentelemetry.io/otel/trace"
|
|
|
|
"go.opentelemetry.io/collector/component/componenttest"
|
|
"go.opentelemetry.io/collector/config/confighttp"
|
|
"go.opentelemetry.io/collector/config/confignet"
|
|
)
|
|
|
|
func TestServerWithOtelHTTPOptions(t *testing.T) {
|
|
// prepare
|
|
sc := confighttp.ServerConfig{
|
|
NetAddr: confignet.AddrConfig{
|
|
Endpoint: "localhost:0",
|
|
Transport: confignet.TransportTypeTCP,
|
|
},
|
|
}
|
|
|
|
telemetry := componenttest.NewNopTelemetrySettings()
|
|
tp, te := tracerProvider(t)
|
|
telemetry.TracerProvider = tp
|
|
|
|
srv, err := sc.ToServer(
|
|
context.Background(),
|
|
nil,
|
|
telemetry,
|
|
http.HandlerFunc(func(http.ResponseWriter, *http.Request) {}),
|
|
WithOtelHTTPOptions(
|
|
otelhttp.WithSpanNameFormatter(func(_ string, r *http.Request) string {
|
|
return "example" + r.URL.Path
|
|
}),
|
|
otelhttp.WithFilter(func(r *http.Request) bool {
|
|
return r.URL.Path != "/foobar"
|
|
}),
|
|
),
|
|
)
|
|
require.NoError(t, err)
|
|
|
|
for _, path := range []string{"/path", "/foobar"} {
|
|
response := &httptest.ResponseRecorder{}
|
|
req, err := http.NewRequest(http.MethodGet, srv.Addr+path, http.NoBody)
|
|
require.NoError(t, err)
|
|
srv.Handler.ServeHTTP(response, req)
|
|
assert.Equal(t, http.StatusOK, response.Result().StatusCode)
|
|
}
|
|
|
|
spans := te.GetSpans().Snapshots()
|
|
assert.Len(t, spans, 1, "the request to /foobar should not be traced")
|
|
assert.Equal(t, "example/path", spans[0].Name())
|
|
}
|
|
|
|
func tracerProvider(t *testing.T) (trace.TracerProvider, *tracetest.InMemoryExporter) {
|
|
exporter := tracetest.NewInMemoryExporter()
|
|
tp := sdktrace.NewTracerProvider(
|
|
sdktrace.WithSampler(sdktrace.AlwaysSample()),
|
|
sdktrace.WithSyncer(exporter),
|
|
)
|
|
t.Cleanup(func() {
|
|
assert.NoError(t, tp.Shutdown(context.Background()))
|
|
})
|
|
return tp, exporter
|
|
}
|