Skip to content

Commit 9fb669c

Browse files
authored
Merge pull request #273 from asteurer/otel
adding otel capabilities
2 parents f73395a + 195e95c commit 9fb669c

File tree

7 files changed

+136
-4
lines changed

7 files changed

+136
-4
lines changed

api/v1alpha1/spinappexecutor_types.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ type ExecutorDeploymentConfig struct {
4848
// will be created containing the certificates. If no secret name is
4949
// defined in `CACertSecret` the secret name will be `spin-ca`.
5050
InstallDefaultCACerts bool `json:"installDefaultCACerts,omitempty"`
51+
52+
// Otel provides Kubernetes Bindings to Otel Variables.
53+
Otel *OtelConfig `json:"otel,omitempty"`
5154
}
5255

5356
// SpinAppExecutorStatus defines the observed state of SpinAppExecutor
@@ -56,6 +59,18 @@ type SpinAppExecutorStatus struct {
5659
// Important: Run "make" to regenerate code after modifying this file
5760
}
5861

62+
// OtelConfig is the supported environment variables for OpenTelemetry
63+
type OtelConfig struct {
64+
// ExporterOtlpEndpoint configures the default combined otlp endpoint for sending telemetry
65+
ExporterOtlpEndpoint string `json:"exporter_otlp_endpoint,omitempty"`
66+
// ExporterOtlpTracesEndpoint configures the trace-specific otlp endpoint
67+
ExporterOtlpTracesEndpoint string `json:"exporter_otlp_traces_endpoint,omitempty"`
68+
// ExporterOtlpMetricsEndpoint configures the metrics-specific otlp endpoint
69+
ExporterOtlpMetricsEndpoint string `json:"exporter_otlp_metrics_endpoint,omitempty"`
70+
// ExporterOtlpLogsEndpoint configures the logs-specific otlp endpoint
71+
ExporterOtlpLogsEndpoint string `json:"exporter_otlp_logs_endpoint,omitempty"`
72+
}
73+
5974
//+kubebuilder:object:root=true
6075
//+kubebuilder:subresource:status
6176

api/v1alpha1/zz_generated.deepcopy.go

Lines changed: 21 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/core.spinoperator.dev_spinappexecutors.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,26 @@ spec:
6161
will be created containing the certificates. If no secret name is
6262
defined in `CACertSecret` the secret name will be `spin-ca`.
6363
type: boolean
64+
otel:
65+
description: Otel provides Kubernetes Bindings to Otel Variables.
66+
properties:
67+
exporter_otlp_endpoint:
68+
description: ExporterOtlpEndpoint configures the default combined
69+
otlp endpoint for sending telemetry
70+
type: string
71+
exporter_otlp_logs_endpoint:
72+
description: ExporterOtlpLogsEndpoint configures the logs-specific
73+
otlp endpoint
74+
type: string
75+
exporter_otlp_metrics_endpoint:
76+
description: ExporterOtlpMetricsEndpoint configures the metrics-specific
77+
otlp endpoint
78+
type: string
79+
exporter_otlp_traces_endpoint:
80+
description: ExporterOtlpTracesEndpoint configures the trace-specific
81+
otlp endpoint
82+
type: string
83+
type: object
6484
runtimeClassName:
6585
description: |-
6686
RuntimeClassName is the runtime class name that should be used by pods created

config/samples/otel.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
apiVersion: core.spinoperator.dev/v1alpha1
2+
kind: SpinApp
3+
metadata:
4+
name: otel-spinapp
5+
spec:
6+
image: ghcr.io/spinkube/spin-operator/cpu-load-gen:20240311-163328-g1121986
7+
executor: otel-shim-executor
8+
replicas: 1
9+
---
10+
apiVersion: core.spinoperator.dev/v1alpha1
11+
kind: SpinAppExecutor
12+
metadata:
13+
name: otel-shim-executor
14+
spec:
15+
createDeployment: true
16+
deploymentConfig:
17+
runtimeClassName: wasmtime-spin-v2
18+
installDefaultCACerts: true
19+
otel:
20+
exporter_otlp_endpoint: http://otel-collector.default.svc.cluster.local:4318

internal/controller/deployment.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,9 @@ func ConstructVolumeMountsForApp(ctx context.Context, app *spinv1alpha1.SpinApp,
103103

104104
// ConstructEnvForApp constructs the env for a spin app that runs as a k8s pod.
105105
// Variables are not guaranteed to stay backed by ENV.
106-
func ConstructEnvForApp(ctx context.Context, app *spinv1alpha1.SpinApp, listenPort int) []corev1.EnvVar {
106+
func ConstructEnvForApp(ctx context.Context, app *spinv1alpha1.SpinApp, listenPort int, otel *spinv1alpha1.OtelConfig) []corev1.EnvVar {
107107
envs := make([]corev1.EnvVar, len(app.Spec.Variables))
108+
// Adding the Spin Variables
108109
for idx, variable := range app.Spec.Variables {
109110
env := corev1.EnvVar{
110111
// Spin Variables only allow lowercase ascii characters, `_`, and numbers.
@@ -121,6 +122,24 @@ func ConstructEnvForApp(ctx context.Context, app *spinv1alpha1.SpinApp, listenPo
121122
Name: "SPIN_HTTP_LISTEN_ADDR",
122123
Value: fmt.Sprintf("0.0.0.0:%d", listenPort),
123124
})
125+
// Adding the OpenTelemetry params
126+
if otel != nil {
127+
if otel.ExporterOtlpEndpoint != "" {
128+
envs = append(envs, corev1.EnvVar{Name: "OTEL_EXPORTER_OTLP_ENDPOINT", Value: otel.ExporterOtlpEndpoint})
129+
}
130+
131+
if otel.ExporterOtlpTracesEndpoint != "" {
132+
envs = append(envs, corev1.EnvVar{Name: "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT", Value: otel.ExporterOtlpTracesEndpoint})
133+
}
134+
135+
if otel.ExporterOtlpMetricsEndpoint != "" {
136+
envs = append(envs, corev1.EnvVar{Name: "OTEL_EXPORTER_OTLP_METRICS_ENDPOINT", Value: otel.ExporterOtlpMetricsEndpoint})
137+
}
138+
139+
if otel.ExporterOtlpLogsEndpoint != "" {
140+
envs = append(envs, corev1.EnvVar{Name: "OTEL_EXPORTER_OTLP_LOGS_ENDPOINT", Value: otel.ExporterOtlpLogsEndpoint})
141+
}
142+
}
124143

125144
return envs
126145
}

internal/controller/deployment_test.go

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ func TestConstructEnvForApp(t *testing.T) {
108108

109109
value string
110110
valueFrom *corev1.EnvVarSource
111+
112+
expectedOtelVars map[string]string
113+
otelVars spinv1alpha1.OtelConfig
111114
}{
112115
{
113116
name: "simple_secret_with_static_value",
@@ -143,6 +146,24 @@ func TestConstructEnvForApp(t *testing.T) {
143146
},
144147
},
145148
},
149+
{
150+
name: "otel_vars_are_properly_set",
151+
varName: "simple_secret",
152+
expectedEnvName: "SPIN_VARIABLE_SIMPLE_SECRET",
153+
value: "f00",
154+
otelVars: spinv1alpha1.OtelConfig{
155+
ExporterOtlpEndpoint: "http://otlp",
156+
ExporterOtlpTracesEndpoint: "http://traces",
157+
ExporterOtlpMetricsEndpoint: "http://metrics",
158+
ExporterOtlpLogsEndpoint: "http://logs",
159+
},
160+
expectedOtelVars: map[string]string{
161+
"OTEL_EXPORTER_OTLP_ENDPOINT": "http://otlp",
162+
"OTEL_EXPORTER_OTLP_TRACES_ENDPOINT": "http://traces",
163+
"OTEL_EXPORTER_OTLP_METRICS_ENDPOINT": "http://metrics",
164+
"OTEL_EXPORTER_OTLP_LOGS_ENDPOINT": "http://logs",
165+
},
166+
},
146167
}
147168

148169
for _, test := range tests {
@@ -156,11 +177,28 @@ func TestConstructEnvForApp(t *testing.T) {
156177
},
157178
}
158179

159-
envs := ConstructEnvForApp(context.Background(), app, 0)
180+
envs := ConstructEnvForApp(context.Background(), app, 0, &test.otelVars)
160181

161182
require.Equal(t, test.expectedEnvName, envs[0].Name)
162183
require.Equal(t, test.value, envs[0].Value)
163184
require.Equal(t, test.valueFrom, envs[0].ValueFrom)
185+
186+
for key, value := range test.expectedOtelVars {
187+
varNotFound := true
188+
for _, envVar := range envs {
189+
if envVar.Name == key {
190+
varNotFound = false
191+
if envVar.Value != value {
192+
require.Equal(t, test.value, envVar.Value)
193+
break
194+
}
195+
}
196+
}
197+
198+
if varNotFound {
199+
require.NotContains(t, test.expectedOtelVars, key)
200+
}
201+
}
164202
})
165203
}
166204
}

internal/controller/spinapp_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ func constructDeployment(ctx context.Context, app *spinv1alpha1.SpinApp, config
426426
Requests: app.Spec.Resources.Requests,
427427
}
428428

429-
env := ConstructEnvForApp(ctx, app, spinapp.DefaultHTTPPort)
429+
env := ConstructEnvForApp(ctx, app, spinapp.DefaultHTTPPort, config.Otel)
430430

431431
readinessProbe, livenessProbe, err := ConstructPodHealthChecks(app)
432432
if err != nil {

0 commit comments

Comments
 (0)