Skip to content

Commit

Permalink
make Http.middleware.withTracerDisabledWhen a Layer api (#2491)
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-smart committed Apr 16, 2024
1 parent 5fbab74 commit 3fe683c
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 1 deletion.
21 changes: 21 additions & 0 deletions .changeset/grumpy-pens-compare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
"@effect/platform": minor
---

make Http.middleware.withTracerDisabledWhen a Layer api

And add Http.middleware.withTracerDisabledWhenEffect to operate on Effect's.

Usage is now:

```ts
import * as Http from "@effect/platform/HttpServer";

Http.router.empty.pipe(
Http.router.get("/health"),
Http.server.serve(),
Http.middleware.withTracerDisabledWhen(
(request) => request.url === "/no-tracing",
),
);
```
17 changes: 17 additions & 0 deletions .changeset/healthy-trees-crash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
"@effect/platform": patch
---

add Http.middleware.withTracerDisabledForUrls

Allows you to disable the http server tracer for the given urls:

```ts
import * as Http from "@effect/platform/HttpServer";

Http.router.empty.pipe(
Http.router.get("/health"),
Http.server.serve(),
Http.middleware.withTracerDisabledForUrls(["/health"])
);
```
26 changes: 25 additions & 1 deletion packages/platform/src/Http/Middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
import type * as Effect from "effect/Effect"
import type * as FiberRef from "effect/FiberRef"
import type * as Layer from "effect/Layer"
import type * as Predicate from "effect/Predicate"
import * as internal from "../internal/http/middleware.js"
import type * as App from "./App.js"
Expand Down Expand Up @@ -65,14 +66,37 @@ export const currentTracerDisabledWhen: FiberRef.FiberRef<Predicate.Predicate<Se
* @category fiber refs
*/
export const withTracerDisabledWhen: {
(
predicate: Predicate.Predicate<ServerRequest.ServerRequest>
): <R, E, A>(layer: Layer.Layer<A, E, R>) => Layer.Layer<A, E, R>
<R, E, A>(
layer: Layer.Layer<A, E, R>,
predicate: Predicate.Predicate<ServerRequest.ServerRequest>
): Layer.Layer<A, E, R>
} = internal.withTracerDisabledWhen

/**
* @since 1.0.0
* @category fiber refs
*/
export const withTracerDisabledWhenEffect: {
(
predicate: Predicate.Predicate<ServerRequest.ServerRequest>
): <R, E, A>(effect: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>
<R, E, A>(
effect: Effect.Effect<A, E, R>,
predicate: Predicate.Predicate<ServerRequest.ServerRequest>
): Effect.Effect<A, E, R>
} = internal.withTracerDisabledWhen
} = internal.withTracerDisabledWhenEffect

/**
* @since 1.0.0
* @category fiber refs
*/
export const withTracerDisabledForUrls: {
(urls: ReadonlyArray<string>): <R, E, A>(layer: Layer.Layer<A, E, R>) => Layer.Layer<A, E, R>
<R, E, A>(layer: Layer.Layer<A, E, R>, urls: ReadonlyArray<string>): Layer.Layer<A, E, R>
} = internal.withTracerDisabledForUrls

/**
* @since 1.0.0
Expand Down
23 changes: 23 additions & 0 deletions packages/platform/src/internal/http/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as Effect from "effect/Effect"
import * as FiberRef from "effect/FiberRef"
import { constFalse, dual } from "effect/Function"
import { globalValue } from "effect/GlobalValue"
import * as Layer from "effect/Layer"
import * as Option from "effect/Option"
import type * as Predicate from "effect/Predicate"
import * as Headers from "../../Http/Headers.js"
Expand Down Expand Up @@ -36,6 +37,17 @@ export const currentTracerDisabledWhen = globalValue(

/** @internal */
export const withTracerDisabledWhen = dual<
(
predicate: Predicate.Predicate<ServerRequest.ServerRequest>
) => <R, E, A>(layer: Layer.Layer<A, E, R>) => Layer.Layer<A, E, R>,
<R, E, A>(
layer: Layer.Layer<A, E, R>,
predicate: Predicate.Predicate<ServerRequest.ServerRequest>
) => Layer.Layer<A, E, R>
>(2, (self, pred) => Layer.locally(self, currentTracerDisabledWhen, pred))

/** @internal */
export const withTracerDisabledWhenEffect = dual<
(
predicate: Predicate.Predicate<ServerRequest.ServerRequest>
) => <R, E, A>(effect: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>,
Expand All @@ -45,6 +57,17 @@ export const withTracerDisabledWhen = dual<
) => Effect.Effect<A, E, R>
>(2, (self, pred) => Effect.locally(self, currentTracerDisabledWhen, pred))

/** @internal */
export const withTracerDisabledForUrls = dual<
(
urls: ReadonlyArray<string>
) => <R, E, A>(layer: Layer.Layer<A, E, R>) => Layer.Layer<A, E, R>,
<R, E, A>(
layer: Layer.Layer<A, E, R>,
urls: ReadonlyArray<string>
) => Layer.Layer<A, E, R>
>(2, (self, urls) => Layer.locally(self, currentTracerDisabledWhen, (req) => urls.includes(req.url)))

/** @internal */
export const logger = make((httpApp) => {
let counter = 0
Expand Down

0 comments on commit 3fe683c

Please sign in to comment.