Skip to content

Commit

Permalink
Add propagators configuration to the Instrumentation kind (#515)
Browse files Browse the repository at this point in the history
Signed-off-by: Pavol Loffay <p.loffay@gmail.com>
  • Loading branch information
pavolloffay committed Nov 9, 2021
1 parent f6607c9 commit de6219f
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ metadata:
spec:
exporter:
endpoint: http://otel-collector:4317
propagators:
- tracecontext
- baggage
- b3
java:
image: ghcr.io/open-telemetry/opentelemetry-operator/autoinstrumentation-java:latest # <1>
EOF
Expand Down
5 changes: 5 additions & 0 deletions apis/instrumentation/v1alpha1/instrumentation_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ type InstrumentationSpec struct {
// +operator-sdk:gen-csv:customresourcedefinitions.specDescriptors=true
ResourceAttributes map[string]string `json:"resourceAttributes,omitempty"`

// Propagators defines inter-process context propagation configuration.
// +optional
// +operator-sdk:gen-csv:customresourcedefinitions.specDescriptors=true
Propagators []Propagator `json:"propagators,omitempty"`

// Java defines configuration for java auto-instrumentation.
// +optional
// +operator-sdk:gen-csv:customresourcedefinitions.specDescriptors=true
Expand Down
40 changes: 40 additions & 0 deletions apis/instrumentation/v1alpha1/propagation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package v1alpha1

type (
// Propagator represents the propagation type.
// +kubebuilder:validation:Enum=tracecontext;baggage;b3;b3multi;jaeger;xray;ottrace;no
Propagator string
)

const (
// TraceContext represents W3C Trace Context.
TraceContext Propagator = "tracecontext"
// Baggage represents W3C Baggage.
Baggage Propagator = "baggage"
// B3 represents B3 Single.
B3 Propagator = "b3"
// B3Multi represents B3 Multi.
B3Multi Propagator = "b3multi"
// Jaeger represents Jaeger.
Jaeger Propagator = "jaeger"
// XRay represents AWS X-Ray.
XRay Propagator = "xray"
// OTTrace represents OT Trace.
OTTrace Propagator = "ottrace"
// No represents automatically configured propagator.
None Propagator = "no"
)
5 changes: 5 additions & 0 deletions apis/instrumentation/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions bundle/manifests/opentelemetry.io_instrumentations.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,22 @@ spec:
description: Image is a container image with javaagent JAR.
type: string
type: object
propagators:
description: Propagators defines inter-process context propagation
configuration.
items:
description: Propagator represents the propagation type.
enum:
- tracecontext
- baggage
- b3
- b3multi
- jaeger
- xray
- ottrace
- "no"
type: string
type: array
resourceAttributes:
additionalProperties:
type: string
Expand Down
16 changes: 16 additions & 0 deletions config/crd/bases/opentelemetry.io_instrumentations.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,22 @@ spec:
description: Image is a container image with javaagent JAR.
type: string
type: object
propagators:
description: Propagators defines inter-process context propagation
configuration.
items:
description: Propagator represents the propagation type.
enum:
- tracecontext
- baggage
- b3
- b3multi
- jaeger
- xray
- ottrace
- "no"
type: string
type: array
resourceAttributes:
additionalProperties:
type: string
Expand Down
10 changes: 10 additions & 0 deletions pkg/instrumentation/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"fmt"
"sort"
"strings"
"unsafe"

"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
Expand All @@ -32,6 +33,7 @@ const (
envOTELServiceName = "OTEL_SERVICE_NAME"
envOTELExporterOTLPEndpoint = "OTEL_EXPORTER_OTLP_ENDPOINT"
envOTELResourceAttrs = "OTEL_RESOURCE_ATTRIBUTES"
envOTELPropagators = "OTEL_PROPAGATORS"
)

// inject a new sidecar container to the given pod, based on the given OpenTelemetryCollector.
Expand Down Expand Up @@ -79,6 +81,14 @@ func injectCommonSDKConfig(otelinst v1alpha1.Instrumentation, ns corev1.Namespac
}
container.Env[idx].Value += resStr
}
idx = getIndexOfEnv(container.Env, envOTELPropagators)
if idx == -1 && len(otelinst.Spec.Propagators) > 0 {
propagators := *(*[]string)((unsafe.Pointer(&otelinst.Spec.Propagators)))
container.Env = append(container.Env, corev1.EnvVar{
Name: envOTELPropagators,
Value: strings.Join(propagators, ","),
})
}

return pod
}
Expand Down
14 changes: 14 additions & 0 deletions pkg/instrumentation/sdk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func TestSDKInjection(t *testing.T) {
Exporter: v1alpha1.Exporter{
Endpoint: "https://collector:4317",
},
Propagators: []v1alpha1.Propagator{"b3", "jaeger"},
},
},
pod: corev1.Pod{
Expand Down Expand Up @@ -76,6 +77,10 @@ func TestSDKInjection(t *testing.T) {
Name: "OTEL_RESOURCE_ATTRIBUTES",
Value: "k8s.container.name=application-name,k8s.namespace.name=project1,k8s.pod.name=app",
},
{
Name: "OTEL_PROPAGATORS",
Value: "b3,jaeger",
},
},
},
},
Expand All @@ -92,6 +97,7 @@ func TestSDKInjection(t *testing.T) {
ResourceAttributes: map[string]string{
"fromcr": "val",
},
Propagators: []v1alpha1.Propagator{"jaeger"},
},
},
pod: corev1.Pod{
Expand All @@ -115,6 +121,10 @@ func TestSDKInjection(t *testing.T) {
Name: "OTEL_RESOURCE_ATTRIBUTES",
Value: "foo=bar,k8s.container.name=other,",
},
{
Name: "OTEL_PROPAGATORS",
Value: "b3",
},
},
},
},
Expand All @@ -141,6 +151,10 @@ func TestSDKInjection(t *testing.T) {
Name: "OTEL_RESOURCE_ATTRIBUTES",
Value: "foo=bar,k8s.container.name=other,fromcr=val,k8s.namespace.name=project1,k8s.pod.name=app",
},
{
Name: "OTEL_PROPAGATORS",
Value: "b3",
},
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@ metadata:
spec:
exporter:
endpoint: http://localhost:4317
propagators:
- jaeger
- b3
java:
image: ghcr.io/open-telemetry/opentelemetry-operator/autoinstrumentation-java:latest
2 changes: 2 additions & 0 deletions tests/e2e/instrumentation-java/01-assert.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ spec:
- name: OTEL_EXPORTER_OTLP_ENDPOINT
value: http://localhost:4317
- name: OTEL_RESOURCE_ATTRIBUTES
- name: OTEL_PROPAGATORS
value: jaeger,b3
- name: JAVA_TOOL_OPTIONS
value: " -javaagent:/otel-auto-instrumentation/javaagent.jar"
volumeMounts:
Expand Down

0 comments on commit de6219f

Please sign in to comment.