Same agent Smaller bill. The tools and infrastructure to get more work from your AI. Fewer tokens. Less compute. Lower cost.
TypeScript agent-harnessagent-harness-engineeringai-engineeringai-observabilityamd-gpuclaude-codeclickhousecodexcoding-agentcoding-agentscursorgenaigpu-monitoringllm-observabilityllmopsnvidia-smiobservabilityopen-sourceopenaiopentelemetry
fix: keep OTLP ingest reachable across architectures and host setups (#1834)
Aman Agarwal1ed8bb7359a8
diff --git a/docker-compose.yml b/docker-compose.ymlindex 1ae9e9e9..afec9f6f 100644--- a/docker-compose.yml+++ b/docker-compose.yml@@ -90,7 +90,7 @@ services:volumes:- openlit-data:/app/client/datahealthcheck:- test: ["CMD", "node", "-e", "fetch('http://localhost:3000').then(r=>{process.exit(r.ok?0:1)}).catch(()=>process.exit(1))"]+ test: ["CMD", "node", "-e", "const ui=process.env.DOCKER_PORT||process.env.PORT||3000;let raw=process.env.OTLP_HTTP_ADDR||'0.0.0.0:4318',host='127.0.0.1',port='4318';if(raw.startsWith('[')){const end=raw.indexOf(']');if(end>1&&raw[end+1]===':'){host=raw.slice(1,end);port=raw.slice(end+2)}}else{const i=raw.lastIndexOf(':');if(i>=0){host=raw.slice(0,i);port=raw.slice(i+1)}}if(host===''||host==='0.0.0.0'||host==='::')host='127.0.0.1';const h=host.includes(':')?'['+host+']':host;Promise.all([fetch('http://127.0.0.1:'+ui),fetch('http://'+h+':'+port+'/health')]).then(rs=>process.exit(rs.every(r=>r.ok)?0:1)).catch(()=>process.exit(1))"]interval: 5stimeout: 3sretries: 10diff --git a/examples/kubernetes/openlit.yaml b/examples/kubernetes/openlit.yamlindex e8592729..4178af6e 100644--- a/examples/kubernetes/openlit.yaml+++ b/examples/kubernetes/openlit.yaml@@ -49,9 +49,11 @@ spec:- name: datamountPath: /app/client/datareadinessProbe:- httpGet:- path: /- port: 3000+ exec:+ command:+ - node+ - -e+ - "const ui=process.env.DOCKER_PORT||process.env.PORT||3000;let raw=process.env.OTLP_HTTP_ADDR||'0.0.0.0:4318',host='127.0.0.1',port='4318';if(raw.startsWith('[')){const end=raw.indexOf(']');if(end>1&&raw[end+1]===':'){host=raw.slice(1,end);port=raw.slice(end+2)}}else{const i=raw.lastIndexOf(':');if(i>=0){host=raw.slice(0,i);port=raw.slice(i+1)}}if(host===''||host==='0.0.0.0'||host==='::')host='127.0.0.1';const h=host.includes(':')?'['+host+']':host;Promise.all([fetch('http://127.0.0.1:'+ui),fetch('http://'+h+':'+port+'/health')]).then(rs=>process.exit(rs.every(r=>r.ok)?0:1)).catch(()=>process.exit(1))"initialDelaySeconds: 30periodSeconds: 10resources:diff --git a/examples/linux/docker-compose.yaml b/examples/linux/docker-compose.yamlindex 3eab4eaa..9c8cb85e 100644--- a/examples/linux/docker-compose.yaml+++ b/examples/linux/docker-compose.yaml@@ -42,7 +42,7 @@ services:volumes:- openlit-data:/app/client/datahealthcheck:- test: ["CMD", "node", "-e", "fetch('http://localhost:3000').then(r=>{process.exit(r.ok?0:1)}).catch(()=>process.exit(1))"]+ test: ["CMD", "node", "-e", "const ui=process.env.DOCKER_PORT||process.env.PORT||3000;let raw=process.env.OTLP_HTTP_ADDR||'0.0.0.0:4318',host='127.0.0.1',port='4318';if(raw.startsWith('[')){const end=raw.indexOf(']');if(end>1&&raw[end+1]===':'){host=raw.slice(1,end);port=raw.slice(end+2)}}else{const i=raw.lastIndexOf(':');if(i>=0){host=raw.slice(0,i);port=raw.slice(i+1)}}if(host===''||host==='0.0.0.0'||host==='::')host='127.0.0.1';const h=host.includes(':')?'['+host+']':host;Promise.all([fetch('http://127.0.0.1:'+ui),fetch('http://'+h+':'+port+'/health')]).then(rs=>process.exit(rs.every(r=>r.ok)?0:1)).catch(()=>process.exit(1))"]interval: 5stimeout: 5sretries: 10diff --git a/src/Dockerfile b/src/Dockerfileindex 633d23ce..ad636e64 100644--- a/src/Dockerfile+++ b/src/Dockerfile@@ -23,24 +23,44 @@ ENV PRISMA_CLI_BINARY_TARGETS=linux-musl-openssl-3.0.x,linux-musl-arm64-openssl-RUN npm install && npx prisma generate && npm run build-# OTLP receiver build from source - pinned with digest-FROM golang:1.25-alpine@sha256:8e02eb337d9e0ea459e041f1ee5eece41cbb61f1d83e7d883a3e2fb4862063fa AS go-builder+# OTLP receiver build from source - pinned with digest.+# Cross-compile for the image platform. Do not default TARGETARCH: openlit+# 1.24.0–2.1.0 arm64 images shipped amd64 otelcontribcol/opampsupervisor+# (https://github.com/openlit/openlit/issues/1747), so 4317/4318 never opened.+FROM --platform=$BUILDPLATFORM golang:1.25-alpine@sha256:8e02eb337d9e0ea459e041f1ee5eece41cbb61f1d83e7d883a3e2fb4862063fa AS go-builder++ARG TARGETOS+ARG TARGETARCHWORKDIR /app/otlp-receiverCOPY ./otlp-receiver/ ./-RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o otlp-receiver ./cmd/otlp-receiver+RUN set -eu; \+ if [ -z "${TARGETOS}" ] || [ -z "${TARGETARCH}" ]; then \+ echo "TARGETOS and TARGETARCH are required; refusing to build for the builder CPU." >&2; \+ exit 1; \+ fi; \+ echo "Building otlp-receiver for ${TARGETOS}/${TARGETARCH}"; \+ CGO_ENABLED=0 GOOS="${TARGETOS}" GOARCH="${TARGETARCH}" go build -trimpath -o otlp-receiver ./cmd/otlp-receiver# Realtime engine (NATS JetStream consumer); started only when NATS_URL is setWORKDIR /app/engineCOPY ./engine/ ./-RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o openlit-engine ./cmd/openlit-engine+RUN set -eu; \+ if [ -z "${TARGETOS}" ] || [ -z "${TARGETARCH}" ]; then \+ echo "TARGETOS and TARGETARCH are required; refusing to build for the builder CPU." >&2; \+ exit 1; \+ fi; \+ echo "Building openlit-engine for ${TARGETOS}/${TARGETARCH}"; \+ CGO_ENABLED=0 GOOS="${TARGETOS}" GOARCH="${TARGETARCH}" go build -trimpath -o openlit-engine ./cmd/openlit-engine# Final runtime image - Node 20 on Debian bookworm (bullseye-security# package indexes now 404 openssl 1.1.1w updates required by bullseye-slim)FROM node:20-bookworm-slim@sha256:2cf067cfed83d5ea958367df9f966191a942351a2df77d6f0193e162b5febfc0-ARG TARGETARCH=amd64+# No default. ARG TARGETARCH=amd64 made arm64 images record amd64+# (https://github.com/openlit/openlit/issues/1747).+ARG TARGETARCHARG TARGETPLATFORMENV TARGETARCH=${TARGETARCH}@@ -76,6 +96,12 @@ COPY --from=go-builder /app/engine/openlit-engine /app/engine/openlit-engineRUN chmod +x ./scripts/entrypoint.sh /app/otlp-receiver/otlp-receiver /app/engine/openlit-engine+# The published arm64 image used to contain x86-64 ingest binaries and the+# UI still came up. Refuse that image here.+COPY ./scripts/check-elf-arch.mjs /tmp/check-elf-arch.mjs+RUN node /tmp/check-elf-arch.mjs /app/otlp-receiver/otlp-receiver /app/engine/openlit-engine \+ && rm /tmp/check-elf-arch.mjs+# Next.js UI + OTLP HTTP/gRPCEXPOSE ${DOCKER_PORT:-3000}EXPOSE 4317diff --git a/src/client/scripts/entrypoint.sh b/src/client/scripts/entrypoint.shindex fd5eb216..54caad11 100644--- a/src/client/scripts/entrypoint.sh+++ b/src/client/scripts/entrypoint.sh@@ -62,18 +62,32 @@ elsefiexport OPENLIT_ENGINE_SECRET-# Set NextAuth.js environment variables-# remove any existing NEXTAUTH_SECRET line, then append the current value-if [ -w /etc/environment ]; then- sed -i '/^NEXTAUTH_SECRET=/d' /etc/environment- echo "NEXTAUTH_SECRET=$NEXTAUTH_SECRET" >> /etc/environment- sed -i '/^CRON_JOB_SECRET=/d' /etc/environment- echo "CRON_JOB_SECRET=$CRON_JOB_SECRET" >> /etc/environment- sed -i '/^OPENLIT_ENGINE_SECRET=/d' /etc/environment- echo "OPENLIT_ENGINE_SECRET=$OPENLIT_ENGINE_SECRET" >> /etc/environment-else- echo "WARNING: /etc/environment is not writable; NEXTAUTH_SECRET, CRON_JOB_SECRET and OPENLIT_ENGINE_SECRET will not be persisted there." >&2-fi+# Write a shell-safe line and export it. Sourcing /etc/environment does not+# export new names, and an unquoted password containing $, spaces, or # is+# expanded or truncated. The OTLP receiver only sees variables that are+# exported in this process.+persist_warned=+persist_env() {+ local key="$1"+ local value="$2"+ export "${key}=${value}"+ if [ ! -w /etc/environment ]; then+ if [ -z "${persist_warned}" ]; then+ echo "WARNING: /etc/environment is not writable; secrets will not be persisted there." >&2+ persist_warned=1+ fi+ return 0+ fi+ # The value is already exported. A read-only mount can still pass -w and+ # then fail the write; that must not skip opening the OTLP ports.+ if ! sed -i "/^${key}=/d" /etc/environment || ! printf '%s=%q\n' "$key" "$value" >> /etc/environment; then+ echo "WARNING: could not persist ${key} to /etc/environment; the process environment is still set." >&2+ fi+}++persist_env NEXTAUTH_SECRET "${NEXTAUTH_SECRET}"+persist_env CRON_JOB_SECRET "${CRON_JOB_SECRET}"+persist_env OPENLIT_ENGINE_SECRET "${OPENLIT_ENGINE_SECRET}"# Do NOT pin NEXTAUTH_URL to localhost. When it is unset, NextAuth derives the# origin from the incoming request host (and X-Forwarded-* behind a proxy), so@@ -81,20 +95,27 @@ fi# Only export it when the operator explicitly set it, which is required for# OAuth providers whose redirect URIs must match a fixed public URL.if [ -n "${NEXTAUTH_URL}" ]; then- echo "NEXTAUTH_URL=${NEXTAUTH_URL}" >> /etc/environment+ persist_env NEXTAUTH_URL "${NEXTAUTH_URL}"fi-echo "SQLITE_DATABASE_URL=${SQLITE_DATABASE_URL:-file:../data/data.db}" >> /etc/environment-echo "PATH=./node_modules/.bin:$PATH" >> /etc/environment+persist_env SQLITE_DATABASE_URL "${SQLITE_DATABASE_URL:-file:../data/data.db}"+persist_env PATH "./node_modules/.bin:${PATH}"# Environment variables for DB config-echo "INIT_DB_USERNAME=${INIT_DB_USERNAME}" >> /etc/environment-echo "INIT_DB_PASSWORD=${INIT_DB_PASSWORD}" >> /etc/environment-echo "INIT_DB_HOST=${INIT_DB_HOST}" >> /etc/environment-echo "INIT_DB_PORT=${INIT_DB_PORT}" >> /etc/environment-echo "INIT_DB_DATABASE=${INIT_DB_DATABASE}" >> /etc/environment--# Load the environment variables-. /etc/environment+persist_env INIT_DB_USERNAME "${INIT_DB_USERNAME:-}"+persist_env INIT_DB_PASSWORD "${INIT_DB_PASSWORD:-}"+persist_env INIT_DB_HOST "${INIT_DB_HOST:-}"+persist_env INIT_DB_PORT "${INIT_DB_PORT:-}"+persist_env INIT_DB_DATABASE "${INIT_DB_DATABASE:-}"++# Re-export the file only after it was rewritten. Sourcing a read-only copy+# would replace the secrets just computed above, and a bad line must not+# stop ingest: Docker already injected the process environment.+if [ -r /etc/environment ] && [ -w /etc/environment ]; then+ set -a+ # shellcheck disable=SC1091+ . /etc/environment || echo "WARNING: could not read /etc/environment; continuing with the process environment." >&2+ set +a+fi# Receiver reads SQLITE_DATABASE_URL / INIT_DB_* from the process environment.# Run Prisma migrations and generate prisma client@@ -104,11 +125,42 @@ prisma generate# Run the seedprisma db seed-# Run crond in the background-service cron start+# Cron is optional. A failure here (non-root, read-only root) must not stop+# the OTLP receiver from opening :4317 and :4318.+if ! service cron start; then+ echo "WARNING: cron did not start; scheduled jobs will not run. OTLP ingest will still start." >&2+fi++# Stay PID 1 so we can reap children. 2.1.0 exec'd the UI and left a dead+# collector behind: Docker still published :4317, accepted TCP, and closed it.+# gRPC exporters then logged "End of TCP stream" while the dashboard looked healthy.+receiver_pid=+engine_pid=+node_pid=++stop_children() {+ if [ -n "${node_pid}" ]; then+ kill -TERM "${node_pid}" 2>/dev/null || true+ fi+ if [ -n "${engine_pid}" ]; then+ kill -TERM "${engine_pid}" 2>/dev/null || true+ fi+ if [ -n "${receiver_pid}" ]; then+ kill -TERM "${receiver_pid}" 2>/dev/null || true+ fi+}++on_stop() {+ echo "Stopping OpenLIT..."+ stop_children+ wait ${node_pid:+"$node_pid"} ${engine_pid:+"$engine_pid"} ${receiver_pid:+"$receiver_pid"} 2>/dev/null || true+ exit 0+}+trap on_stop TERM INTecho "Starting OTLP receiver..."/app/otlp-receiver/otlp-receiver &+receiver_pid=$!# The realtime engine consumes the NATS signal stream and posts findings back# to the UI server's internal API, authenticated with OPENLIT_ENGINE_SECRET.@@ -121,6 +173,7 @@ if [ -n "${NATS_URL:-}" ]; thenecho "Starting realtime engine..."OPENLIT_URL="${OPENLIT_URL:-http://127.0.0.1:${DOCKER_PORT:-3000}}" \/app/engine/openlit-engine &+ engine_pid=$!fi# Starting the OpenLIT UI Server@@ -132,4 +185,27 @@ export HOSTNAME="${HOSTNAME:-0.0.0.0}"if echo "$HOSTNAME" | grep -qvE '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$|^::$|^localhost$'; thenexport HOSTNAME="0.0.0.0"fi-exec node --max_old_space_size=512 /app/client/server.js\ No newline at end of file+node --max_old_space_size=512 /app/client/server.js &+node_pid=$!++# set -e would exit on the first child failure before we can tell which one.+set +e+while true; do+ wait -n+ status=$?+ if ! kill -0 "${receiver_pid}" 2>/dev/null; then+ echo "OTLP receiver exited; stopping the container so :4317 and :4318 are not left closed." >&2+ stop_children+ wait ${node_pid:+"$node_pid"} ${engine_pid:+"$engine_pid"} 2>/dev/null+ exit 1+ fi+ if ! kill -0 "${node_pid}" 2>/dev/null; then+ stop_children+ wait ${receiver_pid:+"$receiver_pid"} ${engine_pid:+"$engine_pid"} 2>/dev/null+ exit "${status}"+ fi+ if [ -n "${engine_pid}" ] && ! kill -0 "${engine_pid}" 2>/dev/null; then+ echo "Realtime engine exited; trace ingest continues." >&2+ engine_pid=+ fi+donediff --git a/src/dev-docker-compose.yml b/src/dev-docker-compose.ymlindex 5be5261e..48068fec 100644--- a/src/dev-docker-compose.yml+++ b/src/dev-docker-compose.yml@@ -97,7 +97,7 @@ services:volumes:- openlit-data:/app/client/datahealthcheck:- test: ["CMD", "node", "-e", "fetch('http://localhost:3000').then(r=>{process.exit(r.ok?0:1)}).catch(()=>process.exit(1))"]+ test: ["CMD", "node", "-e", "const ui=process.env.DOCKER_PORT||process.env.PORT||3000;let raw=process.env.OTLP_HTTP_ADDR||'0.0.0.0:4318',host='127.0.0.1',port='4318';if(raw.startsWith('[')){const end=raw.indexOf(']');if(end>1&&raw[end+1]===':'){host=raw.slice(1,end);port=raw.slice(end+2)}}else{const i=raw.lastIndexOf(':');if(i>=0){host=raw.slice(0,i);port=raw.slice(i+1)}}if(host===''||host==='0.0.0.0'||host==='::')host='127.0.0.1';const h=host.includes(':')?'['+host+']':host;Promise.all([fetch('http://127.0.0.1:'+ui),fetch('http://'+h+':'+port+'/health')]).then(rs=>process.exit(rs.every(r=>r.ok)?0:1)).catch(()=>process.exit(1))"]interval: 5stimeout: 3sretries: 10diff --git a/src/otlp-receiver/cmd/otlp-receiver/main.go b/src/otlp-receiver/cmd/otlp-receiver/main.goindex 153f0bad..84f53a85 100644--- a/src/otlp-receiver/cmd/otlp-receiver/main.go+++ b/src/otlp-receiver/cmd/otlp-receiver/main.go@@ -3,6 +3,7 @@ package mainimport ("context""log"+ "net""net/http""os""os/signal"@@ -14,6 +15,7 @@ import ("github.com/openlit/openlit/otlp-receiver/internal/grpcserver""github.com/openlit/openlit/otlp-receiver/internal/httpserver""github.com/openlit/openlit/otlp-receiver/internal/ingest"+ "github.com/openlit/openlit/otlp-receiver/internal/listen""github.com/openlit/openlit/otlp-receiver/internal/natsx""github.com/openlit/openlit/otlp-receiver/internal/realtime"sigpkg "github.com/openlit/openlit/otlp-receiver/internal/signal"@@ -99,12 +101,18 @@ func main() {}()}- go func() {- log.Printf("OTLP HTTP listening on %s", cfg.HTTPAddr)- if err := httpSrv.ListenAndServe(); err != nil && err != http.ErrServerClosed {- log.Fatalf("otlp http: %v", err)- }- }()+ httpLns, err := listen.TCP(cfg.HTTPAddr)+ if err != nil {+ log.Fatalf("otlp http: %v", err)+ }+ log.Printf("OTLP HTTP listening on %s", cfg.HTTPAddr)+ for _, ln := range httpLns {+ go func(ln net.Listener) {+ if err := httpSrv.Serve(ln); err != nil && err != http.ErrServerClosed {+ log.Fatalf("otlp http: %v", err)+ }+ }(ln)+ }go func() {log.Printf("OTLP gRPC listening on %s", cfg.GRPCAddr)if err := grpcserver.Listen(cfg.GRPCAddr, grpcSrv); err != nil {diff --git a/src/otlp-receiver/internal/config/config.go b/src/otlp-receiver/internal/config/config.goindex 76fd6e9d..1c3a9c9f 100644--- a/src/otlp-receiver/internal/config/config.go+++ b/src/otlp-receiver/internal/config/config.go@@ -54,6 +54,8 @@ type ClickHouseConfig struct {func Load() Config {return Config{+ // Wildcard addresses are opened on IPv4 and IPv6. 0.0.0.0 alone leaves+ // ::1 closed, so localhost fails on machines that prefer IPv6.HTTPAddr: env("OTLP_HTTP_ADDR", "0.0.0.0:4318"),GRPCAddr: env("OTLP_GRPC_ADDR", "0.0.0.0:4317"),SQLitePath: sqlitePath(os.Getenv("SQLITE_DATABASE_URL")),diff --git a/src/otlp-receiver/internal/grpcserver/server.go b/src/otlp-receiver/internal/grpcserver/server.goindex 548f9ac0..789822e1 100644--- a/src/otlp-receiver/internal/grpcserver/server.go+++ b/src/otlp-receiver/internal/grpcserver/server.go@@ -2,11 +2,13 @@ package grpcserverimport ("context"+ "errors""log""net""google.golang.org/grpc""google.golang.org/grpc/codes"+ _ "google.golang.org/grpc/encoding/gzip" // OTLP clients send grpc-encoding=gzip"google.golang.org/grpc/metadata""google.golang.org/grpc/peer""google.golang.org/grpc/status"@@ -16,6 +18,7 @@ import (colltrace "go.opentelemetry.io/proto/otlp/collector/trace/v1""github.com/openlit/openlit/otlp-receiver/internal/ingest"+ "github.com/openlit/openlit/otlp-receiver/internal/listen""github.com/openlit/openlit/otlp-receiver/internal/tenant")@@ -34,8 +37,15 @@ type metricsServer struct {svc *ingest.Service}+// maxMessageBytes matches the HTTP receiver. The gRPC default of 4MiB+// rejects larger OTLP batches that the HTTP path accepts.+const maxMessageBytes = 32 << 20+func New(svc *ingest.Service) *grpc.Server {- s := grpc.NewServer()+ s := grpc.NewServer(+ grpc.MaxRecvMsgSize(maxMessageBytes),+ grpc.MaxSendMsgSize(maxMessageBytes),+ )colltrace.RegisterTraceServiceServer(s, &tracesServer{svc: svc})colllog.RegisterLogsServiceServer(s, &logsServer{svc: svc})collmetric.RegisterMetricsServiceServer(s, &metricsServer{svc: svc})@@ -43,11 +53,34 @@ func New(svc *ingest.Service) *grpc.Server {}func Listen(addr string, srv *grpc.Server) error {- ln, err := net.Listen("tcp", addr)+ lns, err := listen.TCP(addr)if err != nil {return err}- return srv.Serve(ln)+ if len(lns) == 1 {+ return ignoreStopped(srv.Serve(lns[0]))+ }+ errc := make(chan error, len(lns))+ for _, ln := range lns {+ go func(ln net.Listener) {+ errc <- srv.Serve(ln)+ }(ln)+ }+ first := <-errc+ for _, ln := range lns {+ _ = ln.Close()+ }+ for i := 1; i < len(lns); i++ {+ <-errc+ }+ return ignoreStopped(first)+}++func ignoreStopped(err error) error {+ if errors.Is(err, grpc.ErrServerStopped) {+ return nil+ }+ return err}func authorization(ctx context.Context) string {diff --git a/src/otlp-receiver/internal/grpcserver/server_test.go b/src/otlp-receiver/internal/grpcserver/server_test.gonew file mode 100644index 00000000..828afc89--- /dev/null+++ b/src/otlp-receiver/internal/grpcserver/server_test.go@@ -0,0 +1,90 @@+package grpcserver++import (+ "context"+ "net"+ "testing"+ "time"++ colltrace "go.opentelemetry.io/proto/otlp/collector/trace/v1"+ "google.golang.org/grpc"+ "google.golang.org/grpc/codes"+ "google.golang.org/grpc/credentials/insecure"+ "google.golang.org/grpc/status"++ "github.com/openlit/openlit/otlp-receiver/internal/ingest"+ "github.com/openlit/openlit/otlp-receiver/internal/tenant"+)++// TestExportSpeaksGRPC fails if the listener accepts TCP and then drops the+// HTTP/2 preface. That is the failure Python exporters report as+// StatusCode.UNAVAILABLE / "End of TCP stream" against :4317.+func TestExportSpeaksGRPC(t *testing.T) {+ probe, err := net.Listen("tcp", "127.0.0.1:0")+ if err != nil {+ t.Fatal(err)+ }+ addr := probe.Addr().String()+ if err := probe.Close(); err != nil {+ t.Fatal(err)+ }+ _, port, err := net.SplitHostPort(addr)+ if err != nil {+ t.Fatal(err)+ }++ srv := New(&ingest.Service{+ Tenants: &tenant.Store{RequireKey: true},+ Limiter: tenant.NewFailLimiter(20, 60),+ })+ errc := make(chan error, 1)+ go func() {+ errc <- Listen("0.0.0.0:"+port, srv)+ }()+ t.Cleanup(func() {+ srv.Stop()+ select {+ case <-errc:+ case <-time.After(2 * time.Second):+ t.Errorf("Listen did not return")+ }+ })+ waitForAccept(t, "127.0.0.1:"+port)++ conn, err := grpc.NewClient("127.0.0.1:"+port, grpc.WithTransportCredentials(insecure.NewCredentials()))+ if err != nil {+ t.Fatal(err)+ }+ t.Cleanup(func() { _ = conn.Close() })++ ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)+ defer cancel()+ _, err = colltrace.NewTraceServiceClient(conn).Export(+ ctx,+ &colltrace.ExportTraceServiceRequest{},+ grpc.UseCompressor("gzip"),+ )+ st, ok := status.FromError(err)+ if !ok {+ t.Fatalf("transport error, not a gRPC status: %v", err)+ }+ if st.Code() != codes.Unauthenticated {+ t.Fatalf("code %s: %s", st.Code(), st.Message())+ }+}++func waitForAccept(t *testing.T, addr string) {+ t.Helper()+ deadline := time.Now().Add(2 * time.Second)+ var last error+ for time.Now().Before(deadline) {+ conn, err := net.DialTimeout("tcp", addr, 100*time.Millisecond)+ if err == nil {+ conn.Close()+ return+ }+ last = err+ time.Sleep(10 * time.Millisecond)+ }+ t.Fatalf("Listen did not accept on %s: %v", addr, last)+}diff --git a/src/otlp-receiver/internal/listen/listen.go b/src/otlp-receiver/internal/listen/listen.gonew file mode 100644index 00000000..b5ec2b59--- /dev/null+++ b/src/otlp-receiver/internal/listen/listen.go@@ -0,0 +1,94 @@+// Package listen opens the OTLP ports on every address family the machine has.+// Binding only 0.0.0.0 leaves ::1 closed, so clients that resolve localhost to+// IPv6 first never reach the receiver.+package listen++import (+ "errors"+ "net"+ "strconv"+ "syscall"+)++// TCP listens on addr. A wildcard ("" / 0.0.0.0 / ::) listens on IPv6 and,+// when that socket is IPv4-only or IPv6 is unavailable, on IPv4 as well.+func TCP(addr string) ([]net.Listener, error) {+ host, port, err := net.SplitHostPort(addr)+ if err != nil || !wildcard(host) {+ ln, lerr := net.Listen("tcp", addr)+ if lerr != nil {+ return nil, lerr+ }+ return []net.Listener{ln}, nil+ }+ if port == "" {+ port = "0"+ }++ v6, v6err := net.Listen("tcp", net.JoinHostPort("::", port))+ if v6err != nil {+ ln, lerr := net.Listen("tcp4", net.JoinHostPort("0.0.0.0", port))+ if lerr != nil {+ return nil, lerr+ }+ return []net.Listener{ln}, nil+ }++ gotPort := port+ if tcp, ok := v6.Addr().(*net.TCPAddr); ok {+ gotPort = strconv.Itoa(tcp.Port)+ }+ v4, v4err := net.Listen("tcp4", net.JoinHostPort("0.0.0.0", gotPort))+ if v4err != nil {+ return afterIPv4Failure(v6, v4err)+ }+ return []net.Listener{v6, v4}, nil+}++// afterIPv4Failure decides what an IPv4 wildcard failure means.+// EADDRINUSE is the dual-stack socket already covering IPv4 only when that+// socket is not IPv6-only. An unrelated listener on the same port must fail+// the bind. A machine with no IPv4 stack keeps the IPv6 listener.+func afterIPv4Failure(v6 net.Listener, v4err error) ([]net.Listener, error) {+ if errors.Is(v4err, syscall.EADDRINUSE) && dualStack(v6) {+ return []net.Listener{v6}, nil+ }+ if ipv4Unavailable(v4err) {+ return []net.Listener{v6}, nil+ }+ v6.Close()+ return nil, v4err+}++func dualStack(ln net.Listener) bool {+ tcp, ok := ln.(*net.TCPListener)+ if !ok {+ return false+ }+ if addr, ok := tcp.Addr().(*net.TCPAddr); !ok || (addr.IP != nil && !addr.IP.IsUnspecified()) {+ return false+ }+ raw, err := tcp.SyscallConn()+ if err != nil {+ return false+ }+ var v6only int+ var getErr error+ err = raw.Control(func(fd uintptr) {+ v6only, getErr = syscall.GetsockoptInt(int(fd), syscall.IPPROTO_IPV6, syscall.IPV6_V6ONLY)+ })+ return err == nil && getErr == nil && v6only == 0+}++func ipv4Unavailable(err error) bool {+ return errors.Is(err, syscall.EAFNOSUPPORT) || errors.Is(err, syscall.EPROTONOSUPPORT)+}++func wildcard(host string) bool {+ switch host {+ case "", "0.0.0.0", "::", "[::]":+ return true+ default:+ return false+ }+}diff --git a/src/otlp-receiver/internal/listen/listen_test.go b/src/otlp-receiver/internal/listen/listen_test.gonew file mode 100644index 00000000..cc2b1dbc--- /dev/null+++ b/src/otlp-receiver/internal/listen/listen_test.go@@ -0,0 +1,102 @@+package listen++import (+ "errors"+ "net"+ "strconv"+ "syscall"+ "testing"+)++func TestWildcardReachesIPv4(t *testing.T) {+ lns, err := TCP("0.0.0.0:0")+ if err != nil {+ t.Fatal(err)+ }+ t.Cleanup(func() {+ for _, ln := range lns {+ ln.Close()+ }+ })+ port := lns[0].Addr().(*net.TCPAddr).Port+ conn, err := net.Dial("tcp4", net.JoinHostPort("127.0.0.1", strconv.Itoa(port)))+ if err != nil {+ t.Fatalf("ipv4 localhost: %v", err)+ }+ conn.Close()+}++func TestWildcardReachesIPv6(t *testing.T) {+ probe, err := net.Listen("tcp", "[::1]:0")+ if err != nil {+ t.Skip("ipv6 is not available")+ }+ probe.Close()++ lns, err := TCP(":0")+ if err != nil {+ t.Fatal(err)+ }+ t.Cleanup(func() {+ for _, ln := range lns {+ ln.Close()+ }+ })+ port := lns[0].Addr().(*net.TCPAddr).Port+ conn, err := net.Dial("tcp6", net.JoinHostPort("::1", strconv.Itoa(port)))+ if err != nil {+ t.Fatalf("ipv6 localhost: %v", err)+ }+ conn.Close()+}++func TestEADDRINUSEOnIPv6OnlyIsAnError(t *testing.T) {+ ln, err := net.Listen("tcp", "[::1]:0")+ if err != nil {+ t.Skip("ipv6 is not available")+ }+ if dualStack(ln) {+ ln.Close()+ t.Fatal("[::1] listener reported dual-stack")+ }+ got, err := afterIPv4Failure(ln, syscall.EADDRINUSE)+ if err == nil {+ for _, l := range got {+ l.Close()+ }+ t.Fatal("expected EADDRINUSE")+ }+ if !errors.Is(err, syscall.EADDRINUSE) {+ t.Fatalf("error: %v", err)+ }+}++func TestMissingIPv4KeepsIPv6(t *testing.T) {+ ln, err := net.Listen("tcp", "[::1]:0")+ if err != nil {+ t.Skip("ipv6 is not available")+ }+ got, err := afterIPv4Failure(ln, syscall.EAFNOSUPPORT)+ if err != nil {+ ln.Close()+ t.Fatal(err)+ }+ if len(got) != 1 || got[0] != ln {+ t.Fatalf("listeners: %v", got)+ }+ ln.Close()+}++func TestSpecificHostStaysSpecific(t *testing.T) {+ lns, err := TCP("127.0.0.1:0")+ if err != nil {+ t.Fatal(err)+ }+ t.Cleanup(func() { lns[0].Close() })+ if len(lns) != 1 {+ t.Fatalf("listeners: %d", len(lns))+ }+ if lns[0].Addr().(*net.TCPAddr).IP.String() != "127.0.0.1" {+ t.Fatalf("bound %s", lns[0].Addr())+ }+}diff --git a/src/scripts/check-elf-arch.mjs b/src/scripts/check-elf-arch.mjsnew file mode 100644index 00000000..37ce4cc0--- /dev/null+++ b/src/scripts/check-elf-arch.mjs@@ -0,0 +1,45 @@+// Fail when a copied binary cannot run in this image.+// OpenLIT 1.24–2.1.0 arm64 images contained x86-64 otelcontribcol and+// opampsupervisor (e_machine 0x3e), so nothing listened on 4317/4318.+// https://github.com/openlit/openlit/issues/1747++import { readFileSync } from "node:fs";+import { pathToFileURL } from "node:url";++const ELF_MACHINE = {+ x64: 62, // EM_X86_64+ arm64: 183, // EM_AARCH64+};++export function elfMachine(buf) {+ if (buf.length < 20 || buf[0] !== 0x7f || buf.toString("ascii", 1, 4) !== "ELF") {+ throw new Error("not an ELF binary");+ }+ return buf.readUInt16LE(18);+}++export function assertBinaryArch(path, arch = process.arch) {+ const expected = ELF_MACHINE[arch];+ if (!expected) {+ throw new Error(`unsupported runtime arch ${arch}`);+ }+ const machine = elfMachine(readFileSync(path));+ if (machine !== expected) {+ throw new Error(+ `${path} is e_machine ${machine} but this image is ${arch} (expected ${expected})`,+ );+ }+}++const isMain = process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href;+if (isMain) {+ const bins = process.argv.slice(2);+ if (bins.length === 0) {+ console.error("usage: check-elf-arch.mjs <binary>...");+ process.exit(1);+ }+ for (const bin of bins) {+ assertBinaryArch(bin);+ console.log(`${bin} matches ${process.arch}`);+ }+}diff --git a/src/scripts/check-elf-arch.test.mjs b/src/scripts/check-elf-arch.test.mjsnew file mode 100644index 00000000..24ebabba--- /dev/null+++ b/src/scripts/check-elf-arch.test.mjs@@ -0,0 +1,49 @@+import { test } from "node:test";+import assert from "node:assert/strict";+import { mkdtempSync, writeFileSync, rmSync } from "node:fs";+import { tmpdir } from "node:os";+import { join } from "node:path";+import { spawnSync } from "node:child_process";+import { fileURLToPath } from "node:url";++import { assertBinaryArch, elfMachine } from "./check-elf-arch.mjs";++const script = fileURLToPath(new URL("./check-elf-arch.mjs", import.meta.url));++function fakeElf(machine) {+ const buf = Buffer.alloc(64);+ buf[0] = 0x7f;+ buf.write("ELF", 1, "ascii");+ buf.writeUInt16LE(machine, 18);+ return buf;+}++test("reads e_machine and rejects a foreign binary", () => {+ const dir = mkdtempSync(join(tmpdir(), "elf-"));+ try {+ const native = join(dir, "native");+ const foreign = join(dir, "foreign");+ const expected = process.arch === "arm64" ? 183 : 62;+ const other = expected === 183 ? 62 : 183;+ writeFileSync(native, fakeElf(expected));+ writeFileSync(foreign, fakeElf(other));+ assert.equal(elfMachine(fakeElf(expected)), expected);+ assert.doesNotThrow(() => assertBinaryArch(native));+ assert.throws(() => assertBinaryArch(foreign), /e_machine/);+ } finally {+ rmSync(dir, { recursive: true, force: true });+ }+});++test("cli exits non-zero for an amd64 binary on the other arch", () => {+ const dir = mkdtempSync(join(tmpdir(), "elf-cli-"));+ try {+ const bin = join(dir, "otelcontribcol");+ writeFileSync(bin, fakeElf(process.arch === "arm64" ? 62 : 183));+ const result = spawnSync(process.execPath, [script, bin], { encoding: "utf8" });+ assert.notEqual(result.status, 0);+ assert.match(result.stderr, /e_machine/);+ } finally {+ rmSync(dir, { recursive: true, force: true });+ }+});