Skip to content

Commit

Permalink
tracer: Parse global labels per tracer (#1290)
Browse files Browse the repository at this point in the history
This patch removes the `globalLabel` variable and one-time parsing of
the global labels, in favor of parsing the global labels individually
on tracer construction. This has the advantage that tracers that are
created after start-up will parse environment variables at that time
and not only when the `apm` package is imported.

Signed-off-by: Marc Lopez Rubio <[email protected]>
  • Loading branch information
marclop authored Aug 10, 2022
1 parent cdc3bca commit b8542dc
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 18 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ endif::[]
https://github.com/elastic/apm-agent-go/compare/v2.1.0...main[View commits]
- Global labels are now parsed when the tracer is constructed, instead of parsing only once on package initialization {pull}1290[#(1290)]
[[release-notes-2.x]]
=== Go Agent version 2.x
Expand Down
16 changes: 0 additions & 16 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import (
"go.elastic.co/apm/v2/internal/apmlog"
"go.elastic.co/apm/v2/internal/configutil"
"go.elastic.co/apm/v2/internal/wildcard"
"go.elastic.co/apm/v2/model"
"go.elastic.co/apm/v2/transport"
)

Expand Down Expand Up @@ -125,21 +124,6 @@ var (
"*auth*",
"set-cookie",
}, ","))

globalLabels = func() model.StringMap {
var labels model.StringMap
for _, kv := range configutil.ParseListEnv(envGlobalLabels, ",", nil) {
i := strings.IndexRune(kv, '=')
if i > 0 {
k, v := strings.TrimSpace(kv[:i]), strings.TrimSpace(kv[i+1:])
labels = append(labels, model.StringMapItem{
Key: cleanLabelKey(k),
Value: truncateString(v),
})
}
}
return labels
}()
)

// Regular expression matching comment characters to escape in the User-Agent header value.
Expand Down
24 changes: 22 additions & 2 deletions tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"io"
"log"
"math/rand"
"strings"
"sync"
"sync/atomic"
"time"
Expand Down Expand Up @@ -159,6 +160,7 @@ type TracerOptions struct {
heapProfileInterval time.Duration
exitSpanMinDuration time.Duration
compressionOptions compressionOptions
globalLabels model.StringMap
}

// initDefaults updates opts with default values.
Expand Down Expand Up @@ -325,6 +327,7 @@ func (opts *TracerOptions) initDefaults(continueOnError bool) error {
log.Printf("[apm]: %s", err)
}

opts.globalLabels = parseGlobalLabels()
opts.requestDuration = requestDuration
opts.metricsInterval = metricsInterval
opts.requestSize = requestSize
Expand Down Expand Up @@ -407,6 +410,7 @@ type Tracer struct {
breakdownMetrics *breakdownMetrics
profileSender profileSender
versionGetter majorVersionGetter
globalLabels model.StringMap

// stats is heap-allocated to ensure correct alignment for atomic access.
stats *TracerStats
Expand Down Expand Up @@ -469,6 +473,7 @@ func newTracer(opts TracerOptions) *Tracer {
instrumentationConfigInternal: &instrumentationConfig{
local: make(map[string]func(*instrumentationConfigValues)),
},
globalLabels: opts.globalLabels,
}
t.breakdownMetrics.enabled = opts.breakdownMetrics
// Initialise local transaction config.
Expand Down Expand Up @@ -1342,9 +1347,9 @@ func (t *Tracer) encodeRequestMetadata(json *fastjson.Writer) {
json.RawString(`,"cloud":`)
cloud.MarshalFastJSON(json)
}
if len(globalLabels) > 0 {
if len(t.globalLabels) > 0 {
json.RawString(`,"labels":`)
globalLabels.MarshalFastJSON(json)
t.globalLabels.MarshalFastJSON(json)
}
json.RawByte('}')
}
Expand Down Expand Up @@ -1453,3 +1458,18 @@ type majorVersionGetter interface {
// cache is stale.
MajorServerVersion(ctx context.Context, refreshStale bool) uint32
}

func parseGlobalLabels() model.StringMap {
var labels model.StringMap
for _, kv := range configutil.ParseListEnv(envGlobalLabels, ",", nil) {
i := strings.IndexRune(kv, '=')
if i > 0 {
k, v := strings.TrimSpace(kv[:i]), strings.TrimSpace(kv[i+1:])
labels = append(labels, model.StringMapItem{
Key: cleanLabelKey(k),
Value: truncateString(v),
})
}
}
return labels
}

0 comments on commit b8542dc

Please sign in to comment.