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¶
- Since v0.33.0
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¶
- Since v0.33.0
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¶
WithExposedPorts
Since v0.37.0WithEnv
Since v0.29.0WithWaitStrategy
Since v0.20.0WithAdditionalWaitStrategy
Not available until the next release mainWithWaitStrategyAndDeadline
Since v0.20.0WithAdditionalWaitStrategyAndDeadline
Not available until the next release mainWithEntrypoint
Since v0.37.0WithEntrypointArgs
Since v0.37.0WithCmd
Since v0.37.0WithCmdArgs
Since v0.37.0WithLabels
Since v0.37.0
Lifecycle Options¶
WithLifecycleHooks
Not available until the next release mainWithAdditionalLifecycleHooks
Not available until the next release mainWithStartupCommand
Since v0.25.0WithAfterReadyCommand
Since v0.28.0
Files & Mounts Options¶
WithFiles
Since v0.37.0WithMounts
Since v0.37.0WithTmpfs
Since v0.37.0WithImageMount
Since v0.37.0
Build Options¶
WithDockerfile
Since v0.37.0
Logging Options¶
WithLogConsumers
Since v0.28.0WithLogConsumerConfig
Not available until the next release mainWithLogger
Since v0.29.0
Image Options¶
WithAlwaysPull
Not available until the next release mainWithImageSubstitutors
Since v0.26.0WithImagePlatform
Not available until the next release main
Networking Options¶
WithNetwork
Since v0.27.0WithNetworkByName
Not available until the next release mainWithBridgeNetwork
Not available until the next release mainWithNewNetwork
Since v0.27.0
Advanced Options¶
WithHostPortAccess
Since v0.31.0WithConfigModifier
Since v0.20.0WithHostConfigModifier
Since v0.20.0WithEndpointSettingsModifier
Since v0.20.0CustomizeRequest
Since v0.20.0WithName
Not available until the next release mainWithNoStart
Not available until the next release main
Experimental Options¶
WithReuseByName
Since v0.37.0
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¶
- Since v0.33.0
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¶
- Since v0.33.0
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¶
- Since v0.33.0
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¶
- Since v0.33.0
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¶
- Since v0.33.0
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¶
- Since v0.33.0
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: