From 101883c51793db2e13da91ea1777f406fd227cfb Mon Sep 17 00:00:00 2001 From: Mario Rodriguez Date: Fri, 26 Nov 2021 11:27:01 +0100 Subject: [PATCH 01/12] Improve k8s pod association --- .../promsdprocessor/prom_sd_processor.go | 47 +++++++++++++++---- .../promsdprocessor/prom_sd_processor_test.go | 3 +- 2 files changed, 40 insertions(+), 10 deletions(-) diff --git a/pkg/traces/promsdprocessor/prom_sd_processor.go b/pkg/traces/promsdprocessor/prom_sd_processor.go index d60e11784aea..635649168245 100644 --- a/pkg/traces/promsdprocessor/prom_sd_processor.go +++ b/pkg/traces/promsdprocessor/prom_sd_processor.go @@ -16,6 +16,7 @@ import ( "github.com/prometheus/prometheus/discovery/targetgroup" "github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/pkg/relabel" + "go.opentelemetry.io/collector/client" "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/component/componenterror" "go.opentelemetry.io/collector/consumer" @@ -23,6 +24,11 @@ import ( semconv "go.opentelemetry.io/collector/model/semconv/v1.6.1" ) +const ( + ipTagName = "ip" + k8sIPTagName = "k8s.host.ip" +) + type promServiceDiscoProcessor struct { nextConsumer consumer.Traces discoveryMgr *discovery.Manager @@ -87,30 +93,53 @@ func (p *promServiceDiscoProcessor) ConsumeTraces(ctx context.Context, td pdata. for i := 0; i < rss.Len(); i++ { rs := rss.At(i) - p.processAttributes(rs.Resource().Attributes()) + p.processAttributes(ctx, rs.Resource().Attributes()) } return p.nextConsumer.ConsumeTraces(ctx, td) } -func (p *promServiceDiscoProcessor) processAttributes(attrs pdata.AttributeMap) { +func stringAttributeFromMap(attrs pdata.AttributeMap, key string) string { + if attr, ok := attrs.Get(key); ok { + if attr.Type() == pdata.AttributeValueTypeString { + return attr.StringVal() + } + } + return "" +} + +func getConnectionIP(ctx context.Context) string { + if c, ok := client.FromContext(ctx); ok { + return c.IP + } + return "" +} + +func getPODIP(ctx context.Context, attrs pdata.AttributeMap) string { // find the ip ipTagNames := []string{ - "ip", // jaeger/opentracing? default + ipTagName, // jaeger/opentracing? default semconv.AttributeNetHostIP, // otel semantics for host ip + k8sIPTagName, // k8s semantics for host ip } - var ip string for _, name := range ipTagNames { - val, ok := attrs.Get(name) - if !ok { - continue + ip := stringAttributeFromMap(attrs, name) + if ip != "" { + return ip } + } - ip = val.StringVal() - break + hostname := stringAttributeFromMap(attrs, semconv.AttributeHostName) + if net.ParseIP(hostname) != nil { + return hostname } + return getConnectionIP(ctx) +} + +func (p *promServiceDiscoProcessor) processAttributes(ctx context.Context, attrs pdata.AttributeMap) { + ip := getPODIP(ctx, attrs) // have to have an ip for labels lookup if ip == "" { level.Debug(p.logger).Log("msg", "unable to find ip in span attributes, skipping attribute addition") diff --git a/pkg/traces/promsdprocessor/prom_sd_processor_test.go b/pkg/traces/promsdprocessor/prom_sd_processor_test.go index 40debbebf550..c95eb937829a 100644 --- a/pkg/traces/promsdprocessor/prom_sd_processor_test.go +++ b/pkg/traces/promsdprocessor/prom_sd_processor_test.go @@ -1,6 +1,7 @@ package promsdprocessor import ( + "context" "testing" "github.com/go-kit/log" @@ -196,7 +197,7 @@ func TestOperationType(t *testing.T) { }, } p.(*promServiceDiscoProcessor).hostLabels = hostLabels - p.(*promServiceDiscoProcessor).processAttributes(attrMap) + p.(*promServiceDiscoProcessor).processAttributes(context.TODO(), attrMap) actualAttrValue, _ := attrMap.Get(attrKey) assert.Equal(t, tc.expectedValue, actualAttrValue.StringVal()) From 6102b8f71a31433e2474a0a0e08bfaf4a130ed0b Mon Sep 17 00:00:00 2001 From: Mario Rodriguez Date: Fri, 26 Nov 2021 12:15:24 +0100 Subject: [PATCH 02/12] Add tests --- .../promsdprocessor/prom_sd_processor.go | 4 +- .../promsdprocessor/prom_sd_processor_test.go | 116 ++++++++++++++++++ 2 files changed, 118 insertions(+), 2 deletions(-) diff --git a/pkg/traces/promsdprocessor/prom_sd_processor.go b/pkg/traces/promsdprocessor/prom_sd_processor.go index 635649168245..1aceed275108 100644 --- a/pkg/traces/promsdprocessor/prom_sd_processor.go +++ b/pkg/traces/promsdprocessor/prom_sd_processor.go @@ -115,7 +115,7 @@ func getConnectionIP(ctx context.Context) string { return "" } -func getPODIP(ctx context.Context, attrs pdata.AttributeMap) string { +func getPodIP(ctx context.Context, attrs pdata.AttributeMap) string { // find the ip ipTagNames := []string{ ipTagName, // jaeger/opentracing? default @@ -139,7 +139,7 @@ func getPODIP(ctx context.Context, attrs pdata.AttributeMap) string { } func (p *promServiceDiscoProcessor) processAttributes(ctx context.Context, attrs pdata.AttributeMap) { - ip := getPODIP(ctx, attrs) + ip := getPodIP(ctx, attrs) // have to have an ip for labels lookup if ip == "" { level.Debug(p.logger).Log("msg", "unable to find ip in span attributes, skipping attribute addition") diff --git a/pkg/traces/promsdprocessor/prom_sd_processor_test.go b/pkg/traces/promsdprocessor/prom_sd_processor_test.go index c95eb937829a..f395236f90f9 100644 --- a/pkg/traces/promsdprocessor/prom_sd_processor_test.go +++ b/pkg/traces/promsdprocessor/prom_sd_processor_test.go @@ -2,6 +2,8 @@ package promsdprocessor import ( "context" + "net" + "net/http" "testing" "github.com/go-kit/log" @@ -10,9 +12,11 @@ import ( "github.com/prometheus/prometheus/pkg/relabel" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "go.opentelemetry.io/collector/client" "go.opentelemetry.io/collector/consumer/consumertest" "go.opentelemetry.io/collector/model/pdata" semconv "go.opentelemetry.io/collector/model/semconv/v1.6.1" + "google.golang.org/grpc/peer" ) func TestSyncGroups(t *testing.T) { @@ -204,3 +208,115 @@ func TestOperationType(t *testing.T) { }) } } + +func TestPodAssociation(t *testing.T) { + const ipStr = "1.1.1.1" + + testCases := []struct { + name string + ctxFn func(t *testing.T) context.Context + attrMapFn func(t *testing.T) pdata.AttributeMap + expectedIP string + }{ + { + name: "HTTP connection IP", + ctxFn: func(t *testing.T) context.Context { + r := &http.Request{RemoteAddr: ipStr} + c, ok := client.FromHTTP(r) + require.True(t, ok) + return client.NewContext(context.Background(), c) + }, + attrMapFn: func(*testing.T) pdata.AttributeMap { return pdata.NewAttributeMap() }, + expectedIP: ipStr, + }, + { + name: "gRPC connection IP", + ctxFn: func(t *testing.T) context.Context { + grpcCtx := peer.NewContext(context.Background(), &peer.Peer{ + Addr: &net.TCPAddr{ + IP: net.ParseIP(ipStr), + Port: 80, + }, + }) + c, ok := client.FromGRPC(grpcCtx) + require.True(t, ok) + return client.NewContext(context.Background(), c) + }, + attrMapFn: func(*testing.T) pdata.AttributeMap { return pdata.NewAttributeMap() }, + expectedIP: ipStr, + }, + { + name: "ip attribute", + ctxFn: func(t *testing.T) context.Context { return context.Background() }, + attrMapFn: func(*testing.T) pdata.AttributeMap { + attrMap := pdata.NewAttributeMap() + attrMap.Insert(ipTagName, pdata.NewAttributeValueString(ipStr)) + return attrMap + }, + expectedIP: ipStr, + }, + { + name: "net.host.ip attribute", + ctxFn: func(t *testing.T) context.Context { return context.Background() }, + attrMapFn: func(*testing.T) pdata.AttributeMap { + attrMap := pdata.NewAttributeMap() + attrMap.Insert(semconv.AttributeNetHostIP, pdata.NewAttributeValueString(ipStr)) + return attrMap + }, + expectedIP: ipStr, + }, + { + name: "k8s ip attribute", + ctxFn: func(t *testing.T) context.Context { return context.Background() }, + attrMapFn: func(*testing.T) pdata.AttributeMap { + attrMap := pdata.NewAttributeMap() + attrMap.Insert(k8sIPTagName, pdata.NewAttributeValueString(ipStr)) + return attrMap + }, + expectedIP: ipStr, + }, + { + name: "ip from hostname", + ctxFn: func(t *testing.T) context.Context { return context.Background() }, + attrMapFn: func(*testing.T) pdata.AttributeMap { + attrMap := pdata.NewAttributeMap() + attrMap.Insert(semconv.AttributeHostName, pdata.NewAttributeValueString(ipStr)) + return attrMap + }, + expectedIP: ipStr, + }, + { + name: "uses attr before context", + ctxFn: func(t *testing.T) context.Context { + r := &http.Request{RemoteAddr: "2.2.2.2"} + c, ok := client.FromHTTP(r) + require.True(t, ok) + return client.NewContext(context.Background(), c) + }, + attrMapFn: func(*testing.T) pdata.AttributeMap { + attrMap := pdata.NewAttributeMap() + attrMap.Insert(semconv.AttributeNetHostIP, pdata.NewAttributeValueString(ipStr)) + return attrMap + }, + expectedIP: ipStr, + }, + { + name: "uses attr before hostname", + ctxFn: func(t *testing.T) context.Context { return context.Background() }, + attrMapFn: func(*testing.T) pdata.AttributeMap { + attrMap := pdata.NewAttributeMap() + attrMap.Insert(semconv.AttributeNetHostIP, pdata.NewAttributeValueString(ipStr)) + attrMap.Insert(semconv.AttributeHostName, pdata.NewAttributeValueString("3.3.3.3")) + return attrMap + }, + expectedIP: ipStr, + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + ip := getPodIP(tc.ctxFn(t), tc.attrMapFn(t)) + assert.Equal(t, tc.expectedIP, ip) + }) + } +} From 9d2ef3f7450d31409b21fd7dca0783f6664a1779 Mon Sep 17 00:00:00 2001 From: Mario Rodriguez Date: Fri, 26 Nov 2021 12:24:33 +0100 Subject: [PATCH 03/12] Changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 236007a7b669..4891ecd41170 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ # v0.21.1 (2021-11-18) +- [ENHANCEMENT] Traces: Improved pod association in PromSD processor (@mapno) + - [BUGFIX] Fix panic when using postgres_exporter integration (@saputradharma) - [BUGFIX] Fix panic when dnsamsq_exporter integration tried to log a warning (@rfratto) From fcbb6a53311caecd094569d3cb133fbd1ae0c4d3 Mon Sep 17 00:00:00 2001 From: Mario Rodriguez Date: Fri, 26 Nov 2021 12:31:38 +0100 Subject: [PATCH 04/12] typo --- pkg/traces/promsdprocessor/prom_sd_processor.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/traces/promsdprocessor/prom_sd_processor.go b/pkg/traces/promsdprocessor/prom_sd_processor.go index 1aceed275108..14569a81221c 100644 --- a/pkg/traces/promsdprocessor/prom_sd_processor.go +++ b/pkg/traces/promsdprocessor/prom_sd_processor.go @@ -26,7 +26,7 @@ import ( const ( ipTagName = "ip" - k8sIPTagName = "k8s.host.ip" + k8sIPTagName = "k8s.pod.ip" ) type promServiceDiscoProcessor struct { From 0bb63ca8f8ca9cae81405d1779141e7da4cb90cd Mon Sep 17 00:00:00 2001 From: Mario Rodriguez Date: Fri, 26 Nov 2021 16:24:28 +0100 Subject: [PATCH 05/12] Add prom_sd_pod_association --- docs/configuration/traces-config.md | 3 + pkg/traces/config.go | 10 ++-- pkg/traces/promsdprocessor/factory.go | 9 ++- .../promsdprocessor/prom_sd_processor.go | 60 ++++++++++++------- .../promsdprocessor/prom_sd_processor_test.go | 17 ++++-- 5 files changed, 66 insertions(+), 33 deletions(-) diff --git a/docs/configuration/traces-config.md b/docs/configuration/traces-config.md index a1851e9b1603..d2dbe4445fe3 100644 --- a/docs/configuration/traces-config.md +++ b/docs/configuration/traces-config.md @@ -145,6 +145,9 @@ scrape_configs: # `update` only modifies an existing k/v and `insert` only appends if the k/v # is not present. `upsert` does both. [ prom_sd_operation_type: | default = "upsert" ] +# Configures something +prom_sd_pod_association: + - [ ... ] # spanmetrics supports aggregating Request, Error and Duration (R.E.D) metrics # from span data. diff --git a/pkg/traces/config.go b/pkg/traces/config.go index ca097c1b3644..dcf6e739ddc4 100644 --- a/pkg/traces/config.go +++ b/pkg/traces/config.go @@ -110,8 +110,9 @@ type InstanceConfig struct { Attributes map[string]interface{} `yaml:"attributes,omitempty"` // prom service discovery config - ScrapeConfigs []interface{} `yaml:"scrape_configs,omitempty"` - OperationType string `yaml:"prom_sd_operation_type,omitempty"` + ScrapeConfigs []interface{} `yaml:"scrape_configs,omitempty"` + OperationType string `yaml:"prom_sd_operation_type,omitempty"` + PodAssociations []string `yaml:"prom_sd_pod_associations,omitempty"` // SpanMetricsProcessor: https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/main/processor/spanmetricsprocessor/README.md SpanMetrics *SpanMetricsConfig `yaml:"spanmetrics,omitempty"` @@ -402,8 +403,9 @@ func (c *InstanceConfig) otelConfig() (*config.Config, error) { } processorNames = append(processorNames, promsdprocessor.TypeStr) processors[promsdprocessor.TypeStr] = map[string]interface{}{ - "scrape_configs": c.ScrapeConfigs, - "operation_type": opType, + "scrape_configs": c.ScrapeConfigs, + "operation_type": opType, + "pod_associations": c.PodAssociations, } } diff --git a/pkg/traces/promsdprocessor/factory.go b/pkg/traces/promsdprocessor/factory.go index 74ab46c9f1ca..785c63169ad5 100644 --- a/pkg/traces/promsdprocessor/factory.go +++ b/pkg/traces/promsdprocessor/factory.go @@ -22,6 +22,12 @@ const ( OperationTypeUpdate = "update" // OperationTypeUpsert does both of above OperationTypeUpsert = "upsert" + + PodAssociationIPLabel = "ip" + PodAssociationOTelIPLabel = "net.host.ip" + PodAssociationk8sIPLabel = "k8s.pod.ip" + PodAssociationHostnameLabel = "hostname" + PodAssociationConnectionIP = "connection" ) // Config holds the configuration for the Prometheus SD processor. @@ -29,6 +35,7 @@ type Config struct { config.ProcessorSettings `mapstructure:",squash"` ScrapeConfigs []interface{} `mapstructure:"scrape_configs"` OperationType string `mapstructure:"operation_type"` + PodAssociations []string `mapstructure:"pod_associations"` } // NewFactory returns a new factory for the Attributes processor. @@ -65,5 +72,5 @@ func createTraceProcessor( return nil, fmt.Errorf("unable to unmarshal bytes to []*config.ScrapeConfig: %w", err) } - return newTraceProcessor(nextConsumer, oCfg.OperationType, scrapeConfigs) + return newTraceProcessor(nextConsumer, oCfg.OperationType, oCfg.PodAssociations, scrapeConfigs) } diff --git a/pkg/traces/promsdprocessor/prom_sd_processor.go b/pkg/traces/promsdprocessor/prom_sd_processor.go index 14569a81221c..4033e9ab5d8d 100644 --- a/pkg/traces/promsdprocessor/prom_sd_processor.go +++ b/pkg/traces/promsdprocessor/prom_sd_processor.go @@ -39,12 +39,13 @@ type promServiceDiscoProcessor struct { hostLabels map[string]model.LabelSet mtx sync.Mutex - operationType string + operationType string + podAssociations []string logger log.Logger } -func newTraceProcessor(nextConsumer consumer.Traces, operationType string, scrapeConfigs []*config.ScrapeConfig) (component.TracesProcessor, error) { +func newTraceProcessor(nextConsumer consumer.Traces, operationType string, podAssociations []string, scrapeConfigs []*config.ScrapeConfig) (component.TracesProcessor, error) { ctx, cancel := context.WithCancel(context.Background()) logger := log.With(util.Logger, "component", "traces service disco") @@ -72,6 +73,19 @@ func newTraceProcessor(nextConsumer consumer.Traces, operationType string, scrap return nil, fmt.Errorf("unknown operation type %s", operationType) } + for _, podAssociation := range podAssociations { + switch podAssociation { + case PodAssociationIPLabel, PodAssociationOTelIPLabel, PodAssociationk8sIPLabel, PodAssociationHostnameLabel, PodAssociationConnectionIP: + default: + cancel() + return nil, fmt.Errorf("unknown pod association %s", podAssociation) + } + } + + if len(podAssociations) == 0 { + podAssociations = []string{PodAssociationIPLabel, PodAssociationOTelIPLabel, PodAssociationk8sIPLabel, PodAssociationHostnameLabel, PodAssociationConnectionIP} + } + if nextConsumer == nil { cancel() return nil, componenterror.ErrNilNextConsumer @@ -85,6 +99,7 @@ func newTraceProcessor(nextConsumer consumer.Traces, operationType string, scrap hostLabels: make(map[string]model.LabelSet), logger: logger, operationType: operationType, + podAssociations: podAssociations, }, nil } @@ -115,31 +130,32 @@ func getConnectionIP(ctx context.Context) string { return "" } -func getPodIP(ctx context.Context, attrs pdata.AttributeMap) string { - // find the ip - ipTagNames := []string{ - ipTagName, // jaeger/opentracing? default - semconv.AttributeNetHostIP, // otel semantics for host ip - k8sIPTagName, // k8s semantics for host ip - } - - for _, name := range ipTagNames { - ip := stringAttributeFromMap(attrs, name) - if ip != "" { - return ip +func (p *promServiceDiscoProcessor) getPodIP(ctx context.Context, attrs pdata.AttributeMap) string { + for _, podAssociation := range p.podAssociations { + switch podAssociation { + case PodAssociationIPLabel, PodAssociationOTelIPLabel, PodAssociationk8sIPLabel: + fmt.Println(podAssociation) + ip := stringAttributeFromMap(attrs, podAssociation) + if ip != "" { + return ip + } + case PodAssociationHostnameLabel: + hostname := stringAttributeFromMap(attrs, semconv.AttributeHostName) + if net.ParseIP(hostname) != nil { + return hostname + } + case PodAssociationConnectionIP: + ip := getConnectionIP(ctx) + if ip != "" { + return ip + } } } - - hostname := stringAttributeFromMap(attrs, semconv.AttributeHostName) - if net.ParseIP(hostname) != nil { - return hostname - } - - return getConnectionIP(ctx) + return "" } func (p *promServiceDiscoProcessor) processAttributes(ctx context.Context, attrs pdata.AttributeMap) { - ip := getPodIP(ctx, attrs) + ip := p.getPodIP(ctx, attrs) // have to have an ip for labels lookup if ip == "" { level.Debug(p.logger).Log("msg", "unable to find ip in span attributes, skipping attribute addition") diff --git a/pkg/traces/promsdprocessor/prom_sd_processor_test.go b/pkg/traces/promsdprocessor/prom_sd_processor_test.go index f395236f90f9..98d71640cffd 100644 --- a/pkg/traces/promsdprocessor/prom_sd_processor_test.go +++ b/pkg/traces/promsdprocessor/prom_sd_processor_test.go @@ -183,7 +183,7 @@ func TestOperationType(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { mockProcessor := new(consumertest.TracesSink) - p, err := newTraceProcessor(mockProcessor, tc.operationType, nil) + p, err := newTraceProcessor(mockProcessor, tc.operationType, nil, nil) require.NoError(t, err) attrValue := pdata.NewAttributeValueString("old-value") @@ -213,10 +213,11 @@ func TestPodAssociation(t *testing.T) { const ipStr = "1.1.1.1" testCases := []struct { - name string - ctxFn func(t *testing.T) context.Context - attrMapFn func(t *testing.T) pdata.AttributeMap - expectedIP string + name string + podAssociations []string + ctxFn func(t *testing.T) context.Context + attrMapFn func(t *testing.T) pdata.AttributeMap + expectedIP string }{ { name: "HTTP connection IP", @@ -315,7 +316,11 @@ func TestPodAssociation(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { - ip := getPodIP(tc.ctxFn(t), tc.attrMapFn(t)) + mockProcessor := new(consumertest.TracesSink) + p, err := newTraceProcessor(mockProcessor, "", tc.podAssociations, nil) + require.NoError(t, err) + + ip := p.(*promServiceDiscoProcessor).getPodIP(tc.ctxFn(t), tc.attrMapFn(t)) assert.Equal(t, tc.expectedIP, ip) }) } From 2dca8f52237b642e3b7512cb1a1279056442602f Mon Sep 17 00:00:00 2001 From: Mario Rodriguez Date: Fri, 26 Nov 2021 16:45:51 +0100 Subject: [PATCH 06/12] Extend tests for pod associations --- .../promsdprocessor/prom_sd_processor_test.go | 27 +++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/pkg/traces/promsdprocessor/prom_sd_processor_test.go b/pkg/traces/promsdprocessor/prom_sd_processor_test.go index 98d71640cffd..592af5083ccd 100644 --- a/pkg/traces/promsdprocessor/prom_sd_processor_test.go +++ b/pkg/traces/promsdprocessor/prom_sd_processor_test.go @@ -287,7 +287,7 @@ func TestPodAssociation(t *testing.T) { expectedIP: ipStr, }, { - name: "uses attr before context", + name: "uses attr before context (default associations)", ctxFn: func(t *testing.T) context.Context { r := &http.Request{RemoteAddr: "2.2.2.2"} c, ok := client.FromHTTP(r) @@ -302,7 +302,7 @@ func TestPodAssociation(t *testing.T) { expectedIP: ipStr, }, { - name: "uses attr before hostname", + name: "uses attr before hostname (default associations)", ctxFn: func(t *testing.T) context.Context { return context.Background() }, attrMapFn: func(*testing.T) pdata.AttributeMap { attrMap := pdata.NewAttributeMap() @@ -312,6 +312,29 @@ func TestPodAssociation(t *testing.T) { }, expectedIP: ipStr, }, + { + name: "ip attribute but not as pod association", + podAssociations: []string{PodAssociationk8sIPLabel}, + ctxFn: func(t *testing.T) context.Context { return context.Background() }, + attrMapFn: func(*testing.T) pdata.AttributeMap { + attrMap := pdata.NewAttributeMap() + attrMap.Insert(ipTagName, pdata.NewAttributeValueString(ipStr)) + return attrMap + }, + expectedIP: "", + }, + { + name: "uses hostname before attribute (reverse order from default)", + podAssociations: []string{PodAssociationHostnameLabel, PodAssociationOTelIPLabel}, + ctxFn: func(t *testing.T) context.Context { return context.Background() }, + attrMapFn: func(*testing.T) pdata.AttributeMap { + attrMap := pdata.NewAttributeMap() + attrMap.Insert(semconv.AttributeNetHostIP, pdata.NewAttributeValueString("3.3.3.3")) + attrMap.Insert(semconv.AttributeHostName, pdata.NewAttributeValueString(ipStr)) + return attrMap + }, + expectedIP: ipStr, + }, } for _, tc := range testCases { From f8c71b10e790035d16c1a1d2a47edda323d4a24b Mon Sep 17 00:00:00 2001 From: Mario Rodriguez Date: Fri, 26 Nov 2021 17:08:03 +0100 Subject: [PATCH 07/12] Docs for pod association config --- docs/configuration/traces-config.md | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/docs/configuration/traces-config.md b/docs/configuration/traces-config.md index d2dbe4445fe3..e6133b4a51d7 100644 --- a/docs/configuration/traces-config.md +++ b/docs/configuration/traces-config.md @@ -145,7 +145,17 @@ scrape_configs: # `update` only modifies an existing k/v and `insert` only appends if the k/v # is not present. `upsert` does both. [ prom_sd_operation_type: | default = "upsert" ] -# Configures something +# Configures what methods to use to do association between spans and pods. +# PromSD processor matches the IP address of the metadata labels from the k8s API +# with the IP address obtained from the specified pod association method. +# If a match is found then the span is labeled. +# +# Options are `ip`, `net.host.ip`, `k8s.pod.ip`, `hostname` and `connection`. +# - `ip`, `net.host.ip` and `k8s.pod.ip`, `hostname` match spans tags. +# - `connection` inspects the context from the incoming requests (gRPC and HTTP). +# +# By default, all methods are enabled, and evaluated in the order specified above. +# Order of evaluation is honored when multiple methods are enabled. prom_sd_pod_association: - [ ... ] From aecdad57872c4023cbfb6bbf1db10edd06346a46 Mon Sep 17 00:00:00 2001 From: Mario Rodriguez Date: Mon, 29 Nov 2021 10:22:43 +0100 Subject: [PATCH 08/12] Lint fixes --- pkg/traces/promsdprocessor/factory.go | 10 +++++----- pkg/traces/promsdprocessor/prom_sd_processor.go | 11 +++++------ pkg/traces/promsdprocessor/prom_sd_processor_test.go | 4 ++-- 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/pkg/traces/promsdprocessor/factory.go b/pkg/traces/promsdprocessor/factory.go index 785c63169ad5..5a859bcaaf5d 100644 --- a/pkg/traces/promsdprocessor/factory.go +++ b/pkg/traces/promsdprocessor/factory.go @@ -23,11 +23,11 @@ const ( // OperationTypeUpsert does both of above OperationTypeUpsert = "upsert" - PodAssociationIPLabel = "ip" - PodAssociationOTelIPLabel = "net.host.ip" - PodAssociationk8sIPLabel = "k8s.pod.ip" - PodAssociationHostnameLabel = "hostname" - PodAssociationConnectionIP = "connection" + podAssociationIPLabel = "ip" + podAssociationOTelIPLabel = "net.host.ip" + podAssociationk8sIPLabel = "k8s.pod.ip" + podAssociationHostnameLabel = "hostname" + podAssociationConnectionIP = "connection" ) // Config holds the configuration for the Prometheus SD processor. diff --git a/pkg/traces/promsdprocessor/prom_sd_processor.go b/pkg/traces/promsdprocessor/prom_sd_processor.go index 4033e9ab5d8d..162029827a6f 100644 --- a/pkg/traces/promsdprocessor/prom_sd_processor.go +++ b/pkg/traces/promsdprocessor/prom_sd_processor.go @@ -75,7 +75,7 @@ func newTraceProcessor(nextConsumer consumer.Traces, operationType string, podAs for _, podAssociation := range podAssociations { switch podAssociation { - case PodAssociationIPLabel, PodAssociationOTelIPLabel, PodAssociationk8sIPLabel, PodAssociationHostnameLabel, PodAssociationConnectionIP: + case podAssociationIPLabel, podAssociationOTelIPLabel, podAssociationk8sIPLabel, podAssociationHostnameLabel, podAssociationConnectionIP: default: cancel() return nil, fmt.Errorf("unknown pod association %s", podAssociation) @@ -83,7 +83,7 @@ func newTraceProcessor(nextConsumer consumer.Traces, operationType string, podAs } if len(podAssociations) == 0 { - podAssociations = []string{PodAssociationIPLabel, PodAssociationOTelIPLabel, PodAssociationk8sIPLabel, PodAssociationHostnameLabel, PodAssociationConnectionIP} + podAssociations = []string{podAssociationIPLabel, podAssociationOTelIPLabel, podAssociationk8sIPLabel, podAssociationHostnameLabel, podAssociationConnectionIP} } if nextConsumer == nil { @@ -133,18 +133,17 @@ func getConnectionIP(ctx context.Context) string { func (p *promServiceDiscoProcessor) getPodIP(ctx context.Context, attrs pdata.AttributeMap) string { for _, podAssociation := range p.podAssociations { switch podAssociation { - case PodAssociationIPLabel, PodAssociationOTelIPLabel, PodAssociationk8sIPLabel: - fmt.Println(podAssociation) + case podAssociationIPLabel, podAssociationOTelIPLabel, podAssociationk8sIPLabel: ip := stringAttributeFromMap(attrs, podAssociation) if ip != "" { return ip } - case PodAssociationHostnameLabel: + case podAssociationHostnameLabel: hostname := stringAttributeFromMap(attrs, semconv.AttributeHostName) if net.ParseIP(hostname) != nil { return hostname } - case PodAssociationConnectionIP: + case podAssociationConnectionIP: ip := getConnectionIP(ctx) if ip != "" { return ip diff --git a/pkg/traces/promsdprocessor/prom_sd_processor_test.go b/pkg/traces/promsdprocessor/prom_sd_processor_test.go index 592af5083ccd..91d2270db1f5 100644 --- a/pkg/traces/promsdprocessor/prom_sd_processor_test.go +++ b/pkg/traces/promsdprocessor/prom_sd_processor_test.go @@ -314,7 +314,7 @@ func TestPodAssociation(t *testing.T) { }, { name: "ip attribute but not as pod association", - podAssociations: []string{PodAssociationk8sIPLabel}, + podAssociations: []string{podAssociationk8sIPLabel}, ctxFn: func(t *testing.T) context.Context { return context.Background() }, attrMapFn: func(*testing.T) pdata.AttributeMap { attrMap := pdata.NewAttributeMap() @@ -325,7 +325,7 @@ func TestPodAssociation(t *testing.T) { }, { name: "uses hostname before attribute (reverse order from default)", - podAssociations: []string{PodAssociationHostnameLabel, PodAssociationOTelIPLabel}, + podAssociations: []string{podAssociationHostnameLabel, podAssociationOTelIPLabel}, ctxFn: func(t *testing.T) context.Context { return context.Background() }, attrMapFn: func(*testing.T) pdata.AttributeMap { attrMap := pdata.NewAttributeMap() From 2f96c456e323bb9b5cd2ffd452d435ca409d5b59 Mon Sep 17 00:00:00 2001 From: Mario Rodriguez Date: Mon, 29 Nov 2021 11:42:06 +0100 Subject: [PATCH 09/12] Move to unreleased --- CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4891ecd41170..5991dd6cc834 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,9 +1,9 @@ # Main (unreleased) -# v0.21.1 (2021-11-18) - - [ENHANCEMENT] Traces: Improved pod association in PromSD processor (@mapno) +# v0.21.1 (2021-11-18) + - [BUGFIX] Fix panic when using postgres_exporter integration (@saputradharma) - [BUGFIX] Fix panic when dnsamsq_exporter integration tried to log a warning (@rfratto) From 1f4e5b4bc2c4d0964217bd869bd9dc0b1932df67 Mon Sep 17 00:00:00 2001 From: Mario Rodriguez Date: Mon, 29 Nov 2021 12:10:21 +0100 Subject: [PATCH 10/12] Add instrumentation recommendations --- docs/configuration/traces-config.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/docs/configuration/traces-config.md b/docs/configuration/traces-config.md index e6133b4a51d7..fb26dd90c180 100644 --- a/docs/configuration/traces-config.md +++ b/docs/configuration/traces-config.md @@ -154,6 +154,13 @@ scrape_configs: # - `ip`, `net.host.ip` and `k8s.pod.ip`, `hostname` match spans tags. # - `connection` inspects the context from the incoming requests (gRPC and HTTP). # +# Tracing instrumentation is commonly the responsible for tagging spans +# with IP address to the labels mentioned above. +# If running on kubernetes, `k8s.pod.ip` can be automatically attached via the +# downward API. For example, if you're using OTel instrumentation libraries, set +# OTEL_RESOURCE_ATTRIBUTES=k8s.pod.ip=$(POD_IP) to inject spans with the sender +# pod's IP. +# # By default, all methods are enabled, and evaluated in the order specified above. # Order of evaluation is honored when multiple methods are enabled. prom_sd_pod_association: From 3f2daebd8d61ea45ab4677b1bec1768e4d3252c9 Mon Sep 17 00:00:00 2001 From: Mario Rodriguez Date: Tue, 30 Nov 2021 14:19:48 +0100 Subject: [PATCH 11/12] Remove uncessary constants --- pkg/traces/promsdprocessor/prom_sd_processor.go | 5 ----- pkg/traces/promsdprocessor/prom_sd_processor_test.go | 6 +++--- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/pkg/traces/promsdprocessor/prom_sd_processor.go b/pkg/traces/promsdprocessor/prom_sd_processor.go index 162029827a6f..8d0b995b68a9 100644 --- a/pkg/traces/promsdprocessor/prom_sd_processor.go +++ b/pkg/traces/promsdprocessor/prom_sd_processor.go @@ -24,11 +24,6 @@ import ( semconv "go.opentelemetry.io/collector/model/semconv/v1.6.1" ) -const ( - ipTagName = "ip" - k8sIPTagName = "k8s.pod.ip" -) - type promServiceDiscoProcessor struct { nextConsumer consumer.Traces discoveryMgr *discovery.Manager diff --git a/pkg/traces/promsdprocessor/prom_sd_processor_test.go b/pkg/traces/promsdprocessor/prom_sd_processor_test.go index 91d2270db1f5..10b3d6daf60a 100644 --- a/pkg/traces/promsdprocessor/prom_sd_processor_test.go +++ b/pkg/traces/promsdprocessor/prom_sd_processor_test.go @@ -251,7 +251,7 @@ func TestPodAssociation(t *testing.T) { ctxFn: func(t *testing.T) context.Context { return context.Background() }, attrMapFn: func(*testing.T) pdata.AttributeMap { attrMap := pdata.NewAttributeMap() - attrMap.Insert(ipTagName, pdata.NewAttributeValueString(ipStr)) + attrMap.Insert(podAssociationIPLabel, pdata.NewAttributeValueString(ipStr)) return attrMap }, expectedIP: ipStr, @@ -271,7 +271,7 @@ func TestPodAssociation(t *testing.T) { ctxFn: func(t *testing.T) context.Context { return context.Background() }, attrMapFn: func(*testing.T) pdata.AttributeMap { attrMap := pdata.NewAttributeMap() - attrMap.Insert(k8sIPTagName, pdata.NewAttributeValueString(ipStr)) + attrMap.Insert(podAssociationk8sIPLabel, pdata.NewAttributeValueString(ipStr)) return attrMap }, expectedIP: ipStr, @@ -318,7 +318,7 @@ func TestPodAssociation(t *testing.T) { ctxFn: func(t *testing.T) context.Context { return context.Background() }, attrMapFn: func(*testing.T) pdata.AttributeMap { attrMap := pdata.NewAttributeMap() - attrMap.Insert(ipTagName, pdata.NewAttributeValueString(ipStr)) + attrMap.Insert(podAssociationIPLabel, pdata.NewAttributeValueString(ipStr)) return attrMap }, expectedIP: "", From 1a06aa43531e1269e5e52c91fadaa10b557f6e2e Mon Sep 17 00:00:00 2001 From: Mario Rodriguez Date: Wed, 1 Dec 2021 13:15:11 +0100 Subject: [PATCH 12/12] Improve tests --- pkg/traces/promsdprocessor/prom_sd_processor_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/traces/promsdprocessor/prom_sd_processor_test.go b/pkg/traces/promsdprocessor/prom_sd_processor_test.go index 10b3d6daf60a..c6aebb0d7c5b 100644 --- a/pkg/traces/promsdprocessor/prom_sd_processor_test.go +++ b/pkg/traces/promsdprocessor/prom_sd_processor_test.go @@ -251,7 +251,7 @@ func TestPodAssociation(t *testing.T) { ctxFn: func(t *testing.T) context.Context { return context.Background() }, attrMapFn: func(*testing.T) pdata.AttributeMap { attrMap := pdata.NewAttributeMap() - attrMap.Insert(podAssociationIPLabel, pdata.NewAttributeValueString(ipStr)) + attrMap.Insert("ip", pdata.NewAttributeValueString(ipStr)) return attrMap }, expectedIP: ipStr, @@ -271,7 +271,7 @@ func TestPodAssociation(t *testing.T) { ctxFn: func(t *testing.T) context.Context { return context.Background() }, attrMapFn: func(*testing.T) pdata.AttributeMap { attrMap := pdata.NewAttributeMap() - attrMap.Insert(podAssociationk8sIPLabel, pdata.NewAttributeValueString(ipStr)) + attrMap.Insert("k8s.pod.ip", pdata.NewAttributeValueString(ipStr)) return attrMap }, expectedIP: ipStr, @@ -318,7 +318,7 @@ func TestPodAssociation(t *testing.T) { ctxFn: func(t *testing.T) context.Context { return context.Background() }, attrMapFn: func(*testing.T) pdata.AttributeMap { attrMap := pdata.NewAttributeMap() - attrMap.Insert(podAssociationIPLabel, pdata.NewAttributeValueString(ipStr)) + attrMap.Insert("ip", pdata.NewAttributeValueString(ipStr)) return attrMap }, expectedIP: "",