Skip to content

Commit

Permalink
add smoke tests for the WithResource config func
Browse files Browse the repository at this point in the history
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
  • Loading branch information
robbkidd committed Jun 1, 2023
1 parent 6365f2d commit 7856679
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
14 changes: 11 additions & 3 deletions examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,28 @@ 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)
}

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()
}
1 change: 1 addition & 0 deletions smoke-tests/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
15 changes: 15 additions & 0 deletions smoke-tests/smoke-sdk-grpc.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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"'
}
9 changes: 9 additions & 0 deletions smoke-tests/smoke-sdk-http.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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"'
}

0 comments on commit 7856679

Please sign in to comment.