Skip to content

Commit

Permalink
Set otel export url (#184)
Browse files Browse the repository at this point in the history
  • Loading branch information
JamieMagee authored Oct 25, 2023
1 parent dfcbba8 commit b8df176
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
13 changes: 13 additions & 0 deletions internal/infra/open_telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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.
Expand Down Expand Up @@ -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

}
Expand Down
5 changes: 3 additions & 2 deletions internal/infra/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, &params, prox)
collector, err = NewCollector(ctx, cli, networks, &params, prox)
if err != nil {
return err
}
defer collector.Close()
}

updater, err := NewUpdater(ctx, cli, networks, &params, prox)
updater, err := NewUpdater(ctx, cli, networks, &params, prox, collector)
if err != nil {
return err
}
Expand Down
9 changes: 7 additions & 2 deletions internal/infra/updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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{}
Expand Down

0 comments on commit b8df176

Please sign in to comment.