Skip to content

Commit

Permalink
Add OTEL_SERVICE_NAME and OTEL_RESOURCE_ATTRIBUTES
Browse files Browse the repository at this point in the history
  • Loading branch information
Starefossen committed Nov 6, 2023
1 parent c379453 commit 9f53102
Showing 2 changed files with 24 additions and 8 deletions.
16 changes: 14 additions & 2 deletions pkg/resourcecreator/observability/observability.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package observability

import (
"fmt"

nais_io_v1 "github.com/nais/liberator/pkg/apis/nais.io/v1"
"github.com/nais/liberator/pkg/namegen"
corev1 "k8s.io/api/core/v1"
@@ -18,13 +20,23 @@ type Source interface {

// Standard environment variable names from https://opentelemetry.io/docs/specs/otel/protocol/exporter/
// These are hard-coded because they are the same across installations, feel free to make them configurable.
const otelServiceName = "OTEL_SERVICE_NAME"
const otelResourceAttributes = "OTEL_RESOURCE_ATTRIBUTES"
const otelExporterEndpoint = "OTEL_EXPORTER_OTLP_ENDPOINT"
const collectorEndpoint = "http://tempo-distributor.nais-system:4317"
const otelExporterProtocol = "OTEL_EXPORTER_OTLP_PROTOCOL"
const collectorProtocol = "grpc"

func envVars() []corev1.EnvVar {
func envVars(source Source) []corev1.EnvVar {
return []corev1.EnvVar{
{
Name: otelServiceName,
Value: source.GetName(),
},
{
Name: otelResourceAttributes,
Value: fmt.Sprintf("service.name=%s,service.namespace=%s", source.GetName(), source.GetNamespace()),
},
{
Name: otelExporterEndpoint,
Value: collectorEndpoint,
@@ -93,7 +105,7 @@ func Create(source Source, ast *resource.Ast, _ any) error {
return err
}

ast.Env = append(ast.Env, envVars()...)
ast.Env = append(ast.Env, envVars(source)...)
ast.AppendOperation(resource.OperationCreateOrUpdate, np)

return nil
16 changes: 10 additions & 6 deletions pkg/resourcecreator/testdata/observability_tracing_enabled.yaml
Original file line number Diff line number Diff line change
@@ -32,6 +32,10 @@ tests:
- image: navikt/myapplication:1.2.3
name: myapplication
env:
- name: OTEL_SERVICE_NAME
value: myapplication
- name: OTEL_RESOURCE_ATTRIBUTES
value: service.name=myapplication,service.namespace=mynamespace
- name: OTEL_EXPORTER_OTLP_ENDPOINT
value: http://tempo-distributor.nais-system:4317
- name: OTEL_EXPORTER_OTLP_PROTOCOL
@@ -48,9 +52,9 @@ tests:
spec:
egress:
- to:
- namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: nais-system
podSelector:
matchLabels:
app.kubernetes.io/instance: tempo
- namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: nais-system
podSelector:
matchLabels:
app.kubernetes.io/instance: tempo

0 comments on commit 9f53102

Please sign in to comment.