From 78566790a76f81c510b817be6d415490f058600d Mon Sep 17 00:00:00 2001 From: Robb Kidd Date: Thu, 1 Jun 2023 17:06:40 -0400 Subject: [PATCH] add smoke tests for the WithResource config func Per the OTel spec, resource attributes set via environment variable must supercede resource attributes set in code.[1] [1] https://opentelemetry.io/docs/specs/otel/resource/sdk/#specifying-resource-information-via-an-environment-variable --- examples/main.go | 14 +++++++++++--- smoke-tests/docker-compose.yml | 1 + smoke-tests/smoke-sdk-grpc.bats | 15 +++++++++++++++ smoke-tests/smoke-sdk-http.bats | 9 +++++++++ 4 files changed, 36 insertions(+), 3 deletions(-) diff --git a/examples/main.go b/examples/main.go index e83c3d2..246c9d9 100644 --- a/examples/main.go +++ b/examples/main.go @@ -5,12 +5,21 @@ import ( "log" "go.opentelemetry.io/otel" + "go.opentelemetry.io/otel/attribute" + "go.opentelemetry.io/otel/sdk/resource" "github.com/honeycombio/otel-config-go/otelconfig" ) func main() { - otelShutdown, err := otelconfig.ConfigureOpenTelemetry() + otelShutdown, err := otelconfig.ConfigureOpenTelemetry( + otelconfig.WithResourceOption( + resource.WithAttributes( + attribute.String("resource.example_set_in_code", "CODE"), + attribute.String("resource.example_clobber", "CODE_WON"), + ), + ), + ) if err != nil { log.Fatalf("error setting up OTel SDK - %e", err) @@ -18,7 +27,6 @@ func main() { defer otelShutdown() tracer := otel.Tracer("my-app") - ctx := context.Background() - ctx, span := tracer.Start(ctx, "doing-things") + _, span := tracer.Start(context.Background(), "doing-things") defer span.End() } diff --git a/smoke-tests/docker-compose.yml b/smoke-tests/docker-compose.yml index 32bbc34..c47fd7f 100644 --- a/smoke-tests/docker-compose.yml +++ b/smoke-tests/docker-compose.yml @@ -3,6 +3,7 @@ version: '3.0' x-env-base: &env_base OTEL_EXPORTER_OTLP_ENDPOINT: http://collector:4317 OTEL_EXPORTER_OTLP_INSECURE: "true" + OTEL_RESOURCE_ATTRIBUTES: resource.example_set_in_env=ENV,resource.example_clobber=ENV_WON OTEL_SERVICE_NAME: "my-go-app" OTEL_METRICS_ENABLED: "false" DEBUG: "true" diff --git a/smoke-tests/smoke-sdk-grpc.bats b/smoke-tests/smoke-sdk-grpc.bats index d4b024d..dd710d0 100644 --- a/smoke-tests/smoke-sdk-grpc.bats +++ b/smoke-tests/smoke-sdk-grpc.bats @@ -27,3 +27,18 @@ teardown_file() { result=$(span_names_for ${TRACER_NAME}) assert_equal "$result" '"doing-things"' } + +@test "Resource attributes can be set via environment variable" { + env_result=$(spans_received | jq ".resource.attributes[] | select(.key == \"resource.example_set_in_env\") | .value.stringValue") + assert_equal "$env_result" '"ENV"' +} + +@test "Resource attributes can be set in code" { + code_result=$(spans_received | jq ".resource.attributes[] | select(.key == \"resource.example_set_in_code\") | .value.stringValue") + assert_equal "$code_result" '"CODE"' +} + +@test "Resource attributes set in environment win over matching key set in code" { + code_result=$(spans_received | jq ".resource.attributes[] | select(.key == \"resource.example_clobber\") | .value.stringValue") + assert_equal "$code_result" '"ENV_WON"' +} diff --git a/smoke-tests/smoke-sdk-http.bats b/smoke-tests/smoke-sdk-http.bats index 4d0c05d..c502bdf 100644 --- a/smoke-tests/smoke-sdk-http.bats +++ b/smoke-tests/smoke-sdk-http.bats @@ -27,3 +27,12 @@ teardown_file() { result=$(span_names_for ${TRACER_NAME}) assert_equal "$result" '"doing-things"' } + +@test "Resource attributes can be set in environment or in code, environment wins in a key clobber" { + env_result=$(spans_received | jq ".resource.attributes[] | select(.key == \"resource.example_set_in_env\") | .value.stringValue") + assert_equal "$env_result" '"ENV"' + code_result=$(spans_received | jq ".resource.attributes[] | select(.key == \"resource.example_set_in_code\") | .value.stringValue") + assert_equal "$code_result" '"CODE"' + code_result=$(spans_received | jq ".resource.attributes[] | select(.key == \"resource.example_clobber\") | .value.stringValue") + assert_equal "$code_result" '"ENV_WON"' +}