Skip to content

Commit

Permalink
feat: improve tracing (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
coadler authored Mar 27, 2023
1 parent 7e7d5e6 commit 11a01d3
Show file tree
Hide file tree
Showing 16 changed files with 243 additions and 116 deletions.
5 changes: 5 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Ignore everything
*

# Allow the tunnel binary
!/build/tunneld
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ LABEL \
RUN adduser -D -u 1000 tunneld
USER tunneld

COPY tunneld /
COPY ./build/tunneld /

CMD ["/tunneld"]
8 changes: 1 addition & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,11 @@ build/tunneld.tag: build/tunneld
version="$(VERSION)"
tag="ghcr.io/coder/wgtunnel/tunneld:$${version//+/-}"

# make a temp directory, copy the binary into it, and build the image.
temp_dir=$$(mktemp -d)
cp build/tunneld "$$temp_dir"

docker build \
--file Dockerfile \
--build-arg "WGTUNNEL_VERSION=$(VERSION)" \
--tag "$$tag" \
"$$temp_dir"
--tag "$$tag"

rm -rf "$$temp_dir"
echo "$$tag" > "$@"

test:
Expand Down
13 changes: 10 additions & 3 deletions cmd/tunneld/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"errors"
"io"
"log"
"net/http"
Expand Down Expand Up @@ -121,6 +122,11 @@ func main() {
Usage: "The Honeycomb team ID to send tracing data to. If not specified, tracing will not be shipped anywhere.",
EnvVars: []string{"TUNNELD_TRACING_HONEYCOMB_TEAM"},
},
&cli.StringFlag{
Name: "tracing-service-id",
Usage: "The service ID to annotate all traces with that uniquely identifies this deployment.",
EnvVars: []string{"TUNNELD_TRACING_SERVICE_ID"},
},
},
Action: runApp,
}
Expand All @@ -146,6 +152,7 @@ func runApp(ctx *cli.Context) error {
realIPHeader = ctx.String("real-ip-header")
pprofListenAddress = ctx.String("pprof-listen-address")
tracingHoneycombTeam = ctx.String("tracing-honeycomb-team")
tracingServiceID = ctx.String("tracing-service-id")
)
if baseURL == "" {
return xerrors.New("base-url is required. See --help for more information.")
Expand Down Expand Up @@ -173,12 +180,12 @@ func runApp(ctx *cli.Context) error {
if tracingHoneycombTeam != "" {
exp, err := newHoneycombExporter(ctx.Context, tracingHoneycombTeam)
if err != nil {
return xerrors.Errorf("failed to create honeycomb telemetry exporter: %w", err)
return xerrors.Errorf("create honeycomb telemetry exporter: %w", err)
}

// Create a new tracer provider with a batch span processor and the otlp
// exporter.
tp := newTraceProvider(exp)
tp := newTraceProvider(exp, tracingServiceID)
otel.SetTracerProvider(tp)
otel.SetTextMapPropagator(
propagation.NewCompositeTextMapPropagator(
Expand Down Expand Up @@ -210,7 +217,7 @@ func runApp(ctx *cli.Context) error {

if wireguardKeyFile != "" {
_, err = os.Stat(wireguardKeyFile)
if xerrors.Is(err, os.ErrNotExist) {
if errors.Is(err, os.ErrNotExist) {
logger.Info(ctx.Context, "generating private key to file", slog.F("path", wireguardKeyFile))
key, err := tunnelsdk.GeneratePrivateKey()
if err != nil {
Expand Down
8 changes: 6 additions & 2 deletions cmd/tunneld/tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import (
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
"go.opentelemetry.io/otel/sdk/resource"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
semconv "go.opentelemetry.io/otel/semconv/v1.11.0"
"google.golang.org/grpc/credentials"

"github.com/coder/wgtunnel/buildinfo"
)

func newHoneycombExporter(ctx context.Context, teamID string) (*otlptrace.Exporter, error) {
Expand All @@ -24,10 +26,12 @@ func newHoneycombExporter(ctx context.Context, teamID string) (*otlptrace.Export
return otlptrace.New(ctx, client)
}

func newTraceProvider(exp *otlptrace.Exporter) *sdktrace.TracerProvider {
func newTraceProvider(exp *otlptrace.Exporter, serviceID string) *sdktrace.TracerProvider {
rsc := resource.NewWithAttributes(
semconv.SchemaURL,
semconv.ServiceNameKey.String("WireguardTunnel"),
semconv.ServiceInstanceIDKey.String(serviceID),
semconv.ServiceVersionKey.String(buildinfo.Version()),
)

return sdktrace.NewTracerProvider(
Expand Down
2 changes: 2 additions & 0 deletions compose/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CLOUDFLARE_TOKEN=
HONEYCOMB_TEAM=
1 change: 1 addition & 0 deletions compose/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.env
17 changes: 17 additions & 0 deletions compose/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Use a single bash shell for each job, and immediately exit on failure
SHELL := bash
.SHELLFLAGS := -ceu
.ONESHELL:

# Don't print the commands in the file unless you specify VERBOSE. This is
# essentially the same as putting "@" at the start of each line.
ifndef VERBOSE
.SILENT:
endif

up:
pushd ..
make -B build
popd
docker compose -p wgtunnel up --build
.PHONY: up
12 changes: 12 additions & 0 deletions compose/caddy/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
ARG CADDY_VERSION=2.6.4
FROM caddy:${CADDY_VERSION}-builder AS builder

RUN xcaddy build \
--with github.com/lucaslorentz/caddy-docker-proxy/v2 \
--with github.com/caddy-dns/cloudflare

FROM caddy:${CADDY_VERSION}

COPY --from=builder /usr/bin/caddy /usr/bin/caddy

CMD ["caddy", "docker-proxy"]
47 changes: 47 additions & 0 deletions compose/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
version: "3.9"
services:
caddy:
build: ./caddy
ports:
- 8080:80
- 4443:443
environment:
- CADDY_INGRESS_NETWORKS=caddy
networks:
- caddy
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- caddy_data:/data
restart: unless-stopped

tunnel:
build: ..
restart: always
ports:
- 55551:55551/udp
networks:
- caddy
environment:
TUNNELD_LISTEN_ADDRESS: "0.0.0.0:8080"
TUNNELD_BASE_URL: "https://local.try.coder.app:4443"
TUNNELD_WIREGUARD_ENDPOINT: "local.try.coder.app:55551"
TUNNELD_WIREGUARD_PORT: "55551"
TUNNELD_WIREGUARD_KEY_FILE: "/home/tunneld/wg.key"
TUNNELD_WIREGUARD_MTU: "1280"
TUNNELD_WIREGUARD_SERVER_IP: "fcca::1"
TUNNELD_WIREGUARD_NETWORK_PREFIX: "fcca::/16"
TUNNELD_REAL_IP_HEADER: "X-Forwarded-For"
TUNNELD_PPROF_LISTEN_ADDRESS: "127.0.0.1:6060"
TUNNELD_TRACING_HONEYCOMB_TEAM: "${HONEYCOMB_TEAM}"
TUNNELD_TRACING_INSTANCE_ID: "local"
labels:
caddy: "local.try.coder.app, *.local.try.coder.app"
caddy.reverse_proxy: "{{upstreams 8080}}"
caddy.tls.dns: cloudflare ${CLOUDFLARE_TOKEN}

networks:
caddy:
external: true

volumes:
caddy_data: {}
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ go 1.20

require (
cdr.dev/slog v1.4.1
github.com/go-chi/chi v1.5.4
github.com/go-chi/chi/v5 v5.0.8
github.com/go-chi/hostrouter v0.2.0
github.com/go-chi/httprate v0.7.1
github.com/riandyrn/otelchi v0.5.1
github.com/stretchr/testify v1.8.1
github.com/urfave/cli/v2 v2.24.4
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.36.4
Expand Down Expand Up @@ -44,6 +46,7 @@ require (
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/contrib v1.0.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.11.1 // indirect
go.opentelemetry.io/otel/metric v0.33.0 // indirect
go.opentelemetry.io/proto/otlp v0.19.0 // indirect
Expand Down
19 changes: 17 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -95,19 +95,26 @@ github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7
github.com/fatih/color v1.12.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM=
github.com/fatih/color v1.14.1 h1:qfhVLaG5s+nCROl1zJsZRxFeYrHLqWroPOQ8BWiNb4w=
github.com/fatih/color v1.14.1/go.mod h1:2oHN61fhTpgcxD3TSWCgKDiH1+x4OiDVVGH8WlgGZGg=
github.com/felixge/httpsnoop v1.0.2/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/felixge/httpsnoop v1.0.3 h1:s/nj+GCswXYzN5v2DpNMuMQYe+0DDwt5WVCU6CWBdXk=
github.com/felixge/httpsnoop v1.0.3/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/go-chi/chi v1.5.4 h1:QHdzF2szwjqVV4wmByUnTcsbIg7UGaQ0tPF2t5GcAIs=
github.com/go-chi/chi v1.5.4/go.mod h1:uaf8YgoFazUOkPBG7fxPftUylNumIev9awIWOENIuEg=
github.com/go-chi/chi/v5 v5.0.0/go.mod h1:BBug9lr0cqtdAhsu6R4AAdvufI0/XBzAQSsUqJpoZOs=
github.com/go-chi/chi/v5 v5.0.8 h1:lD+NLqFcAi1ovnVZpsnObHGW4xb4J8lNmoYVfECH1Y0=
github.com/go-chi/chi/v5 v5.0.8/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/go-chi/hostrouter v0.2.0 h1:GwC7TZz8+SlJN/tV/aeJgx4F+mI5+sp+5H1PelQUjHM=
github.com/go-chi/hostrouter v0.2.0/go.mod h1:pJ49vWVmtsKRKZivQx0YMYv4h0aX+Gcn6V23Np9Wf1s=
github.com/go-chi/httprate v0.7.1 h1:d5kXARdms2PREQfU4pHvq44S6hJ1hPu4OXLeBKmCKWs=
github.com/go-chi/httprate v0.7.1/go.mod h1:6GOYBSwnpra4CQfAKXu8sQZg+nZ0M1g9QnyFvxrAB8A=
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-logr/logr v1.2.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/logr v1.2.1/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/logr v1.2.3 h1:2DntVwHkVopvECVRSlL5PSo9eG+cAkDCuckLubN+rq0=
github.com/go-logr/logr v1.2.3/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/stdr v1.2.0/go.mod h1:YkVgnZu1ZjjL7xTxrfm/LLZBfkhTqSR1ydtm6jTKKwI=
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
Expand Down Expand Up @@ -211,6 +218,8 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/riandyrn/otelchi v0.5.1 h1:0/45omeqpP7f/cvdL16GddQBfAEmZvUyl2QzLSE6uYo=
github.com/riandyrn/otelchi v0.5.1/go.mod h1:ZxVxNEl+jQ9uHseRYIxKWRb3OY8YXFEu+EkNiiSNUEA=
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
Expand Down Expand Up @@ -248,8 +257,11 @@ go.opencensus.io v0.22.5/go.mod h1:5pWMHQbX5EPX2/62yrJeAkowc+lfs/XD7Uxpq3pI6kk=
go.opencensus.io v0.23.0/go.mod h1:XItmlyltB5F7CS4xOC1DcqMoFqwtC6OG2xF7mCv7P7E=
go.opencensus.io v0.24.0 h1:y73uSU6J157QMP2kn2r30vwW1A2W2WFwSCGnAVxeaD0=
go.opencensus.io v0.24.0/go.mod h1:vNK8G9p7aAivkbmorf4v+7Hgx+Zs0yY+0fOtgBfjQKo=
go.opentelemetry.io/contrib v1.0.0 h1:khwDCxdSspjOLmFnvMuSHd/5rPzbTx0+l6aURwtQdfE=
go.opentelemetry.io/contrib v1.0.0/go.mod h1:EH4yDYeNoaTqn/8yCWQmfNB78VHfGX2Jt2bvnvzBlGM=
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.36.4 h1:aUEBEdCa6iamGzg6fuYxDA8ThxvOG240mAvWDU+XLio=
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.36.4/go.mod h1:l2MdsbKTocpPS5nQZscqTR9jd8u96VYZdcpF8Sye7mA=
go.opentelemetry.io/otel v1.3.0/go.mod h1:PWIKzi6JCp7sM0k9yZ43VX+T345uNbAkDKwHVjb2PTs=
go.opentelemetry.io/otel v1.11.1 h1:4WLLAmcfkmDk2ukNXJyq3/kiz/3UzCaYq6PskJsaou4=
go.opentelemetry.io/otel v1.11.1/go.mod h1:1nNhXBbWSD0nsL38H6btgnFN2k4i0sNLHNNMZMSbUGE=
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.11.1 h1:X2GndnMCsUPh6CiY2a+frAbNsXaPLbB0soHRYhAZ5Ig=
Expand All @@ -260,8 +272,10 @@ go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.11.1 h1:LYyG/
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.11.1/go.mod h1:QrRRQiY3kzAoYPNLP0W/Ikg0gR6V3LMc+ODSxr7yyvg=
go.opentelemetry.io/otel/metric v0.33.0 h1:xQAyl7uGEYvrLAiV/09iTJlp1pZnQ9Wl793qbVvED1E=
go.opentelemetry.io/otel/metric v0.33.0/go.mod h1:QlTYc+EnYNq/M2mNk1qDDMRLpqCOj2f/r5c7Fd5FYaI=
go.opentelemetry.io/otel/sdk v1.3.0/go.mod h1:rIo4suHNhQwBIPg9axF8V9CA72Wz2mKF1teNrup8yzs=
go.opentelemetry.io/otel/sdk v1.11.1 h1:F7KmQgoHljhUuJyA+9BiU+EkJfyX5nVVF4wyzWZpKxs=
go.opentelemetry.io/otel/sdk v1.11.1/go.mod h1:/l3FE4SupHJ12TduVjUkZtlfFqDCQJlOlithYrdktys=
go.opentelemetry.io/otel/trace v1.3.0/go.mod h1:c/VDhno8888bvQYmbYLqe41/Ldmr/KKunbvWM4/fEjk=
go.opentelemetry.io/otel/trace v1.11.1 h1:ofxdnzsNrGBYXbP7t7zpUK281+go5rF7dvdIZXF8gdQ=
go.opentelemetry.io/otel/trace v1.11.1/go.mod h1:f/Q9G7vzk5u91PhbmKbg1Qn0rzH1LJ4vbPHFGkTPtOk=
go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqeYNgFYFoEGnI=
Expand Down Expand Up @@ -414,6 +428,7 @@ golang.org/x/sys v0.0.0-20210315160823-c6e025ad8005/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210514084401-e8d321eab015/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down
Loading

0 comments on commit 11a01d3

Please sign in to comment.