Skip to content

Commit 7005086

Browse files
committed
auto-instrumentation-go
Added logic for golang ebpf sidecar auto-instrumentation
1 parent 2b0a0cd commit 7005086

38 files changed

+1928
-444
lines changed

Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ ARG NEWRELIC_INSTRUMENTATION_JAVA_VERSION
2525
ARG NEWRELIC_INSTRUMENTATION_NODEJS_VERSION
2626
ARG NEWRELIC_INSTRUMENTATION_PYTHON_VERSION
2727
ARG NEWRELIC_INSTRUMENTATION_DOTNET_VERSION
28+
ARG AUTO_INSTRUMENTATION_GO_VERSION
2829
# Build
2930
# the GOARCH has not a default value to allow the binary be built according to the host where the command
3031
# was called. For example, if we call make docker-build in a local env which has the Apple Silicon M1 SO

Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ NEWRELIC_INSTRUMENTATION_JAVA_VERSION ?= "$(shell grep -v '\#' versions.txt | gr
1111
NEWRELIC_INSTRUMENTATION_NODEJS_VERSION ?= "$(shell grep -v '\#' versions.txt | grep newrelic-instrumentation-nodejs | awk -F= '{print $$2}')"
1212
NEWRELIC_INSTRUMENTATION_PYTHON_VERSION ?= "$(shell grep -v '\#' versions.txt | grep newrelic-instrumentation-python | awk -F= '{print $$2}')"
1313
NEWRELIC_INSTRUMENTATION_DOTNET_VERSION ?= "$(shell grep -v '\#' versions.txt | grep newrelic-instrumentation-dotnet | awk -F= '{print $$2}')"
14-
LD_FLAGS ?= "-X ${VERSION_PKG}.version=${VERSION} -X ${VERSION_PKG}.buildDate=${VERSION_DATE} -X ${VERSION_PKG}.autoInstrumentationJava=${NEWRELIC_INSTRUMENTATION_JAVA_VERSION} -X ${VERSION_PKG}.autoInstrumentationNodeJS=${NEWRELIC_INSTRUMENTATION_NODEJS_VERSION} -X ${VERSION_PKG}.autoInstrumentationPython=${NEWRELIC_INSTRUMENTATION_PYTHON_VERSION} -X ${VERSION_PKG}.autoInstrumentationDotNet=${NEWRELIC_INSTRUMENTATION_DOTNET_VERSION}"
14+
AUTO_INSTRUMENTATION_GO_VERSION ?= "$(shell grep -v '\#' versions.txt | grep autoinstrumentation-go | awk -F= '{print $$2}')"
15+
LD_FLAGS ?= "-X ${VERSION_PKG}.version=${VERSION} -X ${VERSION_PKG}.buildDate=${VERSION_DATE} -X ${VERSION_PKG}.autoInstrumentationJava=${NEWRELIC_INSTRUMENTATION_JAVA_VERSION} -X ${VERSION_PKG}.autoInstrumentationNodeJS=${NEWRELIC_INSTRUMENTATION_NODEJS_VERSION} -X ${VERSION_PKG}.autoInstrumentationPython=${NEWRELIC_INSTRUMENTATION_PYTHON_VERSION} -X ${VERSION_PKG}.autoInstrumentationGo=${AUTO_INSTRUMENTATION_GO_VERSION} -X ${VERSION_PKG}.autoInstrumentationDotNet=${NEWRELIC_INSTRUMENTATION_DOTNET_VERSION}"
1516
ARCH ?= $(shell go env GOARCH)
1617

1718
# Image URL to use all building/pushing image targets
@@ -310,4 +311,4 @@ $(HELMIFY): $(LOCALBIN)
310311
test -s $(LOCALBIN)/helmify || GOBIN=$(LOCALBIN) go install github.com/arttor/helmify/cmd/helmify@$(HELMIFY_VERSION)
311312

312313
helm: manifests kustomize helmify
313-
$(KUSTOMIZE) build config/default | $(HELMIFY)
314+
$(KUSTOMIZE) build config/default | $(HELMIFY)

api/v1alpha1/instrumentation_types.go

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package v1alpha1
1818

1919
import (
2020
corev1 "k8s.io/api/core/v1"
21+
"k8s.io/apimachinery/pkg/api/resource"
2122
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2223
)
2324

@@ -26,7 +27,27 @@ type InstrumentationSpec struct {
2627
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
2728
// Important: Run "make" to regenerate code after modifying this file
2829

29-
// Env defines agent specific env vars
30+
// Exporter defines exporter configuration.
31+
// +optional
32+
Exporter `json:"exporter,omitempty"`
33+
34+
// Resource defines the configuration for the resource attributes, as defined by the OpenTelemetry specification.
35+
// +optional
36+
Resource Resource `json:"resource,omitempty"`
37+
38+
// Propagators defines inter-process context propagation configuration.
39+
// Values in this list will be set in the OTEL_PROPAGATORS env var.
40+
// Enum=tracecontext;none
41+
// +optional
42+
Propagators []Propagator `json:"propagators,omitempty"`
43+
44+
// Sampler defines sampling configuration.
45+
// +optional
46+
Sampler `json:"sampler,omitempty"`
47+
48+
// Env defines common env vars. There are four layers for env vars' definitions and
49+
// the precedence order is: `original container env vars` > `language specific env vars` > `common env vars` > `instrument spec configs' vars`.
50+
// If the former var had been defined, then the other vars would be ignored.
3051
// +optional
3152
Env []corev1.EnvVar `json:"env,omitempty"`
3253

@@ -45,6 +66,47 @@ type InstrumentationSpec struct {
4566
// DotNet defines configuration for DotNet auto-instrumentation.
4667
// +optional
4768
DotNet DotNet `json:"dotnet,omitempty"`
69+
70+
// Go defines configuration for Go auto-instrumentation.
71+
// When using Go auto-instrumentation you must provide a value for the OTEL_GO_AUTO_TARGET_EXE env var via the
72+
// Instrumentation env vars or via the instrumentation.opentelemetry.io/otel-go-auto-target-exe pod annotation.
73+
// Failure to set this value causes instrumentation injection to abort, leaving the original pod unchanged.
74+
// +optional
75+
Go Go `json:"go,omitempty"`
76+
}
77+
78+
type Resource struct {
79+
// Attributes defines attributes that are added to the resource.
80+
// For example environment: dev
81+
// +optional
82+
Attributes map[string]string `json:"resourceAttributes,omitempty"`
83+
84+
// AddK8sUIDAttributes defines whether K8s UID attributes should be collected (e.g. k8s.deployment.uid).
85+
// +optional
86+
AddK8sUIDAttributes bool `json:"addK8sUIDAttributes,omitempty"`
87+
}
88+
89+
// Exporter defines OTLP exporter configuration.
90+
type Exporter struct {
91+
// Endpoint is address of the collector with OTLP endpoint.
92+
// +optional
93+
Endpoint string `json:"endpoint,omitempty"`
94+
}
95+
96+
// Sampler defines sampling configuration.
97+
type Sampler struct {
98+
// Type defines sampler type.
99+
// The value will be set in the OTEL_TRACES_SAMPLER env var.
100+
// The value can be for instance parentbased_always_on, parentbased_always_off, parentbased_traceidratio...
101+
// +optional
102+
Type SamplerType `json:"type,omitempty"`
103+
104+
// Argument defines sampler argument.
105+
// The value depends on the sampler type.
106+
// For instance for parentbased_traceidratio sampler type it is a number in range [0..1] e.g. 0.25.
107+
// The value will be set in the OTEL_TRACES_SAMPLER_ARG env var.
108+
// +optional
109+
Argument string `json:"argument,omitempty"`
48110
}
49111

50112
// Java defines Java agent and instrumentation configuration.
@@ -94,6 +156,26 @@ type DotNet struct {
94156
Env []corev1.EnvVar `json:"env,omitempty"`
95157
}
96158

159+
type Go struct {
160+
// Image is a container image with Go SDK and auto-instrumentation.
161+
// +optional
162+
Image string `json:"image,omitempty"`
163+
164+
// VolumeSizeLimit defines size limit for volume used for auto-instrumentation.
165+
// The default size is 200Mi.
166+
VolumeSizeLimit *resource.Quantity `json:"volumeLimitSize,omitempty"`
167+
168+
// Env defines Go specific env vars. There are four layers for env vars' definitions and
169+
// the precedence order is: `original container env vars` > `language specific env vars` > `common env vars` > `instrument spec configs' vars`.
170+
// If the former var had been defined, then the other vars would be ignored.
171+
// +optional
172+
Env []corev1.EnvVar `json:"env,omitempty"`
173+
174+
// Resources describes the compute resource requirements.
175+
// +optional
176+
Resources corev1.ResourceRequirements `json:"resourceRequirements,omitempty"`
177+
}
178+
97179
// InstrumentationStatus defines the observed state of Instrumentation
98180
type InstrumentationStatus struct {
99181
}

api/v1alpha1/instrumentation_webhook.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ const (
3232
AnnotationDefaultAutoInstrumentationNodeJS = "instrumentation.newrelic.com/default-auto-instrumentation-nodejs-image"
3333
AnnotationDefaultAutoInstrumentationPython = "instrumentation.newrelic.com/default-auto-instrumentation-python-image"
3434
AnnotationDefaultAutoInstrumentationDotNet = "instrumentation.newrelic.com/default-auto-instrumentation-dotnet-image"
35-
envPrefix = "NEW_RELIC_"
35+
AnnotationDefaultAutoInstrumentationGo = "instrumentation.newrelic.com/default-auto-instrumentation-go-image"
36+
envNewRelicPrefix = "NEW_RELIC_"
37+
envOtelPrefix = "OTEL_"
3638
)
3739

3840
// log is for logging in this package.
@@ -78,6 +80,11 @@ func (r *Instrumentation) Default() {
7880
r.Spec.DotNet.Image = val
7981
}
8082
}
83+
if r.Spec.Go.Image == "" {
84+
if val, ok := r.Annotations[AnnotationDefaultAutoInstrumentationGo]; ok {
85+
r.Spec.Go.Image = val
86+
}
87+
}
8188
}
8289

8390
// +kubebuilder:webhook:verbs=create;update,path=/validate-newrelic-com-v1alpha1-instrumentation,mutating=false,failurePolicy=fail,groups=newrelic.com,resources=instrumentations,versions=v1alpha1,name=vinstrumentationcreateupdate.kb.io,sideEffects=none,admissionReviewVersions=v1
@@ -121,14 +128,17 @@ func (r *Instrumentation) validate() error {
121128
if err := r.validateEnv(r.Spec.DotNet.Env); err != nil {
122129
return err
123130
}
131+
if err := r.validateEnv(r.Spec.Go.Env); err != nil {
132+
return err
133+
}
124134

125135
return nil
126136
}
127137

128138
func (r *Instrumentation) validateEnv(envs []corev1.EnvVar) error {
129139
for _, env := range envs {
130-
if !strings.HasPrefix(env.Name, envPrefix) {
131-
return fmt.Errorf("env name should start with \"NEW_RELIC_\": %s", env.Name)
140+
if !strings.HasPrefix(env.Name, envNewRelicPrefix) && !strings.HasPrefix(env.Name, envOtelPrefix) {
141+
return fmt.Errorf("env name should start with \"NEW_RELIC_\" or \"OTEL_\": %s", env.Name)
132142
}
133143
}
134144
return nil

api/v1alpha1/propagators.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright The OpenTelemetry Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package v1alpha1
16+
17+
type (
18+
// Propagator represents the propagation type.
19+
// +kubebuilder:validation:Enum=tracecontext;none
20+
Propagator string
21+
)
22+
23+
const (
24+
// TraceContext represents W3C Trace Context.
25+
TraceContext Propagator = "tracecontext"
26+
// None represents automatically configured propagator.
27+
None Propagator = "none"
28+
)

api/v1alpha1/samplers.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright The OpenTelemetry Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package v1alpha1
16+
17+
type (
18+
// SamplerType represents sampler type.
19+
// +kubebuilder:validation:Enum=always_on;always_off;traceidratio;parentbased_always_on;parentbased_always_off;parentbased_traceidratio
20+
SamplerType string
21+
)
22+
23+
const (
24+
// AlwaysOn represents AlwaysOnSampler.
25+
AlwaysOn SamplerType = "always_on"
26+
// AlwaysOff represents AlwaysOffSampler.
27+
AlwaysOff SamplerType = "always_off"
28+
// TraceIDRatio represents TraceIdRatioBased.
29+
TraceIDRatio SamplerType = "traceidratio"
30+
// ParentBasedAlwaysOn represents ParentBased(root=AlwaysOnSampler).
31+
ParentBasedAlwaysOn SamplerType = "parentbased_always_on"
32+
// ParentBasedAlwaysOff represents ParentBased(root=AlwaysOffSampler).
33+
ParentBasedAlwaysOff SamplerType = "parentbased_always_off"
34+
// ParentBasedTraceIDRatio represents ParentBased(root=TraceIdRatioBased).
35+
ParentBasedTraceIDRatio SamplerType = "parentbased_traceidratio"
36+
)

api/v1alpha1/zz_generated.deepcopy.go

Lines changed: 89 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
10.20.0
1+
10.20.0

autoinstrumentation/java/version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8.7.0
1+
8.7.0
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
newrelic==9.2.0
1+
newrelic==9.2.0

0 commit comments

Comments
 (0)