forked from effect-app/boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobservability.ts
More file actions
116 lines (98 loc) · 3.73 KB
/
observability.ts
File metadata and controls
116 lines (98 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/* eslint-disable @typescript-eslint/no-explicit-any */
import { dropUndefinedT } from "@effect-app/core/utils"
import * as Resource from "@effect/opentelemetry/Resource"
import * as Tracer from "@effect/opentelemetry/Tracer"
import { getNodeAutoInstrumentations } from "@opentelemetry/auto-instrumentations-node"
import { AsyncLocalStorageContextManager } from "@opentelemetry/context-async-hooks"
import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-http"
import opentelemetry from "@opentelemetry/sdk-node"
import type { Span } from "@opentelemetry/sdk-trace-node"
import { AlwaysOffSampler, BatchSpanProcessor, ConsoleSpanExporter } from "@opentelemetry/sdk-trace-node"
import * as Sentry from "@sentry/node"
import {
getCurrentHub,
SentryPropagator,
SentrySampler,
SentrySpanProcessor,
setOpenTelemetryContextAsyncContextStrategy,
setupEventContextTrace,
setupGlobalHub,
wrapContextManagerClass
} from "@sentry/opentelemetry"
import { Effect, Layer, Secret } from "effect-app"
import tcpPortUsed from "tcp-port-used"
import { BaseConfig } from "./config"
const appConfig = BaseConfig.runSync
class SentryFilteredSpanProcessor extends SentrySpanProcessor {
override _shouldSendSpanToSentry(span: Span): boolean {
return span.attributes["http.url"] !== "/.well-known/local/server-health"
}
}
const NodeSdkLive = Effect
.gen(function*($) {
const isRemote = appConfig.env !== "local-dev"
// TODO: use this on frontend trace proxy too
const isTelemetryExporterRunning = !isRemote
&& (yield* $(Effect.promise<boolean>(() => tcpPortUsed.check(4318, "localhost"))))
let props: Partial<opentelemetry.NodeSDKConfiguration> = dropUndefinedT({
sampler: isTelemetryExporterRunning ? undefined : new AlwaysOffSampler(),
spanProcessor: new BatchSpanProcessor(
isTelemetryExporterRunning
? new OTLPTraceExporter({
url: "http://localhost:4318/v1/traces"
})
: new ConsoleSpanExporter()
)
})
if (isRemote) {
setupGlobalHub()
}
Sentry.init(dropUndefinedT({
dsn: Secret.value(appConfig.sentry.dsn),
environment: appConfig.env,
enabled: isRemote,
release: appConfig.apiVersion,
// Set tracesSampleRate to 1.0 to capture 100%
// of transactions for performance monitoring.
// We recommend adjusting this value in production
tracesSampleRate: 1.0,
// set the instrumenter to use OpenTelemetry instead of Sentry
instrumenter: isRemote ? "otel" : undefined
}))
const resource = yield* $(Resource.Resource)
if (isRemote) {
const client = getCurrentHub().getClient()!
setupEventContextTrace(client)
// You can wrap whatever local storage context manager you want to use
const SentryContextManager = wrapContextManagerClass(
AsyncLocalStorageContextManager
)
props = {
// Sentry config
spanProcessor: new SentryFilteredSpanProcessor(),
textMapPropagator: new SentryPropagator(),
contextManager: new SentryContextManager(),
sampler: new SentrySampler(client)
}
}
const sdk = new opentelemetry.NodeSDK({
traceExporter: new OTLPTraceExporter(),
instrumentations: [getNodeAutoInstrumentations()],
resource,
...props
})
// Ensure OpenTelemetry Context & Sentry Hub/Scope is synced
setOpenTelemetryContextAsyncContextStrategy()
yield* $(Effect.addFinalizer(() => Effect.promise(() => sdk.shutdown())))
sdk.start()
})
.pipe(Layer.scopedDiscard)
export const TracingLive = Tracer.layerGlobal.pipe(
Layer.provide(NodeSdkLive),
Layer.provide(
Resource.layer({
serviceName: appConfig.serviceName,
serviceVersion: appConfig.apiVersion
})
)
)