From 7370900b043f407e4bdce6a4a866a314f1bbf8ec Mon Sep 17 00:00:00 2001 From: Owais Lone Date: Thu, 23 Apr 2020 00:30:00 +0530 Subject: [PATCH] Use `k8s.pod.ip` to record resource IP instead of just `ip` (#183) Since the k8s processor prefixes all labels with `k8s.*.`, adding the same prefix to the IP label. We'll still continue to look for the `ip` label on resource/node when we can't find the IP by other means but will only write the IP back to `k8s.pod.ip`. --- processor/k8sprocessor/processor.go | 17 +++++++++++++---- processor/k8sprocessor/processor_test.go | 6 +++--- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/processor/k8sprocessor/processor.go b/processor/k8sprocessor/processor.go index 7aaaada01d38..d1fa83def79f 100644 --- a/processor/k8sprocessor/processor.go +++ b/processor/k8sprocessor/processor.go @@ -29,7 +29,8 @@ import ( const ( sourceFormatJaeger string = "jaeger" - ipLabelName string = "ip" + k8sIPLabelName string = "k8s.pod.ip" + clientIPLabelName string = "ip" ) type kubernetesprocessor struct { @@ -92,7 +93,7 @@ func (kp *kubernetesprocessor) ConsumeTraceData(ctx context.Context, td consumer // check if the application, a collector/agent or a prior processor has already // annotated the batch with IP. if td.Resource != nil { - podIP = td.Resource.Labels[ipLabelName] + podIP = kp.k8sIPFromAttributes(td.Resource.Labels) } // Jaeger client libs tag the process with the process/resource IP and @@ -100,7 +101,7 @@ func (kp *kubernetesprocessor) ConsumeTraceData(ctx context.Context, td consumer // TODO: Should jaeger translator map jaeger process to OC resource instead? if podIP == "" && td.SourceFormat == sourceFormatJaeger { if td.Node != nil { - podIP = td.Node.Attributes[ipLabelName] + podIP = kp.k8sIPFromAttributes(td.Node.Attributes) } } @@ -118,7 +119,7 @@ func (kp *kubernetesprocessor) ConsumeTraceData(ctx context.Context, td consumer if td.Resource.Labels == nil { td.Resource.Labels = map[string]string{} } - td.Resource.Labels[ipLabelName] = podIP + td.Resource.Labels[k8sIPLabelName] = podIP } // Don't invoke any k8s client functionality in passthrough mode. @@ -154,3 +155,11 @@ func (kp *kubernetesprocessor) getAttributesForPodIP(ip string) map[string]strin } return pod.Attributes } + +func (kp *kubernetesprocessor) k8sIPFromAttributes(attrs map[string]string) string { + ip := attrs[k8sIPLabelName] + if ip == "" { + ip = attrs[clientIPLabelName] + } + return ip +} diff --git a/processor/k8sprocessor/processor_test.go b/processor/k8sprocessor/processor_test.go index 0d8b099d7ba1..f0f2dd01ef49 100644 --- a/processor/k8sprocessor/processor_test.go +++ b/processor/k8sprocessor/processor_test.go @@ -55,7 +55,7 @@ func TestIPDetection(t *testing.T) { require.Len(t, next.data, 1) require.NotNil(t, next.data[0].Resource) - assert.Equal(t, next.data[0].Resource.Labels["ip"], "1.1.1.1") + assert.Equal(t, next.data[0].Resource.Labels["k8s.pod.ip"], "1.1.1.1") } func TestNoIP(t *testing.T) { @@ -113,7 +113,7 @@ func TestJaegerIP(t *testing.T) { require.NoError(t, err) require.Len(t, next.data, 2) assert.NotNil(t, next.data[1].Resource) - assert.Equal(t, next.data[1].Resource.Labels["ip"], "1.1.1.1") + assert.Equal(t, next.data[1].Resource.Labels["k8s.pod.ip"], "1.1.1.1") } func TestAddLabels(t *testing.T) { @@ -148,7 +148,7 @@ func TestAddLabels(t *testing.T) { require.Len(t, next.data, i+1) td := next.data[i] require.NotNil(t, td.Resource) - assert.Equal(t, td.Resource.Labels["ip"], ip) + assert.Equal(t, td.Resource.Labels["k8s.pod.ip"], ip) for k, v := range attrs { vv, ok := td.Resource.Labels[k] assert.True(t, ok)