From 121f951b2ec644b7e9fae485e2bb5a546bff2c35 Mon Sep 17 00:00:00 2001 From: Ryan Olds Date: Thu, 2 Jan 2025 11:44:07 -0800 Subject: [PATCH] Added --additional-attributes flag and adjusted test running (#102) * Added --add-attrbitues flag and adjusted test running * Removed debug output * Moved flag parsing and the add attributes processing to a better location * Added doc line * Fixed documentation and added fatal exit when invalid attribute value * Fixed error handling and adjusted names of flag and var * Collect attribute errors and return single error with all validation issues * Switching to errors.Join --- .github/workflows/tests.yml | 2 +- Makefile | 3 +++ README.md | 1 + main.go | 23 +++++++++++++++++++++++ 4 files changed, 28 insertions(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 808cf56..ec24001 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -23,4 +23,4 @@ jobs: id: go - name: Run Unit tests - run: go run gotest.tools/gotestsum --format short-verbose -- -timeout=5m ./... + run: make test diff --git a/Makefile b/Makefile index 92eda84..ae06414 100644 --- a/Makefile +++ b/Makefile @@ -51,3 +51,6 @@ demo-start-zipkin: demo-stop-zipkin: $(call stop_demo,zipkin) + +test: + go run gotest.tools/gotestsum --debug --format short-verbose -- -timeout=5m ./... diff --git a/README.md b/README.md index b4f0d09..46f106d 100644 --- a/README.md +++ b/README.md @@ -147,6 +147,7 @@ This tool is able to override the following attributes: | Service Version | --service-version | Empty | Overrides OpenTelemetry's service version. If the `OTEL_SERVICE_VERSION` environment variable is set, it will take precedence over any other value. | | Trace Name | --trace-name | `junit2otlp` | Overrides OpenTelemetry's trace name. | | Properties Allowed | --properties-allowed | All | Comma separated list of properties to be allowed in the jUnit report. | +| Additional Attributes | --additional-attributes | Empty | Comma separated list of attributes to be added to the jUnit report. | For using this tool in a distributed tracing scenario, where there is a parent trace in which the test reports traces should be attached, it's important to set the `TRACEPARENT` environment variable, so that the traces and spans generated by this tool are located under the right parent trace. Please read more on this [here](https://github.com/open-telemetry/opentelemetry-specification/issues/740). diff --git a/main.go b/main.go index c50bff5..332726c 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "bufio" "context" + "errors" "flag" "fmt" "log" @@ -33,6 +34,7 @@ var serviceNameFlag string var serviceVersionFlag string var traceNameFlag string var propertiesAllowedString string +var additionalAttributes string const propertiesAllowAll = "all" @@ -46,6 +48,7 @@ func init() { flag.StringVar(&serviceVersionFlag, "service-version", "", "OpenTelemetry Service Version to be used when sending traces and metrics for the jUnit report") flag.StringVar(&traceNameFlag, "trace-name", Junit2otlp, "OpenTelemetry Trace Name to be used when sending traces and metrics for the jUnit report") flag.StringVar(&propertiesAllowedString, "properties-allowed", propertiesAllowAll, "Comma separated list of properties to be allowed in the jUnit report") + flag.StringVar(&additionalAttributes, "additional-attributes", "", "Comma separated list of attributes to be added to the jUnit report") // initialize runtime keys runtimeAttributes = []attribute.KeyValue{ @@ -270,6 +273,26 @@ func Main(ctx context.Context, reader InputReader) error { ctx = initOtelContext(ctx) + // add additional attributes if provided to the runtime attributes + if additionalAttributes != "" { + additionalAttrsErrors := []error{} + + addAttrs := strings.Split(additionalAttributes, ",") + for _, attr := range addAttrs { + kv := strings.Split(attr, "=") + if len(kv) == 2 { + runtimeAttributes = append(runtimeAttributes, attribute.Key(kv[0]).String(kv[1])) + } else { + additionalAttrsErrors = append(additionalAttrsErrors, + fmt.Errorf("invalid attribute: %s", attr)) + } + } + + if err := errors.Join(additionalAttrsErrors...); err != nil { + return fmt.Errorf("failed to add additional attributes: %w", err) + } + } + // set the service name that will show up in tracing UIs resAttrs := resource.WithAttributes( semconv.ServiceNameKey.String(otlpSrvName),