Skip to content

Commit

Permalink
Log HTTP requests
Browse files Browse the repository at this point in the history
  • Loading branch information
thewilkybarkid committed Nov 30, 2023
1 parent 781de2b commit 112385f
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 4 deletions.
25 changes: 25 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
},
"dependencies": {
"@effect/platform": "^0.31.2",
"@effect/platform-node": "^0.32.3",
"@effect/schema": "^0.49.4",
"doi-ts": "^0.1.7",
"effect": "^2.0.0-next.56",
Expand Down
34 changes: 30 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,35 @@
import { HttpClient } from '@effect/platform'
import { Effect, Layer } from 'effect'
import { HttpClient } from '@effect/platform-node'
import { Effect, Layer, LogLevel, Logger } from 'effect'
import IoRedis from 'ioredis'
import { program } from './Program.js'
import * as Redis from './Redis.js'

const HttpClientLive = Layer.succeed(
HttpClient.client.Client,
HttpClient.client.makeDefault(request =>
Effect.Do.pipe(
Effect.tap(() => Effect.logDebug('Sending HTTP Request').pipe(Effect.annotateLogs({ headers: request.headers }))),
Effect.zipRight(HttpClient.client.fetch()(request)),
Effect.tap(response =>
Effect.logDebug('Received HTTP response').pipe(
Effect.annotateLogs({ status: response.status, headers: response.headers }),
),
),
Effect.tapErrorTag('RequestError', error =>
Effect.logError('Error sending HTTP request').pipe(
Effect.annotateLogs({ reason: error.reason, error: error.error }),
),
),
Effect.annotateLogs({
url: request.url,
urlParams: HttpClient.urlParams.toString(request.urlParams),
method: request.method,
}),
Effect.withLogSpan('fetch'),
),
),
)

const RedisLive = Layer.effect(
Redis.Redis,
Effect.gen(function* (_) {
Expand All @@ -15,8 +41,8 @@ const RedisLive = Layer.effect(
}),
)

const ProgramLive = Layer.mergeAll(HttpClient.client.layer, RedisLive)
const ProgramLive = Layer.mergeAll(HttpClientLive, RedisLive)

const runnable = Effect.provide(program, ProgramLive).pipe(Effect.scoped)
const runnable = Effect.provide(program, ProgramLive).pipe(Effect.scoped, Logger.withMinimumLogLevel(LogLevel.Debug))

await Effect.runPromise(runnable)

0 comments on commit 112385f

Please sign in to comment.