otelstack

package module
v1.1.13 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 27, 2025 License: MIT Imports: 10 Imported by: 0

README

OTEL TestContainer stack for go

godoc license Build Status Go Coverage

Package otelstack provides a full OTEL collector and receiver clients conveniently contained within testcontainers. It removes the hassle of managing inter-container communication, has built in querying for validating your tests, and uses lightweight services (seq, Jaeger, and Prometheus with tsdb) to keep startup time low.

Usage

stack := New()
shutdownFunc, err := stack.Start(t.Context())
require.NotNil(t, err, "the stack must start up")

// be sure to defer shutdown of the stack
t.Cleanup(func() {
  if err := shutdownFunc(context.Background()); err != nil {
    t.Logf("error shutting down stack: %w", err)
  }
})

// For optionally setting OTEL_EXPORTER_OTLP_ENDPOINT
stack.SetTestEnv(t)

// ports can be accessed as such
t.Logf("Seq ui: http://localhost:%d", stack.Seq.Ports[80].Int())
t.Logf("Jaeger ui: http://localhost:%d", stack.Seq.Ports[16686].Int())
t.Logf("Prometheus ui: http://localhost:%d", stack.Prometheus.Ports[9090].Int())

t.Logf("OTEL gRPC endpoint: http://localhost:%d", stack.Collector.Ports[4317].Int())

// Continue to initialise your own otel setup here
...

// Get traces from Jaeger
  traces, err := stack.Jaeger.GetTraces(5, serviceName)
  require.NoError(t, err, "must be able to get traces")
  assert.Equal(t, "test-segment", traces.Data[0].Spans[0].OperationName)

  // Get log events from Seq
  events, err := stack.Seq.GetEvents(5)
  require.NoError(t, err)
  assert.Equal(t, "test message", events[0].MessageTemplateTokens[0].Text)

  // Get metrics from Prometheus
  metrics, err := stack.Prometheus.GetMetrics(3, 30, "goroutine_count", serviceName, time.Second*30)
  require.NoError(t, err, "must be able to get metrics")
  assert.Greater(t, metrics.Values[0][0].(float64), 5.0)

Documentation

Overview

Package otelstack provides a full OTEL collector and receiver clients conveniently contained within testcontainers. It removes the hassle of managing inter-container communication, has built in querying for validating your tests, and uses lightweight services (seq and Jaeger) to keep start time low.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Stack added in v1.0.4

type Stack struct {
	Collector  collector.Collector
	Jaeger     jaeger.Jaeger
	Seq        seq.Seq
	Prometheus prometheus.Prometheus
	// contains filtered or unexported fields
}

Stack holds structs containing to all the testcontainers.

func New

func New(metrics bool, logs bool, traces bool) *Stack

New creates a new Stack and populates it with child container structs. Setting the services toggles will disables or enable the respective receiver containers.

func (*Stack) SetTestEnvGRPC added in v1.0.4

func (s *Stack) SetTestEnvGRPC(t *testing.T)

SetTestEnvGRPC sets the environment variableOTEL_EXPORTER_OTLP_ENDPOINT to the gRPC endpoint.

func (*Stack) SetTestEnvHTTP added in v1.0.4

func (s *Stack) SetTestEnvHTTP(t *testing.T)

SetTestEnvHTTP sets the environment variableOTEL_EXPORTER_OTLP_ENDPOINT to the HTTP endpoint

func (*Stack) Start added in v1.0.4

func (s *Stack) Start(ctx context.Context) (func(context.Context) error, error)

Start creates a testcontainer network and starts up all the child containers.

Directories

Path Synopsis
Package collector holds the resources needed to start an OTEL collector testcontainer
Package collector holds the resources needed to start an OTEL collector testcontainer
Package jaeger holds the resources needed to start a Jaeger testcontainer container.
Package jaeger holds the resources needed to start a Jaeger testcontainer container.
Package prometheus holds the resources needed to start a Prometheus testcontainer container.
Package prometheus holds the resources needed to start a Prometheus testcontainer container.
Package request provides functions for making HTTP requests and unmarshaling the response body into a struct.
Package request provides functions for making HTTP requests and unmarshaling the response body into a struct.
Package seq holds the resources needed to start a Seq testcontainer.
Package seq holds the resources needed to start a Seq testcontainer.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL