diff --git a/README.md b/README.md index 893b44f..c33b603 100644 --- a/README.md +++ b/README.md @@ -146,6 +146,7 @@ This tool is able to override the following attributes: | Service Name | --service-name | `junit2otlp` | Overrides OpenTelemetry's service name. If the `OTEL_SERVICE_NAME` environment variable is set, it will take precedence over any other value. | | 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. | 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 4b5159d..c50bff5 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,8 @@ import ( "log" "os" "runtime" + "slices" + "strings" "time" "github.com/joshdk/go-junit" @@ -30,8 +32,12 @@ var repositoryPathFlag string var serviceNameFlag string var serviceVersionFlag string var traceNameFlag string +var propertiesAllowedString string + +const propertiesAllowAll = "all" var runtimeAttributes []attribute.KeyValue +var propsAllowed []string func init() { flag.IntVar(&batchSizeFlag, "batch-size", defaultMaxBatchSize, "Maximum export batch size allowed when creating a BatchSpanProcessor") @@ -39,12 +45,21 @@ func init() { flag.StringVar(&serviceNameFlag, "service-name", "", "OpenTelemetry Service Name to be used when sending traces and metrics for the jUnit report") 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") - // initialise runtime keys + // initialize runtime keys runtimeAttributes = []attribute.KeyValue{ semconv.HostArchKey.String(runtime.GOARCH), semconv.OSNameKey.String(runtime.GOOS), } + + propsAllowed = []string{} + if propertiesAllowedString != "" { + allowed := strings.Split(propertiesAllowedString, ",") + for _, prop := range allowed { + propsAllowed = append(propsAllowed, strings.TrimSpace(prop)) + } + } } func createIntCounter(meter metric.Meter, name string, description string) metric.Int64Counter { @@ -202,6 +217,13 @@ func initTracerProvider(ctx context.Context, res *resource.Resource) (*sdktrace. func propsToLabels(props map[string]string) []attribute.KeyValue { attributes := []attribute.KeyValue{} for k, v := range props { + // if propertiesAllowedString is not "all" (default) and the key is not in the + // allowed list, skip it + if propertiesAllowedString != propertiesAllowAll && + len(propsAllowed) > 0 && !slices.Contains(propsAllowed, k) { + continue + } + attributes = append(attributes, attribute.Key(k).String(v)) } @@ -224,6 +246,10 @@ func (pr *PipeReader) Read() ([]byte, error) { var buf []byte scanner := bufio.NewScanner(os.Stdin) + // 64KB initial buffer, 1MB max buffer size + // was seeing large failure messages causing parsing to fail + scanner.Buffer(make([]byte, 0, 64*1024), 1024*1024) + for scanner.Scan() { buf = append(buf, scanner.Bytes()...) } diff --git a/main_test.go b/main_test.go index c2f4ba7..34f9671 100644 --- a/main_test.go +++ b/main_test.go @@ -13,7 +13,6 @@ import ( "time" "github.com/stretchr/testify/require" - "github.com/testcontainers/testcontainers-go" "github.com/testcontainers/testcontainers-go/network" "github.com/testcontainers/testcontainers-go/wait" @@ -177,6 +176,7 @@ func setupRuntimeDependencies(t *testing.T) (context.Context, string, testcontai ContainerFilePath: "/tmp/tests.json", }, }, + WaitingFor: wait.ForListeningPort("4317/tcp"), }, Started: true, }) @@ -184,9 +184,7 @@ func setupRuntimeDependencies(t *testing.T) (context.Context, string, testcontai require.NoError(t, err) collectorPort, err := otelCollector.MappedPort(ctx, "4317/tcp") - if err != nil { - t.Errorf("could not get mapped port for otel-collector: %v", err) - } + require.NoError(t, err) t.Setenv(exporterEndpointKey, "http://localhost:"+collectorPort.Port()) t.Setenv("OTEL_EXPORTER_OTLP_SPAN_INSECURE", "true")