Skip to content

Commit

Permalink
set APM global labels as a map
Browse files Browse the repository at this point in the history
  • Loading branch information
pchila committed Sep 1, 2023
1 parent 3175a13 commit 88eb548
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 7 deletions.
12 changes: 6 additions & 6 deletions internal/pkg/core/monitoring/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ func DefaultConfig() *MonitoringConfig {

// APMConfig configures APM Tracing.
type APMConfig struct {
Environment string `config:"environment"`
APIKey string `config:"api_key"`
SecretToken string `config:"secret_token"`
Hosts []string `config:"hosts"`
GlobalLabels string `config:"global_labels"`
TLS APMTLS `config:"tls"`
Environment string `config:"environment"`
APIKey string `config:"api_key"`
SecretToken string `config:"secret_token"`
Hosts []string `config:"hosts"`
GlobalLabels map[string]string `config:"global_labels"`
TLS APMTLS `config:"tls"`
}

// APMTLS contains the configuration options necessary for configuring TLS in
Expand Down
33 changes: 32 additions & 1 deletion pkg/component/runtime/apm_config_mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
package runtime

import (
"sort"
"strings"

"github.com/elastic/elastic-agent-client/v7/pkg/proto"

"github.com/elastic/elastic-agent/internal/pkg/core/monitoring/config"
Expand All @@ -23,7 +26,7 @@ func MapAPMConfig(conf *config.APMConfig) *proto.APMConfig {
ApiKey: conf.APIKey,
SecretToken: conf.SecretToken,
Hosts: append([]string{}, conf.Hosts...),
GlobalLabels: conf.GlobalLabels,
GlobalLabels: buildGlobalLabelsString(conf.GlobalLabels),
}

if conf.TLS != zeroElasticAPMTLS {
Expand All @@ -37,3 +40,31 @@ func MapAPMConfig(conf *config.APMConfig) *proto.APMConfig {

return &proto.APMConfig{Elastic: elasticAPMConf}
}

func buildGlobalLabelsString(labels map[string]string) string {
const separator = ","

if len(labels) == 0 {
return ""
}

//prepare sorted keys to make output deterministic
keys := make([]string, 0, len(labels))
for k := range labels {
keys = append(keys, k)
}

sort.Strings(keys)

// create the key=value string
buf := new(strings.Builder)
for _, k := range keys {
if buf.Len() > 0 {
buf.WriteString(separator)
}
buf.WriteString(k)
buf.WriteString("=")
buf.WriteString(labels[k])
}
return buf.String()
}
126 changes: 126 additions & 0 deletions pkg/component/runtime/apm_config_mapper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package runtime

import (
"testing"

"github.com/elastic/elastic-agent-client/v7/pkg/proto"
"github.com/stretchr/testify/assert"

"github.com/elastic/elastic-agent/internal/pkg/core/monitoring/config"
)

func TestMapAPMConfig(t *testing.T) {
type args struct {
conf *config.APMConfig
}
tests := []struct {
name string
args args
want *proto.APMConfig
}{
{
name: "nil config",
args: args{
conf: nil,
},
want: nil,
},
{
name: "full config",
args: args{
conf: &config.APMConfig{
Environment: "environment",
APIKey: "apikey",
SecretToken: "secrettoken",
Hosts: []string{"host1", "host2"},
GlobalLabels: map[string]string{"k1": "v1", "k2": "v2"},
TLS: config.APMTLS{
SkipVerify: true,
ServerCertificate: "servercertificate",
ServerCA: "serverca",
},
},
},
want: &proto.APMConfig{
Elastic: &proto.ElasticAPM{
Tls: &proto.ElasticAPMTLS{
SkipVerify: true,
ServerCert: "servercertificate",
ServerCa: "serverca",
},
Environment: "environment",
ApiKey: "apikey",
SecretToken: "secrettoken",
Hosts: []string{"host1", "host2"},
GlobalLabels: "k1=v1,k2=v2",
},
},
},
{
name: "config without global labels",
args: args{
conf: &config.APMConfig{
Environment: "environment",
APIKey: "apikey",
SecretToken: "secrettoken",
Hosts: []string{"host1", "host2"},
GlobalLabels: nil,
TLS: config.APMTLS{
SkipVerify: true,
ServerCertificate: "servercertificate",
ServerCA: "serverca",
},
},
},
want: &proto.APMConfig{
Elastic: &proto.ElasticAPM{
Tls: &proto.ElasticAPMTLS{
SkipVerify: true,
ServerCert: "servercertificate",
ServerCa: "serverca",
},
Environment: "environment",
ApiKey: "apikey",
SecretToken: "secrettoken",
Hosts: []string{"host1", "host2"},
GlobalLabels: "",
},
},
},
{
name: "config without hosts",
args: args{
conf: &config.APMConfig{
Environment: "environment",
APIKey: "apikey",
SecretToken: "secrettoken",
GlobalLabels: map[string]string{"k1": "v1", "k2": "v2"},
TLS: config.APMTLS{
SkipVerify: true,
ServerCertificate: "servercertificate",
ServerCA: "serverca",
},
},
},
want: &proto.APMConfig{
Elastic: &proto.ElasticAPM{
Tls: &proto.ElasticAPMTLS{
SkipVerify: true,
ServerCert: "servercertificate",
ServerCa: "serverca",
},
Environment: "environment",
ApiKey: "apikey",
SecretToken: "secrettoken",
Hosts: []string{},
GlobalLabels: "k1=v1,k2=v2",
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, MapAPMConfig(tt.args.conf), "MapAPMConfig(%v)", tt.args.conf)
})
}
}

0 comments on commit 88eb548

Please sign in to comment.