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.
This commit is contained in:
justinsb 2023-11-10 09:34:35 -05:00
parent 3269f07905
commit e0622ba3a2
2 changed files with 62 additions and 5 deletions

View File

@ -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,46 @@ 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 s == "" {
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 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 {
if err := os.MkdirAll(dest, 0755); err != nil {
return nil, fmt.Errorf("creating directories %q: %w", dest, err)
}
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
}

View File

@ -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
}