diff --git a/tracing/tracing.go b/tracing/tracing.go index 1f77294b3983e475d32b65fa9a58d6bfea2cae53..1becbf10bfec942c0fc89efc777c2e9bd9305fcd 100644 --- a/tracing/tracing.go +++ b/tracing/tracing.go @@ -9,11 +9,13 @@ import ( "os" "path/filepath" "strconv" + "strings" "sync" othttp "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp" b3 "go.opentelemetry.io/contrib/propagators/b3" "go.opentelemetry.io/otel" + "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/exporters/zipkin" "go.opentelemetry.io/otel/propagation" "go.opentelemetry.io/otel/sdk/resource" @@ -77,14 +79,35 @@ func getServiceName() string { } func defaultResource(serviceName string) *resource.Resource { - hostname, _ := os.Hostname() + attrs := []attribute.KeyValue{ + semconv.ServiceNameKey.String(serviceName), + } + + if s, err := os.Hostname(); err == nil { + attrs = append(attrs, semconv.HostNameKey.String(s)) + } + if s := os.Getenv("FLOAT_SERVICE"); s != "" { + attrs = append(attrs, attribute.Key("float.service").String(s)) + } + if s := os.Getenv("FLOAT_CONTAINER_NAME"); s != "" { + attrs = append(attrs, semconv.ContainerNameKey.String(s)) + } + if s := os.Getenv("FLOAT_CONTAINER_IMAGE"); s != "" { + imageName := s + tag := "latest" + if strings.Contains(imageName, ":") { + parts := strings.SplitN(imageName, ":", 2) + imageName = parts[0] + tag = parts[1] + } + + attrs = append(attrs, semconv.ContainerImageNameKey.String(imageName)) + attrs = append(attrs, semconv.ContainerImageTagKey.String(tag)) + } + r, _ := resource.Merge( resource.Default(), - resource.NewWithAttributes( - semconv.SchemaURL, - semconv.ServiceNameKey.String(serviceName), - semconv.HostNameKey.String(hostname), - ), + resource.NewWithAttributes(semconv.SchemaURL, attrs...), ) return r }