Skip to content

Grafana LGTM

Since v0.33.0

Introduction

The Testcontainers module for Grafana LGTM.

Adding this module to your project dependencies

Please run the following command to add the Grafana module to your Go dependencies:

go get github.com/testcontainers/testcontainers-go/modules/grafana-lgtm

Usage example

ctx := context.Background()

grafanaLgtmContainer, err := grafanalgtm.Run(ctx, "grafana/otel-lgtm:0.6.0")
defer func() {
    if err := testcontainers.TerminateContainer(grafanaLgtmContainer); err != nil {
        log.Printf("failed to terminate container: %s", err)
    }
}()
if err != nil {
    log.Printf("failed to start container: %s", err)
    return
}

Module Reference

Run function

Info

The RunContainer(ctx, opts...) function is deprecated and will be removed in the next major release of Testcontainers for Go.

The Grafana LGTM module exposes one entrypoint function to create the Grafana LGTM container, and this function receives three parameters:

func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*GrafanaLGTMContainer, error)
  • context.Context, the Go context.
  • string, the Docker image to use.
  • testcontainers.ContainerCustomizer, a variadic argument for passing options.

Image

Use the second argument in the Run function to set a valid Docker image. In example: Run(context.Background(), "grafana/otel-lgtm:0.6.0").

Container Options

When starting the Grafana LGTM container, you can pass options in a variadic way to configure it.

Admin Credentials

If you need to set different admin credentials in the Grafana LGTM container, you can set them using the WithAdminCredentials(user, password) option.

The following options are exposed by the testcontainers package.

Basic Options

Lifecycle Options

Files & Mounts Options

Build Options

Logging Options

Image Options

Networking Options

Advanced Options

Experimental Options

Container Methods

The Grafana LGTM container exposes the following methods:

Info

All the endpoint methods return their endpoints in the format <host>:<port>, so please use them accordingly to configure your client.

Grafana Endpoint

The HttpEndpoint(ctx) method returns the HTTP endpoint to connect to Grafana, using the default 3000 port. The same method with the Must prefix returns just the endpoint, and panics if an error occurs.

Loki Endpoint

The LokiEndpoint(ctx) method returns the HTTP endpoint to connect to Loki, using the default 3100 port. The same method with the Must prefix returns just the endpoint, and panics if an error occurs.

Tempo Endpoint

The TempoEndpoint(ctx) method returns the HTTP endpoint to connect to Tempo, using the default 3200 port. The same method with the Must prefix returns just the endpoint, and panics if an error occurs.

Otel HTTP Endpoint

The OtelHTTPEndpoint(ctx) method returns the endpoint to connect to Otel using HTTP, using the default 4318 port. The same method with the Must prefix returns just the endpoint, and panics if an error occurs.

Otel gRPC Endpoint

The OtelGRPCEndpoint(ctx) method returns the endpoint to connect to Otel using gRPC, using the default 4317 port. The same method with the Must prefix returns just the endpoint, and panics if an error occurs.

Prometheus Endpoint

The PrometheusHttpEndpoint(ctx) method returns the endpoint to connect to Prometheus, using the default 9090 port. The same method with the Must prefix returns just the endpoint, and panics if an error occurs.

Examples

Traces, Logs and Prometheus metrics for a simple Go process

In this example, a simple application is created to generate traces, logs, and Prometheus metrics. The application sends data to Grafana LGTM, and the Otel SDK is used to send the data. The example demonstrates how to set up the Otel SDK and run the Grafana LGTM module, configuring the Otel library to send data to Grafana LGTM thanks to the endpoints provided by the Grafana LGTM container.

const schemaName = "https://github.com/grafana/docker-otel-lgtm"

var (
    tracer = otel.Tracer(schemaName)
    logger = otelslog.NewLogger(schemaName)
    meter  = otel.Meter(schemaName)
)

func rolldice(ctx context.Context) error {
    ctx, span := tracer.Start(ctx, "roll")
    defer span.End()

    // 20-sided dice
    roll := 1 + rand.Intn(20)
    logger.InfoContext(ctx, fmt.Sprintf("Rolled a dice: %d\n", roll), slog.Int("result", roll))

    opt := metricsapi.WithAttributes(
        attribute.Key("sides").Int(roll),
    )

    // This is the equivalent of prometheus.NewCounterVec
    counter, err := meter.Int64Counter("rolldice-counter", metricsapi.WithDescription("a 20-sided dice"))
    if err != nil {
        return fmt.Errorf("roll dice: %w", err)
    }
    counter.Add(ctx, int64(roll), opt)

    return nil
}
var shutdownFuncs []func(context.Context) error

// shutdown calls cleanup functions registered via shutdownFuncs.
// The errors from the calls are joined.
// Each registered cleanup will be invoked once.
shutdown = func(ctx context.Context) error {
    var errs []error
    for _, fn := range shutdownFuncs {
        if err := fn(ctx); err != nil {
            errs = append(errs, err)
        }
    }

    return errors.Join(errs...)
}

// Ensure that the OpenTelemetry SDK is properly shutdown.
defer func() {
    if err != nil {
        err = errors.Join(shutdown(ctx))
    }
}()

prop := propagation.NewCompositeTextMapPropagator(
    propagation.TraceContext{},
    propagation.Baggage{},
)
otel.SetTextMapPropagator(prop)

otlpHTTPEndpoint := ctr.MustOtlpHttpEndpoint(ctx)

traceExporter, err := otlptrace.New(ctx,
    otlptracehttp.NewClient(
        // adding schema to avoid this error:
        // 2024/07/19 13:16:30 internal_logging.go:50: "msg"="otlptrace: parse endpoint url" "error"="parse \"127.0.0.1:33007\": first path segment in URL cannot contain colon" "url"="127.0.0.1:33007"
        // it does not happen with the logs and metrics exporters
        otlptracehttp.WithEndpointURL("http://"+otlpHTTPEndpoint),
        otlptracehttp.WithInsecure(),
    ),
)
if err != nil {
    return nil, fmt.Errorf("new trace exporter: %w", err)
}

tracerProvider := trace.NewTracerProvider(trace.WithBatcher(traceExporter))
if err != nil {
    return nil, fmt.Errorf("new trace provider: %w", err)
}
shutdownFuncs = append(shutdownFuncs, tracerProvider.Shutdown)
otel.SetTracerProvider(tracerProvider)

metricExporter, err := otlpmetrichttp.New(ctx,
    otlpmetrichttp.WithInsecure(),
    otlpmetrichttp.WithEndpoint(otlpHTTPEndpoint),
)
if err != nil {
    return nil, fmt.Errorf("new metric exporter: %w", err)
}

// The exporter embeds a default OpenTelemetry Reader and
// implements prometheus.Collector, allowing it to be used as
// both a Reader and Collector.
prometheusExporter, err := prometheus.New()
if err != nil {
    return nil, fmt.Errorf("new prometheus exporter: %w", err)
}

meterProvider := metric.NewMeterProvider(
    metric.WithReader(metric.NewPeriodicReader(metricExporter)),
    metric.WithReader(prometheusExporter),
)
if err != nil {
    return nil, fmt.Errorf("new meter provider: %w", err)
}

shutdownFuncs = append(shutdownFuncs, meterProvider.Shutdown)
otel.SetMeterProvider(meterProvider)

logExporter, err := otlploghttp.New(ctx,
    otlploghttp.WithInsecure(),
    otlploghttp.WithEndpoint(otlpHTTPEndpoint),
)
if err != nil {
    return nil, fmt.Errorf("new log exporter: %w", err)
}

loggerProvider := otellog.NewLoggerProvider(otellog.WithProcessor(otellog.NewBatchProcessor(logExporter)))
if err != nil {
    return nil, fmt.Errorf("new logger provider: %w", err)
}

shutdownFuncs = append(shutdownFuncs, loggerProvider.Shutdown)
global.SetLoggerProvider(loggerProvider)

if err = runtime.Start(runtime.WithMinimumReadMemStatsInterval(time.Second)); err != nil {
    return nil, fmt.Errorf("start runtime instrumentation: %w", err)
}

return shutdown, nil
ctx := context.Background()

ctr, err := grafanalgtm.Run(ctx, "grafana/otel-lgtm:0.6.0", grafanalgtm.WithAdminCredentials("admin", "123456789"))
defer func() {
    if err := testcontainers.TerminateContainer(ctr); err != nil {
        log.Printf("failed to terminate container: %s", err)
    }
}()
if err != nil {
    log.Printf("failed to start Grafana LGTM container: %s", err)
    return
}

// Set up OpenTelemetry.
otelShutdown, err := setupOTelSDK(ctx, ctr)
if err != nil {
    log.Printf("failed to set up OpenTelemetry: %s", err)
    return
}
// Handle shutdown properly so nothing leaks.
defer func() {
    if err := otelShutdown(context.Background()); err != nil {
        log.Printf("failed to shutdown OpenTelemetry: %s", err)
    }
}()

// roll dice 10000 times, concurrently
max := 10_000
var wg errgroup.Group
for i := 0; i < max; i++ {
    wg.Go(func() error {
        return rolldice(ctx)
    })
}

if err = wg.Wait(); err != nil {
    log.Printf("failed to roll dice: %s", err)
    return
}

// Output: