caddy icon indicating copy to clipboard operation
caddy copied to clipboard

Use HTTP tracer if env var is set

Open trajano opened this issue 1 year ago • 7 comments

Fixes https://github.com/caddyserver/caddy/issues/5743

Tested via example from https://github.com/trajano/trajano-portfolio/blob/master/Dockerfile#L17-L24

FROM caddy:builder AS builder
RUN git clone https://github.com/trajano/caddy /mnt/
RUN xcaddy build \
  --with github.com/caddyserver/caddy/v2=github.com/trajano/caddy/v2@ea6a6298f5ef94efef2900af22d200583ca09f3f

FROM caddy:alpine
COPY --from=builder /usr/bin/caddy /usr/bin/caddy

Using Grafana.com to receive the OTEL traces.

This would require a change to the tracing documentation https://caddyserver.com/docs/caddyfile/directives/tracing#tracing

Enables integration with OpenTelemetry tracing facilities, using [opentelemetry-go](https://github.com/open-telemetry/opentelemetry-go) and [opentelemetry-go-contrib](https://github.com/open-telemetry/opentelemetry-go-contrib) .

When enabled, it will propagate an existing trace context or initialize a new one.

It uses the exporter protocol specified by `OTEL_EXPORTER_OTLP_PROTOCOL` and the uses [gRPC](https://github.com/grpc/) as the default.  In addition it uses W3C [tracecontext](https://www.w3.org/TR/trace-context/) and [baggage](https://www.w3.org/TR/baggage/) as propagators.

The trace ID is added to [access logs](https://caddyserver.com/docs/caddyfile/directives/log) as the standard traceID field.  The trace ID is also set as the `http.vars.trace_id` placeholder value.

trajano avatar Jul 28 '24 09:07 trajano

CLA assistant check
All committers have signed the CLA.

CLAassistant avatar Jul 28 '24 09:07 CLAassistant

Added another commit primarily to offload the work to OTEL's autoexport function.

Hopefully this will eventually allow us to send logs to OTEL as well.

trajano avatar Jul 29 '24 12:07 trajano

Had to revert that last commit. It didn't seem to work as advertised.

trajano avatar Jul 29 '24 14:07 trajano

Un reverting the revert. My test was actually flawed and it was working

trajano avatar Jul 29 '24 17:07 trajano

Note this will just get it to function the same way as before. There's a lot missing in Caddy's OTEL messages. To name a fiew

  • there's no Span.Kind pieces for the Client portion so there's no offload shown in the traces from Caddy to the reverse proxy. #6487 will add that
  • Status should be ok not just left as unset when it's the final response.
  • The foward_auth appears as a separate trace
  • The OTEL exports only supports tracing but not others like logs or metrics

trajano avatar Jul 29 '24 17:07 trajano

Thanks for working on this. We'll revisit this shortly as we prepare the 2.9 beta releases! In the meantime how would you feel about resolving the merge conflicts

mholt avatar Oct 02 '24 14:10 mholt

Thanks for working on this. We'll revisit this shortly as we prepare the 2.9 beta releases! In the meantime how would you feel about resolving the merge conflicts

maybe this weekend, I only work on it on during my hobby hours

trajano avatar Oct 02 '24 17:10 trajano

Hi, we wanted to use the otel export for traces and have some trouble with it. After some digging we found out that it seems to ignore the env OTEL_EXPORTER_OTLP_PROTOCOL and always uses gRPC. This exactly looks like what this PR is fixing.

Will this PR be merged any time? Thank you

PKeidel avatar Oct 23 '25 18:10 PKeidel

Yeah we can probably merge it -- but first the go.mod and go.sum changes should be dropped, and we should update it with the latest in master first. @trajano Any interest in making those changes so we can merge it?

If not, another PR would be welcomed.

mholt avatar Oct 23 '25 18:10 mholt

Let me check I haven't done this update for a while

In case you need to verify @PKeidel this is my Dockerfile

FROM caddy:builder-alpine AS builder
RUN --mount=type=cache,target=/go/pkg/mod/cache xcaddy build \
  --with github.com/caddyserver/caddy/v2=github.com/trajano/caddy/v2@otel-client \
  --with github.com/caddyserver/cache-handler \
  --with github.com/darkweak/storages/nuts/caddy \
  --with github.com/darkweak/storages/redis/caddy
  #--with github.com/caddyserver/caddy/v2=github.com/trajano/caddy/v2@1f6b8322bd1828de43db038483f79196d8a094f4
  #--with github.com/caddyserver/caddy/v2=github.com/trajano/caddy/v2@ea6a6298f5ef94efef2900af22d200583ca09f3f

FROM caddy:alpine
COPY --from=builder /usr/bin/caddy /usr/bin/caddy

trajano avatar Oct 23 '25 18:10 trajano

Actually someone else can make the PR, my actual changes spanned quite a few things after already like adding tracing down to the lower levels but I think that's being done by master now which is why I abandoned this PR long ago. Hopefully it goes through with that simple change though.

trajano avatar Oct 23 '25 18:10 trajano

Alrighty. @PKeidel Do you want to make a PR?

mholt avatar Oct 23 '25 18:10 mholt

I created https://github.com/caddyserver/caddy/pull/7317

PKeidel avatar Oct 23 '25 20:10 PKeidel