Skip to content

Commit

Permalink
[v10] Fix tracing exporter endpoints (#14109)
Browse files Browse the repository at this point in the history
* Fix tracing exporter endpoints

Ensure that the endpoint provided to the trace clients
are correct even if the configuration doesn't include
the scheme. Prior to this the endpoint always attempted
to remove the scheme prefix, even when one wasn't provided.
Doing so led to the hostname to be altered which caused
some unknown host issues.

This also removes the process and process owner detector
from the tracing resource. Running within a container might
not have a username mapped to the uid which was preventing
tracing from being initialized.
  • Loading branch information
rosstimothy authored Jul 8, 2022
1 parent ad63528 commit 8f477d4
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 4 deletions.
15 changes: 11 additions & 4 deletions lib/observability/tracing/tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"errors"
"net"
"net/url"
"strings"
"time"

"github.com/gravitational/teleport"
Expand Down Expand Up @@ -124,6 +125,14 @@ func (c *Config) CheckAndSetDefaults() error {
return nil
}

func (c *Config) Endpoint() string {
if !strings.HasPrefix(c.ExporterURL, c.exporterURL.Scheme) {
return c.ExporterURL
}

return c.ExporterURL[len(c.exporterURL.Scheme)+3:]
}

var _ otlptrace.Client = (*noopClient)(nil)

type noopClient struct{}
Expand Down Expand Up @@ -195,10 +204,10 @@ func NewClient(cfg Config) (otlptrace.Client, error) {
var traceClient otlptrace.Client
switch cfg.exporterURL.Scheme {
case "http", "https":
httpOptions = append(httpOptions, otlptracehttp.WithEndpoint(cfg.ExporterURL[len(cfg.exporterURL.Scheme)+3:]))
httpOptions = append(httpOptions, otlptracehttp.WithEndpoint(cfg.Endpoint()))
traceClient = otlptracehttp.NewClient(httpOptions...)
case "grpc":
grpcOptions = append(grpcOptions, otlptracegrpc.WithEndpoint(cfg.ExporterURL[len(cfg.exporterURL.Scheme)+3:]))
grpcOptions = append(grpcOptions, otlptracegrpc.WithEndpoint(cfg.Endpoint()))
traceClient = otlptracegrpc.NewClient(grpcOptions...)
default:
return nil, trace.BadParameter("unsupported exporter scheme: %q", cfg.exporterURL.Scheme)
Expand Down Expand Up @@ -308,11 +317,9 @@ func NewTraceProvider(ctx context.Context, cfg Config) (*Provider, error) {

res, err := resource.New(ctx,
resource.WithFromEnv(),
resource.WithProcess(),
resource.WithProcessPID(),
resource.WithProcessExecutableName(),
resource.WithProcessExecutablePath(),
resource.WithProcessOwner(),
resource.WithProcessRuntimeName(),
resource.WithProcessRuntimeVersion(),
resource.WithProcessRuntimeDescription(),
Expand Down
52 changes: 52 additions & 0 deletions lib/observability/tracing/tracing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,3 +493,55 @@ func TestConfig_CheckAndSetDefaults(t *testing.T) {
})
}
}

func TestConfig_Endpoint(t *testing.T) {
cases := []struct {
name string
cfg Config
expected string
}{
{
name: "with http scheme",
cfg: Config{
Service: "test",
ExporterURL: "http://localhost:8080",
},
expected: "localhost:8080",
},
{
name: "with https scheme",
cfg: Config{
Service: "test",
ExporterURL: "https://localhost:8080/custom",
},
expected: "localhost:8080/custom",
},
{
name: "with grpc scheme",
cfg: Config{
Service: "test",
ExporterURL: "grpc://collector.opentelemetry.svc:4317",
SamplingRate: 1.0,
DialTimeout: time.Millisecond,
},
expected: "collector.opentelemetry.svc:4317",
},
{
name: "without a scheme",
cfg: Config{
Service: "test",
ExporterURL: "collector.opentelemetry.svc:4317",
SamplingRate: 1.0,
DialTimeout: time.Millisecond,
},
expected: "collector.opentelemetry.svc:4317",
},
}

for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
require.NoError(t, tt.cfg.CheckAndSetDefaults())
require.Equal(t, tt.expected, tt.cfg.Endpoint())
})
}
}

0 comments on commit 8f477d4

Please sign in to comment.