Skip to content

Commit

Permalink
Added --properties-allowed switch and retries to test. (#89)
Browse files Browse the repository at this point in the history
* Updating OTEL client

* Updated OTEL client

* Fixed newline causing test to fail

* Adding an allow list for properties

* Added default value to make it cleaner

* Removed debug code

* Removed port debugging output

* Improved test reliability

* Switch to waiting for port

* Tidy the mods
  • Loading branch information
ryanrolds authored Dec 20, 2024
1 parent 8f61b5f commit 0b0d658
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Expand Down
28 changes: 27 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"log"
"os"
"runtime"
"slices"
"strings"
"time"

"github.com/joshdk/go-junit"
Expand All @@ -30,21 +32,34 @@ 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")
flag.StringVar(&repositoryPathFlag, "repository-path", getDefaultwd(), "Path to the SCM repository to be read")
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 {
Expand Down Expand Up @@ -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))
}

Expand All @@ -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()...)
}
Expand Down
6 changes: 2 additions & 4 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -177,16 +176,15 @@ func setupRuntimeDependencies(t *testing.T) (context.Context, string, testcontai
ContainerFilePath: "/tmp/tests.json",
},
},
WaitingFor: wait.ForListeningPort("4317/tcp"),
},
Started: true,
})
testcontainers.CleanupContainer(t, otelCollector)
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")
Expand Down

0 comments on commit 0b0d658

Please sign in to comment.