Skip to content

Commit

Permalink
improve client span
Browse files Browse the repository at this point in the history
  • Loading branch information
patroza committed May 17, 2024
1 parent b8c7744 commit a67d115
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/beige-lemons-watch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect-app": patch
---

improve client trace
25 changes: 12 additions & 13 deletions packages/prelude/src/client/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { Effect, HashMap, Option } from "@effect-app/core"
import { constant } from "@effect-app/core/Function"
import { constant, flow } from "@effect-app/core/Function"
import type { Headers, HttpError, HttpRequestError, HttpResponseError, Method } from "@effect-app/core/http/http-client"
import { Record } from "effect"
import type { REST, Schema } from "effect-app/schema"
Expand Down Expand Up @@ -147,10 +147,7 @@ export function fetchApi(
: HttpClientRequest
.make(method)(path)
.pipe(HttpClientRequest.jsonBody(body), Effect.flatMap(client)))
.pipe(
Effect.scoped,
Effect.withSpan("http.request", { attributes: { "http.method": method, "http.url": path } })
))
.pipe(Effect.scoped))
}

export function fetchApi2S<RequestR, RequestFrom, RequestTo, ResponseR, ResponseFrom, ResponseTo>(
Expand All @@ -160,6 +157,15 @@ export function fetchApi2S<RequestR, RequestFrom, RequestTo, ResponseR, Response
const encodeRequest = S.encode(request)
const decRes = S.decodeUnknown(response)
const decodeRes = (u: unknown) => Effect.mapError(decRes(u), (err) => new ResError(err))
const parse = flow(
mapResponseM(decodeRes),
Effect.map((i) => ({
...i,
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
body: i.body as ResponseTo
})),
Effect.withSpan("client.decode")
)
return (method: Method, path: Path) => (req: RequestTo) => {
return Effect.andThen(encodeRequest(req), (encoded) =>
fetchApi(
Expand All @@ -170,14 +176,7 @@ export function fetchApi2S<RequestR, RequestFrom, RequestTo, ResponseR, Response
: makePathWithBody(path, encoded as any),
encoded
)
.pipe(
Effect.flatMap(mapResponseM(decodeRes)),
Effect.map((i) => ({
...i,
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
body: i.body as ResponseTo
}))
))
.pipe(Effect.flatMap(parse)))
}
}

Expand Down

0 comments on commit a67d115

Please sign in to comment.