Skip to content

Commit

Permalink
Added --additional-attributes flag and adjusted test running (mdelape…
Browse files Browse the repository at this point in the history
…nya#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
  • Loading branch information
ryanrolds authored Jan 2, 2025
1 parent a92b417 commit 121f951
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 ./...
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Expand Down
23 changes: 23 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"bufio"
"context"
"errors"
"flag"
"fmt"
"log"
Expand Down Expand Up @@ -33,6 +34,7 @@ var serviceNameFlag string
var serviceVersionFlag string
var traceNameFlag string
var propertiesAllowedString string
var additionalAttributes string

const propertiesAllowAll = "all"

Expand 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{
Expand Down Expand Up @@ -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),
Expand Down

0 comments on commit 121f951

Please sign in to comment.