Skip to content

Commit

Permalink
K8s env vars (#1279)
Browse files Browse the repository at this point in the history
  • Loading branch information
grcevski authored Oct 29, 2024
1 parent ef57788 commit 0b26729
Show file tree
Hide file tree
Showing 13 changed files with 465 additions and 69 deletions.
26 changes: 26 additions & 0 deletions pkg/export/attributes/env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package attributes

import "strings"

type VarHandler func(k string, v string)

func ParseOTELResourceVariable(envVar string, handler VarHandler) {
// split all the comma-separated key=value entries
for _, entry := range strings.Split(envVar, ",") {
// split only by the first '=' appearance, as values might
// have base64 '=' padding symbols
keyVal := strings.SplitN(entry, "=", 2)
if len(keyVal) < 2 {
continue
}

k := strings.TrimSpace(keyVal[0])
v := strings.TrimSpace(keyVal[1])

if k == "" || v == "" {
continue
}

handler(strings.TrimSpace(keyVal[0]), strings.TrimSpace(keyVal[1]))
}
}
24 changes: 22 additions & 2 deletions pkg/export/debug/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,26 @@ func textPrinter(input <-chan []request.Span) {
for spans := range input {
for i := range spans {
t := spans[i].Timings()

pn := ""
hn := ""

if spans[i].IsClientSpan() {
if spans[i].ServiceID.Namespace != "" {
pn = "." + spans[i].ServiceID.Namespace
}
if spans[i].OtherNamespace != "" {
hn = "." + spans[i].OtherNamespace
}
} else {
if spans[i].OtherNamespace != "" {
pn = "." + spans[i].OtherNamespace
}
if spans[i].ServiceID.Namespace != "" {
hn = "." + spans[i].ServiceID.Namespace
}
}

fmt.Printf("%s (%s[%s]) %s %v %s %s [%s:%d]->[%s:%d] size:%dB svc=[%s %s] traceparent=[%s]\n",
t.Start.Format("2006-01-02 15:04:05.12345"),
t.End.Sub(t.RequestStart),
Expand All @@ -86,9 +106,9 @@ func textPrinter(input <-chan []request.Span) {
spans[i].Status,
spans[i].Method,
spans[i].Path,
spans[i].Peer+" as "+spans[i].PeerName,
spans[i].Peer+" as "+spans[i].PeerName+pn,
spans[i].PeerPort,
spans[i].Host+" as "+spans[i].HostName,
spans[i].Host+" as "+spans[i].HostName+hn,
spans[i].HostPort,
spans[i].ContentLength,
&spans[i].ServiceID,
Expand Down
4 changes: 2 additions & 2 deletions pkg/export/debug/debug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ func traceFuncHelper(t *testing.T, tracePrinter TracePrinter) string {
}

func TestTracePrinterResolve_PrinterText(t *testing.T) {
expected := "(25µs[20µs]) HTTP 200 method path [peer as peername:1234]->" +
"[host as hostname:5678] size:1024B svc=[foo/bar go]" +
expected := "(25µs[20µs]) HTTP 200 method path [peer as peername.otherns:1234]->" +
"[host as hostname.foo:5678] size:1024B svc=[foo/bar go]" +
" traceparent=[00-01020300000000000000000000000000-0102030000000000[0102040000000000]-01]\n"

actual := traceFuncHelper(t, TracePrinterText)
Expand Down
25 changes: 3 additions & 22 deletions pkg/export/otel/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"log/slog"
"os"
"strings"
"time"

"github.com/go-logr/logr"
Expand All @@ -20,6 +19,7 @@ import (
semconv "go.opentelemetry.io/otel/semconv/v1.19.0"
"google.golang.org/grpc/credentials"

"github.com/grafana/beyla/pkg/export/attributes"
"github.com/grafana/beyla/pkg/export/expire"
"github.com/grafana/beyla/pkg/internal/svc"
)
Expand Down Expand Up @@ -345,14 +345,12 @@ func headersFromEnv(varName string) map[string]string {
return headers
}

type varHandler func(k string, v string)

// parseOTELEnvVar parses a comma separated group of variables
// in the format specified by OTEL_EXPORTER_OTLP_*HEADERS or
// OTEL_RESOURCE_ATTRIBUTES, i.e. a comma-separated list of
// key=values. For example: api-key=key,other-config-value=value
// The values are passed as parameters to the handler function
func parseOTELEnvVar(svc *svc.ID, varName string, handler varHandler) {
func parseOTELEnvVar(svc *svc.ID, varName string, handler attributes.VarHandler) {
var envVar string
ok := false

Expand All @@ -368,24 +366,7 @@ func parseOTELEnvVar(svc *svc.ID, varName string, handler varHandler) {
return
}

// split all the comma-separated key=value entries
for _, entry := range strings.Split(envVar, ",") {
// split only by the first '=' appearance, as values might
// have base64 '=' padding symbols
keyVal := strings.SplitN(entry, "=", 2)
if len(keyVal) < 2 {
continue
}

k := strings.TrimSpace(keyVal[0])
v := strings.TrimSpace(keyVal[1])

if k == "" || v == "" {
continue
}

handler(strings.TrimSpace(keyVal[0]), strings.TrimSpace(keyVal[1]))
}
attributes.ParseOTELResourceVariable(envVar, handler)
}

func ResourceAttrsFromEnv(svc *svc.ID) []attribute.KeyValue {
Expand Down
3 changes: 2 additions & 1 deletion pkg/internal/discover/watcher_kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ func (wk *watcherKubeEnricher) enrichPodEvent(podEvent Event[*informer.ObjectMet
switch podEvent.Type {
case EventCreated:
wk.log.Debug("Pod added",
"namespace", podEvent.Obj.Namespace, "name", podEvent.Obj.Name)
"namespace", podEvent.Obj.Namespace, "name", podEvent.Obj.Name,
"containers", podEvent.Obj.Pod.Containers)
if events := wk.onNewPod(podEvent.Obj); len(events) > 0 {
out <- events
}
Expand Down
16 changes: 15 additions & 1 deletion pkg/internal/exec/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"
"syscall"

"github.com/grafana/beyla/pkg/export/attributes"
"github.com/grafana/beyla/pkg/internal/svc"
"github.com/grafana/beyla/pkg/services"
)
Expand All @@ -25,7 +26,9 @@ type FileInfo struct {
}

const (
envServiceName = "OTEL_SERVICE_NAME"
envServiceName = "OTEL_SERVICE_NAME"
envResourceAttrs = "OTEL_RESOURCE_ATTRIBUTES"
serviceNameKey = "service.name"
)

func (fi *FileInfo) ExecutableName() string {
Expand Down Expand Up @@ -73,6 +76,17 @@ func FindExecELF(p *services.ProcessInfo, svcID svc.ID) (*FileInfo, error) {
file.Service.EnvVars = envVars
if svcName, ok := file.Service.EnvVars[envServiceName]; ok {
file.Service.Name = svcName
} else {
if resourceAttrs, ok := file.Service.EnvVars[envResourceAttrs]; ok {
allVars := map[string]string{}
collect := func(k string, v string) {
allVars[k] = v
}
attributes.ParseOTELResourceVariable(resourceAttrs, collect)
if result, ok := allVars[serviceNameKey]; ok {
file.Service.Name = result
}
}
}

return &file, nil
Expand Down
Loading

0 comments on commit 0b26729

Please sign in to comment.