Skip to content

Commit 7faf637

Browse files
Rintaro Taguchiclaude
andcommitted
Remove PublicEndpoint bool in favor of PublicEndpointFn only
Per review feedback: the bool field was redundant sugar over PublicEndpointFn returning a constant. Document the always-public case as an example on PublicEndpointFn instead. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent 909d5db commit 7faf637

3 files changed

Lines changed: 22 additions & 25 deletions

File tree

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,17 @@ that trace as the parent of the server span. For endpoints exposed to untrusted
5151
callers to inject arbitrary trace IDs into your traces or suppress tracing entirely with a
5252
`sampled=0` flag.
5353

54-
Set `PublicEndpoint` to start a new trace for every request instead. The incoming trace context,
55-
if present, is recorded as a span link rather than being used as the parent.
54+
Set `PublicEndpointFn` to start a new trace instead. The incoming trace context,
55+
if present, is recorded as a span link rather than being used as the parent. To treat every
56+
request as public:
5657

5758
```go
5859
e.Use(echootel.NewMiddlewareWithConfig(echootel.Config{
59-
PublicEndpoint: true,
60+
PublicEndpointFn: func(c *echo.Context, remote trace.SpanContext) bool { return true },
6061
}))
6162
```
6263

63-
Use `PublicEndpointFn` to decide per request, for example when the same server serves both
64-
internal and public routes
64+
The decision is made per request, so the same server can serve both internal and public routes
6565

6666
```go
6767
e.Use(echootel.NewMiddlewareWithConfig(echootel.Config{

otel.go

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,26 +48,23 @@ type Config struct {
4848
// Skipper defines a function to skip middleware.
4949
Skipper middleware.Skipper
5050

51-
// PublicEndpoint indicates that this middleware serves a public (internet-facing) endpoint
52-
// receiving requests from untrusted clients.
51+
// PublicEndpointFn decides per request whether it should be handled as a public
52+
// (internet-facing) endpoint receiving requests from untrusted clients.
5353
//
54-
// When enabled, the incoming trace context (e.g. `traceparent`/`tracestate` headers) is not
55-
// used as the parent of the server span. Instead, a new root span (new trace) is started
56-
// and the incoming remote span context, if valid, is recorded as a span link. This prevents
57-
// untrusted clients from injecting arbitrary trace IDs into your traces or influencing the
58-
// sampling decision (e.g. suppressing tracing with a `sampled=0` flag).
59-
PublicEndpoint bool
60-
61-
// PublicEndpointFn allows deciding per request whether it should be handled as a public
62-
// endpoint (see PublicEndpoint for the behavior). Requests for which the function returns
63-
// true are treated as public endpoint requests.
54+
// When the function returns true, the incoming trace context (e.g. `traceparent`/`tracestate`
55+
// headers) is not used as the parent of the server span. Instead, a new root span (new trace)
56+
// is started and the incoming remote span context, if valid, is recorded as a span link. This
57+
// prevents untrusted clients from injecting arbitrary trace IDs into your traces or
58+
// influencing the sampling decision (e.g. suppressing tracing with a `sampled=0` flag).
59+
//
60+
// To treat every request as public:
61+
//
62+
// config.PublicEndpointFn = func(c *echo.Context, remote oteltrace.SpanContext) bool { return true }
6463
//
6564
// The remote span context extracted from the incoming request by Propagators is passed as
6665
// the second argument. It can be invalid (see trace.SpanContext.IsValid) when the request
6766
// carries no trace context. This allows, for example, trusting only trace contexts that
6867
// originate from known internal systems.
69-
//
70-
// This function is only called when PublicEndpoint is false.
7168
PublicEndpointFn func(c *echo.Context, remote oteltrace.SpanContext) bool
7269

7370
// OnNextError is used to specify how errors returned from the next middleware / handler are handled.
@@ -214,7 +211,7 @@ func (config Config) ToMiddleware() (echo.MiddlewareFunc, error) {
214211

215212
ctx := config.Propagators.Extract(request.Context(), propagation.HeaderCarrier(request.Header))
216213
remote := oteltrace.SpanContextFromContext(ctx)
217-
if config.PublicEndpoint || (config.PublicEndpointFn != nil && config.PublicEndpointFn(c, remote)) {
214+
if config.PublicEndpointFn != nil && config.PublicEndpointFn(c, remote) {
218215
spanStartOptions = append(spanStartOptions, oteltrace.WithNewRoot())
219216
// keep the incoming (untrusted) trace context visible by linking it to the new root span
220217
if remote.IsValid() && remote.IsRemote() {

otel_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func TestPropagationWithCustomPropagators(t *testing.T) {
105105
assert.Equal(t, http.StatusOK, w.Result().StatusCode, "should call the 'user' handler")
106106
}
107107

108-
func TestPublicEndpoint(t *testing.T) {
108+
func TestPublicEndpointFnAlwaysPublic(t *testing.T) {
109109
tests := []struct {
110110
name string
111111
traceFlags trace.TraceFlags
@@ -134,10 +134,10 @@ func TestPublicEndpoint(t *testing.T) {
134134

135135
e := echo.New()
136136
e.Use(NewMiddlewareWithConfig(Config{
137-
ServerName: "foobar",
138-
TracerProvider: tp,
139-
Propagators: prop,
140-
PublicEndpoint: true,
137+
ServerName: "foobar",
138+
TracerProvider: tp,
139+
Propagators: prop,
140+
PublicEndpointFn: func(c *echo.Context, remote trace.SpanContext) bool { return true },
141141
}))
142142
e.GET("/user/:id", func(c *echo.Context) error {
143143
return c.NoContent(http.StatusOK)

0 commit comments

Comments
 (0)