fix(otel): accept full https:// URL in OTEL_EXPORTER_OTLP_ENDPOINT
Some checks failed
Release / Check ui (push) Successful in 39s
Release / Docker / caddy (push) Successful in 1m4s
Release / Test backend (push) Successful in 1m38s
Release / Docker / runner (push) Successful in 39s
Release / Docker / backend (push) Successful in 2m12s
Release / Docker / ui (push) Successful in 1m0s
Release / Upload source maps (push) Failing after 5m19s
Release / Gitea Release (push) Has been skipped
Some checks failed
Release / Check ui (push) Successful in 39s
Release / Docker / caddy (push) Successful in 1m4s
Release / Test backend (push) Successful in 1m38s
Release / Docker / runner (push) Successful in 39s
Release / Docker / backend (push) Successful in 2m12s
Release / Docker / ui (push) Successful in 1m0s
Release / Upload source maps (push) Failing after 5m19s
Release / Gitea Release (push) Has been skipped
WithEndpoint expects host[:port] with no scheme. When Doppler has https://otel.libnovel.cc the backend was crashing with 'invalid port'. Now strip the scheme and enable TLS when prefix is https://.
This commit is contained in:
@@ -2,7 +2,10 @@
|
|||||||
//
|
//
|
||||||
// It reads two environment variables:
|
// It reads two environment variables:
|
||||||
//
|
//
|
||||||
// OTEL_EXPORTER_OTLP_ENDPOINT — OTLP/HTTP endpoint, e.g. http://otel-collector:4318
|
// OTEL_EXPORTER_OTLP_ENDPOINT — OTLP/HTTP endpoint; accepts either a full
|
||||||
|
// URL ("https://otel.example.com") or a bare
|
||||||
|
// host[:port] ("otel-collector:4318").
|
||||||
|
// TLS is used when the value starts with "https://".
|
||||||
// OTEL_SERVICE_NAME — service name reported in traces (default: "backend")
|
// OTEL_SERVICE_NAME — service name reported in traces (default: "backend")
|
||||||
//
|
//
|
||||||
// When OTEL_EXPORTER_OTLP_ENDPOINT is empty the function is a no-op: it
|
// When OTEL_EXPORTER_OTLP_ENDPOINT is empty the function is a no-op: it
|
||||||
@@ -21,6 +24,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"go.opentelemetry.io/contrib/bridges/otelslog"
|
"go.opentelemetry.io/contrib/bridges/otelslog"
|
||||||
@@ -41,11 +45,17 @@ import (
|
|||||||
// - logger: an slog.Logger bridged to OTel logs (falls back to default when disabled).
|
// - logger: an slog.Logger bridged to OTel logs (falls back to default when disabled).
|
||||||
// - err: non-nil only on SDK initialisation failure.
|
// - err: non-nil only on SDK initialisation failure.
|
||||||
func Init(ctx context.Context, version string) (shutdown func(), logger *slog.Logger, err error) {
|
func Init(ctx context.Context, version string) (shutdown func(), logger *slog.Logger, err error) {
|
||||||
endpoint := os.Getenv("OTEL_EXPORTER_OTLP_ENDPOINT")
|
rawEndpoint := os.Getenv("OTEL_EXPORTER_OTLP_ENDPOINT")
|
||||||
if endpoint == "" {
|
if rawEndpoint == "" {
|
||||||
return nil, slog.Default(), nil // OTel disabled — not an error
|
return nil, slog.Default(), nil // OTel disabled — not an error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WithEndpoint expects a host[:port] value — no scheme.
|
||||||
|
// Support both "https://otel.example.com" and "otel-collector:4318".
|
||||||
|
useTLS := strings.HasPrefix(rawEndpoint, "https://")
|
||||||
|
endpoint := strings.TrimPrefix(rawEndpoint, "https://")
|
||||||
|
endpoint = strings.TrimPrefix(endpoint, "http://")
|
||||||
|
|
||||||
serviceName := os.Getenv("OTEL_SERVICE_NAME")
|
serviceName := os.Getenv("OTEL_SERVICE_NAME")
|
||||||
if serviceName == "" {
|
if serviceName == "" {
|
||||||
serviceName = "backend"
|
serviceName = "backend"
|
||||||
@@ -63,10 +73,11 @@ func Init(ctx context.Context, version string) (shutdown func(), logger *slog.Lo
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ── Trace provider ────────────────────────────────────────────────────────
|
// ── Trace provider ────────────────────────────────────────────────────────
|
||||||
traceExp, err := otlptracehttp.New(ctx,
|
traceOpts := []otlptracehttp.Option{otlptracehttp.WithEndpoint(endpoint)}
|
||||||
otlptracehttp.WithEndpoint(endpoint),
|
if !useTLS {
|
||||||
otlptracehttp.WithInsecure(), // collector is on the internal Docker network
|
traceOpts = append(traceOpts, otlptracehttp.WithInsecure())
|
||||||
)
|
}
|
||||||
|
traceExp, err := otlptracehttp.New(ctx, traceOpts...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, slog.Default(), fmt.Errorf("otelsetup: create OTLP trace exporter: %w", err)
|
return nil, slog.Default(), fmt.Errorf("otelsetup: create OTLP trace exporter: %w", err)
|
||||||
}
|
}
|
||||||
@@ -79,10 +90,11 @@ func Init(ctx context.Context, version string) (shutdown func(), logger *slog.Lo
|
|||||||
otel.SetTracerProvider(tp)
|
otel.SetTracerProvider(tp)
|
||||||
|
|
||||||
// ── Log provider ──────────────────────────────────────────────────────────
|
// ── Log provider ──────────────────────────────────────────────────────────
|
||||||
logExp, err := otlploghttp.New(ctx,
|
logOpts := []otlploghttp.Option{otlploghttp.WithEndpoint(endpoint)}
|
||||||
otlploghttp.WithEndpoint(endpoint),
|
if !useTLS {
|
||||||
otlploghttp.WithInsecure(),
|
logOpts = append(logOpts, otlploghttp.WithInsecure())
|
||||||
)
|
}
|
||||||
|
logExp, err := otlploghttp.New(ctx, logOpts...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, slog.Default(), fmt.Errorf("otelsetup: create OTLP log exporter: %w", err)
|
return nil, slog.Default(), fmt.Errorf("otelsetup: create OTLP log exporter: %w", err)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user