Skip to content

Commit 01bfb7a

Browse files
feat: add TLS configuration and fix version retrieval for OTel
- Add OTEL_EXPORTER_OTLP_INSECURE env var and --otel-insecure flag - Default to secure TLS connections (production-ready) - Fix version retrieval to use actual leeway.Version - Update documentation with Honeycomb production examples - Add TLS configuration section to observability docs Addresses review feedback on PR #288 Co-authored-by: Ona <no-reply@ona.com>
1 parent 50dd8eb commit 01bfb7a

File tree

5 files changed

+92
-20
lines changed

5 files changed

+92
-20
lines changed

README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -607,25 +607,35 @@ Leeway supports distributed tracing using OpenTelemetry to provide visibility in
607607
Enable tracing by setting the OTLP endpoint:
608608

609609
```bash
610+
# Local development (Jaeger)
610611
export OTEL_EXPORTER_OTLP_ENDPOINT=localhost:4318
612+
export OTEL_EXPORTER_OTLP_INSECURE=true
613+
leeway build :my-package
614+
615+
# Production (Honeycomb)
616+
export OTEL_EXPORTER_OTLP_ENDPOINT=api.honeycomb.io:443
617+
export OTEL_EXPORTER_OTLP_HEADERS="x-honeycomb-team=YOUR_API_KEY"
611618
leeway build :my-package
612619
```
613620

614621
Or using CLI flags:
615622

616623
```bash
617-
leeway build :my-package --otel-endpoint=localhost:4318
624+
leeway build :my-package --otel-endpoint=localhost:4318 --otel-insecure
618625
```
619626

620627
## Environment Variables
621628

622629
- `OTEL_EXPORTER_OTLP_ENDPOINT`: OTLP endpoint URL
630+
- `OTEL_EXPORTER_OTLP_INSECURE`: Disable TLS (`true` or `false`, default: `false`)
631+
- `OTEL_EXPORTER_OTLP_HEADERS`: Additional headers (e.g., API keys)
623632
- `TRACEPARENT`: W3C Trace Context traceparent header for distributed tracing
624633
- `TRACESTATE`: W3C Trace Context tracestate header
625634

626635
## CLI Flags
627636

628637
- `--otel-endpoint`: OTLP endpoint URL (overrides environment variable)
638+
- `--otel-insecure`: Disable TLS for OTLP endpoint
629639
- `--trace-parent`: W3C traceparent header for parent trace context
630640
- `--trace-state`: W3C tracestate header
631641

@@ -646,8 +656,9 @@ docker run -d --name jaeger \
646656
-p 16686:16686 \
647657
jaegertracing/all-in-one:latest
648658
649-
# Build with tracing
659+
# Build with tracing (insecure for local development)
650660
export OTEL_EXPORTER_OTLP_ENDPOINT=localhost:4318
661+
export OTEL_EXPORTER_OTLP_INSECURE=true
651662
leeway build :my-package
652663
653664
# View traces at http://localhost:16686

cmd/build.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ func addBuildFlags(cmd *cobra.Command) {
214214
cmd.Flags().Bool("fixed-build-dir", true, "Use a fixed build directory for each package, instead of based on the package version, to better utilize caches based on absolute paths (defaults to true)")
215215
cmd.Flags().Bool("docker-export-to-cache", false, "Export Docker images to cache instead of pushing directly (enables SLSA L3 compliance)")
216216
cmd.Flags().String("otel-endpoint", os.Getenv("OTEL_EXPORTER_OTLP_ENDPOINT"), "OpenTelemetry OTLP endpoint URL for tracing (defaults to $OTEL_EXPORTER_OTLP_ENDPOINT)")
217+
cmd.Flags().Bool("otel-insecure", os.Getenv("OTEL_EXPORTER_OTLP_INSECURE") == "true", "Disable TLS for OTLP endpoint (for local development only, defaults to $OTEL_EXPORTER_OTLP_INSECURE)")
217218
cmd.Flags().String("trace-parent", os.Getenv("TRACEPARENT"), "W3C Trace Context traceparent header for distributed tracing (defaults to $TRACEPARENT)")
218219
cmd.Flags().String("trace-state", os.Getenv("TRACESTATE"), "W3C Trace Context tracestate header for distributed tracing (defaults to $TRACESTATE)")
219220
}
@@ -325,8 +326,17 @@ func getBuildOpts(cmd *cobra.Command) ([]leeway.BuildOption, cache.LocalCache, f
325326
if otelEndpoint, err := cmd.Flags().GetString("otel-endpoint"); err != nil {
326327
log.Fatal(err)
327328
} else if otelEndpoint != "" {
328-
// Initialize tracer with the provided endpoint
329-
tp, err := telemetry.InitTracer(context.Background(), otelEndpoint)
329+
// Set leeway version for telemetry
330+
telemetry.SetLeewayVersion(leeway.Version)
331+
332+
// Get insecure flag
333+
otelInsecure, err := cmd.Flags().GetBool("otel-insecure")
334+
if err != nil {
335+
log.Fatal(err)
336+
}
337+
338+
// Initialize tracer with the provided endpoint and TLS configuration
339+
tp, err := telemetry.InitTracer(context.Background(), otelEndpoint, otelInsecure)
330340
if err != nil {
331341
log.WithError(err).Warn("failed to initialize OpenTelemetry tracer")
332342
} else {

docs/observability.md

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,15 @@ Leeway supports W3C Trace Context propagation, allowing builds to be part of lar
4444

4545
### Environment Variables
4646

47-
- `OTEL_EXPORTER_OTLP_ENDPOINT`: OTLP endpoint URL (e.g., `localhost:4318`)
47+
- `OTEL_EXPORTER_OTLP_ENDPOINT`: OTLP endpoint URL (e.g., `localhost:4318` or `api.honeycomb.io:443`)
48+
- `OTEL_EXPORTER_OTLP_INSECURE`: Disable TLS for OTLP endpoint (`true` or `false`, default: `false`)
4849
- `TRACEPARENT`: W3C Trace Context traceparent header (format: `00-{trace-id}-{span-id}-{flags}`)
4950
- `TRACESTATE`: W3C Trace Context tracestate header (optional)
5051

5152
### CLI Flags
5253

5354
- `--otel-endpoint`: OTLP endpoint URL (overrides `OTEL_EXPORTER_OTLP_ENDPOINT`)
55+
- `--otel-insecure`: Disable TLS for OTLP endpoint (overrides `OTEL_EXPORTER_OTLP_INSECURE`)
5456
- `--trace-parent`: W3C traceparent header (overrides `TRACEPARENT`)
5557
- `--trace-state`: W3C tracestate header (overrides `TRACESTATE`)
5658

@@ -61,6 +63,22 @@ CLI flags take precedence over environment variables:
6163
CLI flag → Environment variable → Default (disabled)
6264
```
6365

66+
### TLS Configuration
67+
68+
By default, leeway uses **secure TLS connections** to the OTLP endpoint. For local development with tools like Jaeger, you can disable TLS:
69+
70+
```bash
71+
# Local development (insecure)
72+
export OTEL_EXPORTER_OTLP_INSECURE=true
73+
export OTEL_EXPORTER_OTLP_ENDPOINT=localhost:4318
74+
leeway build :my-package
75+
76+
# Production (secure, default)
77+
export OTEL_EXPORTER_OTLP_ENDPOINT=api.honeycomb.io:443
78+
export OTEL_EXPORTER_OTLP_HEADERS="x-honeycomb-team=YOUR_API_KEY"
79+
leeway build :my-package
80+
```
81+
6482
## Span Attributes
6583

6684
### Root Span Attributes
@@ -165,13 +183,38 @@ docker run -d --name jaeger \
165183
-p 16686:16686 \
166184
jaegertracing/all-in-one:latest
167185

168-
# Build with tracing
186+
# Build with tracing (insecure for local development)
169187
export OTEL_EXPORTER_OTLP_ENDPOINT=localhost:4318
188+
export OTEL_EXPORTER_OTLP_INSECURE=true
170189
leeway build :my-package
171190

172191
# View traces at http://localhost:16686
173192
```
174193

194+
### With Honeycomb (Production)
195+
196+
```bash
197+
# Configure Honeycomb endpoint with API key
198+
export OTEL_EXPORTER_OTLP_ENDPOINT=api.honeycomb.io:443
199+
export OTEL_EXPORTER_OTLP_HEADERS="x-honeycomb-team=YOUR_API_KEY"
200+
201+
# Build with tracing (secure by default)
202+
leeway build :my-package
203+
204+
# View traces in Honeycomb UI
205+
```
206+
207+
### In CI/CD with Distributed Tracing
208+
209+
```bash
210+
# Propagate trace context from parent CI system
211+
export OTEL_EXPORTER_OTLP_ENDPOINT=api.honeycomb.io:443
212+
export OTEL_EXPORTER_OTLP_HEADERS="x-honeycomb-team=YOUR_API_KEY"
213+
export TRACEPARENT="00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01"
214+
215+
leeway build :my-package
216+
```
217+
175218
## Error Handling
176219

177220
Leeway implements graceful degradation for tracing:

pkg/leeway/telemetry/tracer.go

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package telemetry
22

33
import (
44
"context"
5-
"os"
65
"strings"
76
"time"
87

@@ -16,19 +15,34 @@ import (
1615
"golang.org/x/xerrors"
1716
)
1817

18+
// leewayVersion is set by the build system and used for telemetry
19+
var leewayVersion = "unknown"
20+
21+
// SetLeewayVersion sets the leeway version for telemetry reporting
22+
func SetLeewayVersion(version string) {
23+
if version != "" {
24+
leewayVersion = version
25+
}
26+
}
27+
1928
// InitTracer initializes the OpenTelemetry tracer with OTLP HTTP exporter.
2029
// The endpoint parameter specifies the OTLP endpoint URL (e.g., "localhost:4318").
30+
// The insecure parameter controls whether to use TLS (false = use TLS, true = no TLS).
2131
// Returns the TracerProvider which must be shut down when done.
22-
func InitTracer(ctx context.Context, endpoint string) (*sdktrace.TracerProvider, error) {
32+
func InitTracer(ctx context.Context, endpoint string, insecure bool) (*sdktrace.TracerProvider, error) {
2333
if endpoint == "" {
2434
return nil, xerrors.Errorf("OTLP endpoint not provided")
2535
}
2636

27-
// Create OTLP HTTP exporter
28-
exporter, err := otlptracehttp.New(ctx,
37+
// Create OTLP HTTP exporter with optional TLS
38+
opts := []otlptracehttp.Option{
2939
otlptracehttp.WithEndpoint(endpoint),
30-
otlptracehttp.WithInsecure(), // Use insecure for local development; configure TLS in production
31-
)
40+
}
41+
if insecure {
42+
opts = append(opts, otlptracehttp.WithInsecure())
43+
}
44+
45+
exporter, err := otlptracehttp.New(ctx, opts...)
3246
if err != nil {
3347
return nil, xerrors.Errorf("failed to create OTLP exporter: %w", err)
3448
}
@@ -114,14 +128,8 @@ func ParseTraceContext(traceparent, tracestate string) (context.Context, error)
114128
}
115129

116130
// getLeewayVersion returns the leeway version for telemetry.
117-
// This is a placeholder that should be replaced with actual version retrieval.
118131
func getLeewayVersion() string {
119-
// This will be imported from the leeway package
120-
version := os.Getenv("LEEWAY_VERSION")
121-
if version == "" {
122-
version = "unknown"
123-
}
124-
return version
132+
return leewayVersion
125133
}
126134

127135
// FormatTraceContext formats a span context into W3C Trace Context format.

pkg/leeway/telemetry/tracer_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ func TestFormatTraceContext_Invalid(t *testing.T) {
167167
}
168168

169169
func TestInitTracer_NoEndpoint(t *testing.T) {
170-
_, err := InitTracer(context.Background(), "")
170+
_, err := InitTracer(context.Background(), "", false)
171171
if err == nil {
172172
t.Error("InitTracer() should fail when endpoint is empty")
173173
}

0 commit comments

Comments
 (0)