Skip to content

Commit

Permalink
Merge pull request #71 from coroot/tracing_service_name_from_containe…
Browse files Browse the repository at this point in the history
…r_id

tracing: retrieve `ServiceName` from `ContainerId`
  • Loading branch information
def committed Feb 28, 2024
2 parents 3eff0c6 + 341cef0 commit ec3a19a
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions tracing/tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/coroot/coroot-node-agent/common"
"github.com/coroot/coroot-node-agent/ebpftracer/l7"
"github.com/coroot/coroot-node-agent/flags"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace"
Expand All @@ -26,7 +25,7 @@ const (
)

var (
tracer trace.Tracer
tracer func(containerId string) trace.Tracer
)

func Init(machineId, hostname, version string) {
Expand All @@ -53,17 +52,22 @@ func Init(machineId, hostname, version string) {
if err != nil {
klog.Exitln(err)
}
tracerProvider := sdktrace.NewTracerProvider(
sdktrace.WithBatcher(exporter),
sdktrace.WithResource(resource.NewWithAttributes(
semconv.SchemaURL,
semconv.ServiceName("coroot-node-agent"),
semconv.HostName(hostname),
semconv.HostID(machineId),
)),
)
otel.SetTracerProvider(tracerProvider)
tracer = tracerProvider.Tracer("coroot-node-agent", trace.WithInstrumentationVersion(version))

batcher := sdktrace.WithBatcher(exporter)

tracer = func(containerId string) trace.Tracer {
provider := sdktrace.NewTracerProvider(
batcher,
sdktrace.WithResource(resource.NewWithAttributes(
semconv.SchemaURL,
semconv.HostName(hostname),
semconv.HostID(machineId),
semconv.ServiceName(common.ContainerIdToOtelServiceName(containerId)),
semconv.ContainerID(containerId),
)),
)
return provider.Tracer("coroot-node-agent", trace.WithInstrumentationVersion(version))
}
}

type Trace struct {
Expand All @@ -77,7 +81,6 @@ func NewTrace(containerId string, destination netaddr.IPPort) *Trace {
return nil
}
return &Trace{containerId: containerId, destination: destination, commonAttrs: []attribute.KeyValue{
semconv.ContainerID(containerId),
semconv.NetPeerName(destination.IP().String()),
semconv.NetPeerPort(int(destination.Port())),
}}
Expand All @@ -86,7 +89,7 @@ func NewTrace(containerId string, destination netaddr.IPPort) *Trace {
func (t *Trace) createSpan(name string, duration time.Duration, error bool, attrs ...attribute.KeyValue) {
end := time.Now()
start := end.Add(-duration)
_, span := tracer.Start(nil, name, trace.WithTimestamp(start), trace.WithSpanKind(trace.SpanKindClient))
_, span := tracer(t.containerId).Start(nil, name, trace.WithTimestamp(start), trace.WithSpanKind(trace.SpanKindClient))
span.SetAttributes(attrs...)
span.SetAttributes(t.commonAttrs...)
if error {
Expand Down

0 comments on commit ec3a19a

Please sign in to comment.