From 2586155b436b2d2472eb1af237071c270eab9051 Mon Sep 17 00:00:00 2001 From: justinsb Date: Fri, 10 Nov 2023 09:34:35 -0500 Subject: [PATCH] Support writing traces to a directory, wire it up in kubetest2 If given a directory, we can construct a reasonable name based on the executable name, pid and timestamp. Then this is relatively easy to wire up from kubetest2, if we have an artifacts directory. --- cmd/kops/otel.go | 41 ++++++++++++++++++--- tests/e2e/kubetest2-kops/deployer/common.go | 23 ++++++++++++ 2 files changed, 59 insertions(+), 5 deletions(-) diff --git a/cmd/kops/otel.go b/cmd/kops/otel.go index 138b3f1f90358..295916c37f6a3 100644 --- a/cmd/kops/otel.go +++ b/cmd/kops/otel.go @@ -19,8 +19,11 @@ package main import ( "context" "errors" + "fmt" "net/http" "os" + "path/filepath" + "strings" "time" "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp" @@ -88,15 +91,43 @@ func newResource(serviceName, serviceVersion string) (*resource.Resource, error) } func newTraceProvider(ctx context.Context, res *resource.Resource) (*trace.TracerProvider, error) { - s := os.Getenv("OTEL_EXPORTER_OTLP_TRACES_FILE") - if s == "" { - s = os.Getenv("OTEL_EXPORTER_OTLP_FILE") + destIsDirectory := false + + dest := os.Getenv("OTEL_EXPORTER_OTLP_TRACES_FILE") + if dest == "" { + dest = os.Getenv("OTEL_EXPORTER_OTLP_FILE") + } + if dest == "" { + dest = os.Getenv("OTEL_EXPORTER_OTLP_TRACES_DIR") + if dest != "" { + destIsDirectory = true + } + } + if dest == "" { + dest = os.Getenv("OTEL_EXPORTER_OTLP_DIR") + if dest != "" { + destIsDirectory = true + } } - if s == "" { + if dest == "" { return nil, nil } - traceExporter, err := otlptracefile.New(ctx, otlptracefile.WithPath(s)) + // If we are writing to a directory, construct a (likely) unique name + if destIsDirectory { + processName, err := os.Executable() + if err != nil { + return nil, fmt.Errorf("getting process name: %w", err) + } + processName = filepath.Base(processName) + processName = strings.TrimSuffix(processName, ".exe") + pid := os.Getpid() + timestamp := time.Now().UTC().Format(time.RFC3339) + filename := fmt.Sprintf("%s-%d-%s.otel", processName, pid, timestamp) + dest = filepath.Join(dest, filename) + } + + traceExporter, err := otlptracefile.New(ctx, otlptracefile.WithPath(dest)) if err != nil { return nil, err } diff --git a/tests/e2e/kubetest2-kops/deployer/common.go b/tests/e2e/kubetest2-kops/deployer/common.go index 5135b53a6b78b..82f5d687f8b79 100644 --- a/tests/e2e/kubetest2-kops/deployer/common.go +++ b/tests/e2e/kubetest2-kops/deployer/common.go @@ -222,6 +222,29 @@ func (d *deployer) env() []string { } else if baseURL := os.Getenv("KOPS_BASE_URL"); baseURL != "" { vars = append(vars, fmt.Sprintf("KOPS_BASE_URL=%v", os.Getenv("KOPS_BASE_URL"))) } + + // Pass through OpenTelemetry flags + { + foundOTEL := false + for _, k := range []string{ + "OTEL_EXPORTER_OTLP_TRACES_FILE", "OTEL_EXPORTER_OTLP_FILE", + "OTEL_EXPORTER_OTLP_TRACES_DIR", "OTEL_EXPORTER_OTLP_DIR", + } { + v := os.Getenv(k) + if v != "" { + foundOTEL = true + vars = append(vars, k+"="+v) + } + } + + // If no otel flags were explicitly specified, and we have artifacts, log under the artifacts directory + if !foundOTEL { + artifacts := d.ArtifactsDir + if artifacts != "" { + vars = append(vars, "OTEL_EXPORTER_OTLP_TRACES_DIR="+filepath.Join(artifacts, "otlp")) + } + } + } return vars }