From 6489572f3f7673c105cb18cb1d1ebe480cca8911 Mon Sep 17 00:00:00 2001 From: Jamie Magee Date: Wed, 25 Oct 2023 12:05:40 -0700 Subject: [PATCH] Set otel export url --- internal/infra/open_telemetry.go | 13 +++++++++++++ internal/infra/run.go | 5 +++-- internal/infra/updater.go | 9 +++++++-- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/internal/infra/open_telemetry.go b/internal/infra/open_telemetry.go index b59da0c..1e25869 100644 --- a/internal/infra/open_telemetry.go +++ b/internal/infra/open_telemetry.go @@ -8,6 +8,7 @@ import ( "github.com/docker/docker/api/types/mount" "github.com/docker/docker/api/types/network" "github.com/moby/moby/client" + "log" "os" "path" "path/filepath" @@ -23,6 +24,7 @@ const sslCertificates = "/etc/ssl/certs/ca-certificates.crt" type Collector struct { cli *client.Client containerID string + url string } // NewCollector starts the OpenTelemetry collector container. @@ -87,6 +89,17 @@ func NewCollector(ctx context.Context, cli *client.Client, net *Networks, params return nil, fmt.Errorf("failed to start collector container: %w", err) } + containerInfo, err := cli.ContainerInspect(ctx, collector.containerID) + if err != nil { + return nil, fmt.Errorf("failed to inspect collector container: %w", err) + } + if net != nil { + collector.url = fmt.Sprintf("http://%s:4318", containerInfo.NetworkSettings.Networks[net.noInternetName].IPAddress) + } else { + // This should only happen during testing, adding a warning in case + log.Println("Warning: no-internet network not found") + } + return collector, nil } diff --git a/internal/infra/run.go b/internal/infra/run.go index d1f83b4..634b9e2 100644 --- a/internal/infra/run.go +++ b/internal/infra/run.go @@ -363,15 +363,16 @@ func runContainers(ctx context.Context, params RunParams, api *server.API) error go prox.TailLogs(ctx, cli) } + var collector *Collector if params.CollectorConfigPath != "" { - collector, err := NewCollector(ctx, cli, networks, ¶ms, prox) + collector, err = NewCollector(ctx, cli, networks, ¶ms, prox) if err != nil { return err } defer collector.Close() } - updater, err := NewUpdater(ctx, cli, networks, ¶ms, prox) + updater, err := NewUpdater(ctx, cli, networks, ¶ms, prox, collector) if err != nil { return err } diff --git a/internal/infra/updater.go b/internal/infra/updater.go index 533a0be..2ce5e57 100644 --- a/internal/infra/updater.go +++ b/internal/infra/updater.go @@ -46,7 +46,7 @@ const ( ) // NewUpdater starts the update container interactively running /bin/sh, so it does not stop. -func NewUpdater(ctx context.Context, cli *client.Client, net *Networks, params *RunParams, prox *Proxy) (*Updater, error) { +func NewUpdater(ctx context.Context, cli *client.Client, net *Networks, params *RunParams, prox *Proxy, collector *Collector) (*Updater, error) { containerCfg := &container.Config{ User: dependabot, Image: params.UpdaterImage, @@ -55,7 +55,12 @@ func NewUpdater(ctx context.Context, cli *client.Client, net *Networks, params * } if params.CollectorConfigPath != "" { - containerCfg.Env = append(containerCfg.Env, "OTEL_ENABLED=true") + containerCfg.Env = append( + containerCfg.Env, + []string{ + "OTEL_ENABLED=true", + fmt.Sprintf("OTEL_EXPORTER_OTLP_ENDPOINT=%s", collector.url), + }...) } hostCfg := &container.HostConfig{}