From 157445b6c2dc8b3e2bc7cb417fc29d91c116b445 Mon Sep 17 00:00:00 2001 From: ale <ale@incal.net> Date: Fri, 25 Nov 2022 15:30:21 +0000 Subject: [PATCH] Export some float-specific environment variables in traces --- tracing/tracing.go | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/tracing/tracing.go b/tracing/tracing.go index 1f77294..1becbf1 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 } -- GitLab