Skip to content

Commit

Permalink
Add env support to instrumentation kind (#674)
Browse files Browse the repository at this point in the history
* allow config customized envs on common and language specific node.

* only env name start with "OTEL_" is allowed.

* deployment env > language spec customzied env > language spec config type> common customized env > common config type

* api docs gen and code fmt

* use corev1.EnvVar instead of Env

* fixing wrong change.

* update e2e testcases

* fix unit tests
  • Loading branch information
Duncan-tree-zhou authored Jan 28, 2022
1 parent d965ac3 commit 2105dad
Show file tree
Hide file tree
Showing 18 changed files with 2,347 additions and 83 deletions.
25 changes: 25 additions & 0 deletions apis/v1alpha1/instrumentation_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package v1alpha1

import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand All @@ -36,6 +37,12 @@ type InstrumentationSpec struct {
// +optional
Sampler `json:"sampler,omitempty"`

// Env defines common env vars. There are four layers for env vars' definitions and
// the precedence order is: `original container env vars` > `language specific env vars` > `common env vars` > `instrument spec configs' vars`.
// If the former var had been defined, then the other vars would be ignored.
// +optional
Env []corev1.EnvVar `json:"env,omitempty"`

// Java defines configuration for java auto-instrumentation.
// +optional
Java Java `json:"java,omitempty"`
Expand Down Expand Up @@ -88,20 +95,38 @@ type Java struct {
// Image is a container image with javaagent auto-instrumentation JAR.
// +optional
Image string `json:"image,omitempty"`

// Env defines java specific env vars. There are four layers for env vars' definitions and
// the precedence order is: `original container env vars` > `language specific env vars` > `common env vars` > `instrument spec configs' vars`.
// If the former var had been defined, then the other vars would be ignored.
// +optional
Env []corev1.EnvVar `json:"env,omitempty"`
}

// NodeJS defines NodeJS SDK and instrumentation configuration.
type NodeJS struct {
// Image is a container image with NodeJS SDK and auto-instrumentation.
// +optional
Image string `json:"image,omitempty"`

// Env defines nodejs specific env vars. There are four layers for env vars' definitions and
// the precedence order is: `original container env vars` > `language specific env vars` > `common env vars` > `instrument spec configs' vars`.
// If the former var had been defined, then the other vars would be ignored.
// +optional
Env []corev1.EnvVar `json:"env,omitempty"`
}

// Python defines Python SDK and instrumentation configuration.
type Python struct {
// Image is a container image with Python SDK and auto-instrumentation.
// +optional
Image string `json:"image,omitempty"`

// Env defines python specific env vars. There are four layers for env vars' definitions and
// the precedence order is: `original container env vars` > `language specific env vars` > `common env vars` > `instrument spec configs' vars`.
// If the former var had been defined, then the other vars would be ignored.
// +optional
Env []corev1.EnvVar `json:"env,omitempty"`
}

// InstrumentationStatus defines status of the instrumentation.
Expand Down
27 changes: 27 additions & 0 deletions apis/v1alpha1/instrumentation_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ package v1alpha1
import (
"fmt"
"strconv"
"strings"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
Expand All @@ -28,6 +30,7 @@ const (
AnnotationDefaultAutoInstrumentationJava = "instrumentation.opentelemetry.io/default-auto-instrumentation-java-image"
AnnotationDefaultAutoInstrumentationNodeJS = "instrumentation.opentelemetry.io/default-auto-instrumentation-nodejs-image"
AnnotationDefaultAutoInstrumentationPython = "instrumentation.opentelemetry.io/default-auto-instrumentation-python-image"
envPrefix = "OTEL_"
)

// log is for logging in this package.
Expand Down Expand Up @@ -107,5 +110,29 @@ func (in *Instrumentation) validate() error {
}
case AlwaysOn, AlwaysOff, JaegerRemote, ParentBasedAlwaysOn, ParentBasedAlwaysOff, XRaySampler:
}

// validate env vars
if err := in.validateEnv(in.Spec.Env); err != nil {
return err
}
if err := in.validateEnv(in.Spec.Java.Env); err != nil {
return err
}
if err := in.validateEnv(in.Spec.NodeJS.Env); err != nil {
return err
}
if err := in.validateEnv(in.Spec.Python.Env); err != nil {
return err
}

return nil
}

func (in *Instrumentation) validateEnv(envs []corev1.EnvVar) error {
for _, env := range envs {
if !strings.HasPrefix(env.Name, envPrefix) {
return fmt.Errorf("env name should start with \"OTEL_\": %s", env.Name)
}
}
return nil
}
34 changes: 31 additions & 3 deletions apis/v1alpha1/zz_generated.deepcopy.go

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

Loading

0 comments on commit 2105dad

Please sign in to comment.