Skip to content

Commit

Permalink
Add sampler configuration to instrumentation kind
Browse files Browse the repository at this point in the history
Signed-off-by: Pavol Loffay <[email protected]>
  • Loading branch information
pavolloffay committed Nov 9, 2021
1 parent de6219f commit 5903d05
Show file tree
Hide file tree
Showing 11 changed files with 176 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@ spec:
- tracecontext
- baggage
- b3
sampler:
type: parentbased_traceidratio
argument: "0.25"
java:
image: ghcr.io/open-telemetry/opentelemetry-operator/autoinstrumentation-java:latest # <1>
EOF
Expand Down
21 changes: 21 additions & 0 deletions apis/instrumentation/v1alpha1/instrumentation_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ type InstrumentationSpec struct {
// +operator-sdk:gen-csv:customresourcedefinitions.specDescriptors=true
Propagators []Propagator `json:"propagators,omitempty"`

// Sampler defines sampling configuration.
// +optional
// +operator-sdk:gen-csv:customresourcedefinitions.specDescriptors=true
Sampler `json:"sampler,omitempty"`

// Java defines configuration for java auto-instrumentation.
// +optional
// +operator-sdk:gen-csv:customresourcedefinitions.specDescriptors=true
Expand All @@ -55,6 +60,22 @@ type Exporter struct {
Endpoint string `json:"endpoint,omitempty"`
}

// Sampler defines sampling configuration.
type Sampler struct {
// Type defines sampler type.
// The value can be for instance parentbased_always_on, parentbased_always_off, parentbased_traceidratio...
// +optional
// +operator-sdk:gen-csv:customresourcedefinitions.specDescriptors=true
Type SamplerType `json:"type,omitempty"`

// Argument defines sampler argument.
// The value depends on the sampler type.
// For instance for parentbased_traceidratio sampler type it is a number in range [0..1] e.g. 0.25.
// +optional
// +operator-sdk:gen-csv:customresourcedefinitions.specDescriptors=true
Argument string `json:"argument,omitempty"`
}

// InstrumentationStatus defines status of the instrumentation.
type InstrumentationStatus struct {
}
Expand Down
40 changes: 40 additions & 0 deletions apis/instrumentation/v1alpha1/samplers.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 (
// SamplerType represents sampler type.
// +kubebuilder:validation:Enum=always_on;always_off;traceidratio;parentbased_always_on;parentbased_always_off;parentbased_traceidratio;jaeger_remote;xray
SamplerType string
)

const (
// AlwaysOn represents AlwaysOnSampler.
AlwaysOn SamplerType = "always_on"
// AlwaysOff represents AlwaysOffSampler.
AlwaysOff SamplerType = "always_off"
// TraceIDRatio represents TraceIdRatioBased.
TraceIDRatio SamplerType = "traceidratio"
// ParentBasedAlwaysOn represents ParentBased(root=AlwaysOnSampler).
ParentBasedAlwaysOn SamplerType = "parentbased_always_on"
// ParentBasedAlwaysOff represents ParentBased(root=AlwaysOffSampler).
ParentBasedAlwaysOff SamplerType = "parentbased_always_off"
// ParentBasedTraceIDRatio represents ParentBased(root=TraceIdRatioBased).
ParentBasedTraceIDRatio SamplerType = "parentbased_traceidratio"
// JaegerRemote represents JaegerRemoteSampler.
JaegerRemote SamplerType = "jaeger_remote"
// XRay represents AWS X-Ray Centralized Sampling.
XRaySampler SamplerType = "xray"
)
16 changes: 16 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.

22 changes: 22 additions & 0 deletions bundle/manifests/opentelemetry.io_instrumentations.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,28 @@ spec:
description: ResourceAttributes defines attributes that are added
to resource.
type: object
sampler:
description: Sampler defines sampling configuration.
properties:
argument:
description: Argument defines sampler argument. The value depends
on the sampler type. For instance for parentbased_traceidratio
sampler type it is a number in range [0..1] e.g. 0.25.
type: string
type:
description: Type defines sampler type. The value can be for instance
parentbased_always_on, parentbased_always_off, parentbased_traceidratio...
enum:
- always_on
- always_off
- traceidratio
- parentbased_always_on
- parentbased_always_off
- parentbased_traceidratio
- jaeger_remote
- xray
type: string
type: object
type: object
status:
description: InstrumentationStatus defines status of the instrumentation.
Expand Down
22 changes: 22 additions & 0 deletions config/crd/bases/opentelemetry.io_instrumentations.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,28 @@ spec:
description: ResourceAttributes defines attributes that are added
to resource.
type: object
sampler:
description: Sampler defines sampling configuration.
properties:
argument:
description: Argument defines sampler argument. The value depends
on the sampler type. For instance for parentbased_traceidratio
sampler type it is a number in range [0..1] e.g. 0.25.
type: string
type:
description: Type defines sampler type. The value can be for instance
parentbased_always_on, parentbased_always_off, parentbased_traceidratio...
enum:
- always_on
- always_off
- traceidratio
- parentbased_always_on
- parentbased_always_off
- parentbased_traceidratio
- jaeger_remote
- xray
type: string
type: object
type: object
status:
description: InstrumentationStatus defines status of the instrumentation.
Expand Down
20 changes: 20 additions & 0 deletions pkg/instrumentation/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ const (
envOTELExporterOTLPEndpoint = "OTEL_EXPORTER_OTLP_ENDPOINT"
envOTELResourceAttrs = "OTEL_RESOURCE_ATTRIBUTES"
envOTELPropagators = "OTEL_PROPAGATORS"
envOTELTracesSampler = "OTEL_TRACES_SAMPLER"
envOTELTracesSamplerArg = "OTEL_TRACES_SAMPLER_ARG"
)

// inject a new sidecar container to the given pod, based on the given OpenTelemetryCollector.
Expand Down Expand Up @@ -90,6 +92,24 @@ func injectCommonSDKConfig(otelinst v1alpha1.Instrumentation, ns corev1.Namespac
})
}

idx = getIndexOfEnv(container.Env, envOTELTracesSampler)
// configure sampler only if it is configured in the CR
if idx == -1 && otelinst.Spec.Sampler.Type != "" {
idxSamplerArg := getIndexOfEnv(container.Env, envOTELTracesSamplerArg)
if idxSamplerArg == -1 {
container.Env = append(container.Env, corev1.EnvVar{
Name: envOTELTracesSampler,
Value: string(otelinst.Spec.Sampler.Type),
})
if otelinst.Spec.Sampler.Argument != "" {
container.Env = append(container.Env, corev1.EnvVar{
Name: envOTELTracesSamplerArg,
Value: otelinst.Spec.Sampler.Argument,
})
}
}
}

return pod
}

Expand Down
24 changes: 24 additions & 0 deletions pkg/instrumentation/sdk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ func TestSDKInjection(t *testing.T) {
Endpoint: "https://collector:4317",
},
Propagators: []v1alpha1.Propagator{"b3", "jaeger"},
Sampler: v1alpha1.Sampler{
Type: "parentbased_traceidratio",
Argument: "0.25",
},
},
},
pod: corev1.Pod{
Expand Down Expand Up @@ -81,6 +85,14 @@ func TestSDKInjection(t *testing.T) {
Name: "OTEL_PROPAGATORS",
Value: "b3,jaeger",
},
{
Name: "OTEL_TRACES_SAMPLER",
Value: "parentbased_traceidratio",
},
{
Name: "OTEL_TRACES_SAMPLER_ARG",
Value: "0.25",
},
},
},
},
Expand All @@ -98,6 +110,10 @@ func TestSDKInjection(t *testing.T) {
"fromcr": "val",
},
Propagators: []v1alpha1.Propagator{"jaeger"},
Sampler: v1alpha1.Sampler{
Type: "parentbased_traceidratio",
Argument: "0.25",
},
},
},
pod: corev1.Pod{
Expand Down Expand Up @@ -125,6 +141,10 @@ func TestSDKInjection(t *testing.T) {
Name: "OTEL_PROPAGATORS",
Value: "b3",
},
{
Name: "OTEL_TRACES_SAMPLER",
Value: "always_on",
},
},
},
},
Expand Down Expand Up @@ -155,6 +175,10 @@ func TestSDKInjection(t *testing.T) {
Name: "OTEL_PROPAGATORS",
Value: "b3",
},
{
Name: "OTEL_TRACES_SAMPLER",
Value: "always_on",
},
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,9 @@ spec:
propagators:
- jaeger
- b3
endpoint: http://localhost:4318
sampler:
type: parentbased_traceidratio
argument: "0.25"
java:
image: ghcr.io/open-telemetry/opentelemetry-operator/autoinstrumentation-java:latest
4 changes: 4 additions & 0 deletions tests/e2e/instrumentation-java/01-assert.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ spec:
- name: OTEL_RESOURCE_ATTRIBUTES
- name: OTEL_PROPAGATORS
value: jaeger,b3
- name: OTEL_TRACES_SAMPLER
value: parentbased_traceidratio
- name: OTEL_TRACES_SAMPLER_ARG
value: "0.25"
- name: JAVA_TOOL_OPTIONS
value: " -javaagent:/otel-auto-instrumentation/javaagent.jar"
volumeMounts:
Expand Down

0 comments on commit 5903d05

Please sign in to comment.