Skip to content

Commit

Permalink
fix: support creating span processor with max batch size (#71)
Browse files Browse the repository at this point in the history
* fix: create span processor with batch size

* chore: make batch size configurable using a command-line flag

* chore: use testing library to set up env vars in tests

* chore: extract creation of test containers to a method

* fix: swap json objects parsing

* fix: swap back

* chore: increase batch size in tests
  • Loading branch information
mdelapenya authored Dec 20, 2024
1 parent 32b827f commit db74cc8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ This tool is able to override the following attributes:

| Attribute | Flag | Default value | Description |
| --------- | ---- | ------------- | ----------- |
| Max Batch Size | --batch-size | `10` | Maximum export batch size allowed when creating a BatchSpanProcessor. |
| Repository Path | --repository-path | `.` | Path to the SCM repository to be read. |
| 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. |
Expand Down
11 changes: 10 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ import (
"go.opentelemetry.io/otel/trace"
)

const defaultMaxBatchSize = 10

var batchSizeFlag int
var repositoryPathFlag string
var serviceNameFlag string
var serviceVersionFlag string
Expand All @@ -31,6 +34,7 @@ var traceNameFlag string
var runtimeAttributes []attribute.KeyValue

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")
Expand Down Expand Up @@ -182,7 +186,12 @@ func initTracerProvider(ctx context.Context, res *resource.Resource) (*sdktrace.

tracerProvider := sdktrace.NewTracerProvider(
sdktrace.WithResource(res),
sdktrace.WithSpanProcessor(sdktrace.NewBatchSpanProcessor(traceExporter)),
sdktrace.WithSpanProcessor(
sdktrace.NewBatchSpanProcessor(
traceExporter,
sdktrace.WithMaxExportBatchSize(batchSizeFlag),
),
),
)

otel.SetTracerProvider(tracerProvider)
Expand Down
16 changes: 12 additions & 4 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,7 @@ func requireAttributeInArray(t *testing.T, attributes []TestAttribute, key strin
return TestAttribute{}
}

func Test_Main_SampleXML(t *testing.T) {
t.Setenv("BRANCH", "main")

func setupRuntimeDependencies(t *testing.T) (context.Context, string, testcontainers.Container) {
ctx := context.Background()

// create file for otel to store the traces
Expand Down Expand Up @@ -197,12 +195,22 @@ func Test_Main_SampleXML(t *testing.T) {
t.Setenv("OTEL_EXPORTER_OTLP_HEADERS", "")
t.Setenv("OTEL_SERVICE_NAME", "jaeger-srv-test")

return ctx, reportFilePath, otelCollector
}

func Test_Main_SampleXML(t *testing.T) {
t.Setenv("BRANCH", "main")
batchSizeFlag = 25

ctx, reportFilePath, otelCollector := setupRuntimeDependencies(t)

defer func() {
batchSizeFlag = defaultMaxBatchSize
// clean up test report
os.Remove(reportFilePath)
}()

err = Main(context.Background(), &TestReader{testFile: "TEST-sample.xml"})
err := Main(context.Background(), &TestReader{testFile: "TEST-sample.xml"})
require.NoError(t, err)

// wait for the file to be written by the otel-exporter
Expand Down

0 comments on commit db74cc8

Please sign in to comment.