Skip to content

Commit

Permalink
Support writing traces to a directory, wire it up in kubetest2
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
justinsb committed Nov 10, 2023
1 parent 6987954 commit 2586155
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 5 deletions.
41 changes: 36 additions & 5 deletions cmd/kops/otel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
23 changes: 23 additions & 0 deletions tests/e2e/kubetest2-kops/deployer/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 2586155

Please sign in to comment.