apiserverspanexporter_test.go

Overview

This file contains unit tests for the APIServerSpanExporter component, which is responsible for exporting OpenTelemetry spans in the context of API server telemetry. The tests ensure that spans are correctly captured, filtered, and stored based on event IDs and span names. It also verifies the correct initialization and shutdown behavior of the exporter. The file includes a custom in-memory span exporter used to capture spans during tests.

Detailed Explanation of Components

capturingExporter

A test utility type implementing the OpenTelemetry SpanExporter interface to capture spans in memory for assertions.

type capturingExporter struct {
	spans []sdktrace.ReadOnlySpan
}

Usage:
Used within tests to intercept spans created by a tracer provider, allowing verification of span export behavior without external dependencies.


TestNewAPIServerSpanExporter

Tests the creation of a new APIServerSpanExporter instance.

Example:

exporter := NewAPIServerSpanExporter()
if exporter == nil {
	t.Fatal("NewAPIServerSpanExporter returned nil")
}
if exporter.GetTraceDict() == nil {
	t.Error("traceDict should be non-nil")
}

TestAPIServerSpanExporterExportSpans

Tests the span export functionality of the APIServerSpanExporter using various scenarios.

Example Test Case:

{
	name:     "call_llm-with-event-id-saved",
	spanName: "call_llm",
	attributes: []attribute.KeyValue{
		attribute.String("gcp.vertex.agent.event_id", "event-id"),
	},
	expectedEvent: true,
}

TestAPIServerSpanExporterShutdown

Tests that the Shutdown method of APIServerSpanExporter executes without error.

Example:

exporter := NewAPIServerSpanExporter()
if err := exporter.Shutdown(context.Background()); err != nil {
	t.Errorf("Shutdown() error = %v, wantErr nil", err)
}

Important Implementation Details


Interaction with Other Parts of the System


Visual Diagram: Flowchart of Test Relationships and Span Export Flow

flowchart TD
A[Start Test] --> B[Create capturingExporter]
B --> C[Initialize TracerProvider with capturingExporter]
C --> D[Start Span with name & attributes]
D --> E[End Span]
E --> F["Shutdown TracerProvider (flush spans)"]
F --> G[Create APIServerSpanExporter]
G --> H[Call ExportSpans with captured spans]
H --> I[Check traceDict contents]
I --> J{Expected Event?}
J -- Yes --> K[Assert traceDict has 1 item with event ID]
J -- No --> L[Assert traceDict is empty]
K & L --> M[End Test]

Summary of Provided Functions and Methods

Function/Method

Parameters

Returns

Description

(*capturingExporter) ExportSpans

ctx context.Context, spans []sdktrace.ReadOnlySpan

error

Stores spans into internal slice for inspection.

(*capturingExporter) Shutdown

ctx context.Context

error

No-op shutdown method.

TestNewAPIServerSpanExporter

t *testing.T

Tests creation and initialization of the API server exporter.

TestAPIServerSpanExporterExportSpans

t *testing.T

Tests span export behavior with various span names and attributes.

TestAPIServerSpanExporterShutdown

t *testing.T

Tests shutdown method completes without error.


Usage Examples

Although this is a test file and not intended for direct usage outside testing, the test cases provide usage patterns for how to:

This aligns with typical OpenTelemetry span capturing and exporting workflows found in telemetry systems [OpenTelemetry Integration](80596).


End of Documentation for apiserverspanexporter_test.go