@effect-gql/opentelemetry 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,177 @@
1
+ import { Effect, Context } from "effect"
2
+ import { HttpServerRequest } from "@effect/platform"
3
+
4
+ /**
5
+ * W3C Trace Context header names
6
+ * @see https://www.w3.org/TR/trace-context/
7
+ */
8
+ export const TRACEPARENT_HEADER = "traceparent"
9
+ export const TRACESTATE_HEADER = "tracestate"
10
+
11
+ /**
12
+ * Parsed W3C Trace Context from incoming HTTP headers
13
+ */
14
+ export interface TraceContext {
15
+ /**
16
+ * Version of the trace context format (always "00" for current spec)
17
+ */
18
+ readonly version: string
19
+
20
+ /**
21
+ * Trace ID - 32 character lowercase hex string
22
+ */
23
+ readonly traceId: string
24
+
25
+ /**
26
+ * Parent Span ID - 16 character lowercase hex string
27
+ */
28
+ readonly parentSpanId: string
29
+
30
+ /**
31
+ * Trace flags - determines if trace is sampled
32
+ * Bit 0 (0x01) = sampled flag
33
+ */
34
+ readonly traceFlags: number
35
+
36
+ /**
37
+ * Optional trace state from upstream services
38
+ */
39
+ readonly traceState?: string
40
+ }
41
+
42
+ /**
43
+ * Context tag for TraceContext
44
+ */
45
+ export class TraceContextTag extends Context.Tag("@effect-gql/opentelemetry/TraceContext")<
46
+ TraceContextTag,
47
+ TraceContext
48
+ >() {}
49
+
50
+ /**
51
+ * Parse a W3C traceparent header value.
52
+ *
53
+ * Format: {version}-{trace-id}-{parent-id}-{trace-flags}
54
+ * Example: 00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01
55
+ *
56
+ * @see https://www.w3.org/TR/trace-context/#traceparent-header
57
+ *
58
+ * @param header - The traceparent header value
59
+ * @returns Parsed trace context or null if invalid
60
+ */
61
+ export const parseTraceParent = (header: string): TraceContext | null => {
62
+ const trimmed = header.trim().toLowerCase()
63
+ const parts = trimmed.split("-")
64
+
65
+ if (parts.length !== 4) {
66
+ return null
67
+ }
68
+
69
+ const [version, traceId, parentSpanId, flagsHex] = parts
70
+
71
+ // Validate version (2 hex chars)
72
+ if (version.length !== 2 || !/^[0-9a-f]{2}$/.test(version)) {
73
+ return null
74
+ }
75
+
76
+ // Validate trace ID (32 hex chars, not all zeros)
77
+ if (traceId.length !== 32 || !/^[0-9a-f]{32}$/.test(traceId)) {
78
+ return null
79
+ }
80
+ if (traceId === "00000000000000000000000000000000") {
81
+ return null
82
+ }
83
+
84
+ // Validate parent span ID (16 hex chars, not all zeros)
85
+ if (parentSpanId.length !== 16 || !/^[0-9a-f]{16}$/.test(parentSpanId)) {
86
+ return null
87
+ }
88
+ if (parentSpanId === "0000000000000000") {
89
+ return null
90
+ }
91
+
92
+ // Validate trace flags (2 hex chars)
93
+ if (flagsHex.length !== 2 || !/^[0-9a-f]{2}$/.test(flagsHex)) {
94
+ return null
95
+ }
96
+
97
+ const traceFlags = parseInt(flagsHex, 16)
98
+
99
+ return {
100
+ version,
101
+ traceId,
102
+ parentSpanId,
103
+ traceFlags,
104
+ }
105
+ }
106
+
107
+ /**
108
+ * Check if a trace context is sampled (should be recorded)
109
+ */
110
+ export const isSampled = (context: TraceContext): boolean => {
111
+ return (context.traceFlags & 0x01) === 0x01
112
+ }
113
+
114
+ /**
115
+ * Extract trace context from HTTP request headers.
116
+ *
117
+ * Looks for the W3C Trace Context headers:
118
+ * - `traceparent`: Required, contains trace ID, span ID, and flags
119
+ * - `tracestate`: Optional, vendor-specific trace data
120
+ *
121
+ * @example
122
+ * ```typescript
123
+ * const context = yield* extractTraceContext
124
+ * if (context) {
125
+ * console.log(`Continuing trace: ${context.traceId}`)
126
+ * }
127
+ * ```
128
+ */
129
+ export const extractTraceContext: Effect.Effect<
130
+ TraceContext | null,
131
+ never,
132
+ HttpServerRequest.HttpServerRequest
133
+ > = Effect.gen(function* () {
134
+ const request = yield* HttpServerRequest.HttpServerRequest
135
+ const headers = request.headers
136
+
137
+ // Get traceparent header (case-insensitive)
138
+ const traceparentKey = Object.keys(headers).find((k) => k.toLowerCase() === TRACEPARENT_HEADER)
139
+
140
+ if (!traceparentKey) {
141
+ return null
142
+ }
143
+
144
+ const traceparentValue = headers[traceparentKey]
145
+ const traceparent = Array.isArray(traceparentValue) ? traceparentValue[0] : traceparentValue
146
+
147
+ if (!traceparent) {
148
+ return null
149
+ }
150
+
151
+ const context = parseTraceParent(traceparent)
152
+ if (!context) {
153
+ return null
154
+ }
155
+
156
+ // Get optional tracestate header
157
+ const tracestateKey = Object.keys(headers).find((k) => k.toLowerCase() === TRACESTATE_HEADER)
158
+
159
+ if (tracestateKey) {
160
+ const tracestateValue = headers[tracestateKey]
161
+ const tracestate = Array.isArray(tracestateValue) ? tracestateValue[0] : tracestateValue
162
+
163
+ if (tracestate) {
164
+ return { ...context, traceState: tracestate }
165
+ }
166
+ }
167
+
168
+ return context
169
+ })
170
+
171
+ /**
172
+ * Format a trace context as a traceparent header value
173
+ */
174
+ export const formatTraceParent = (context: TraceContext): string => {
175
+ const flags = context.traceFlags.toString(16).padStart(2, "0")
176
+ return `${context.version}-${context.traceId}-${context.parentSpanId}-${flags}`
177
+ }
package/src/index.ts ADDED
@@ -0,0 +1,139 @@
1
+ /**
2
+ * @effect-gql/opentelemetry
3
+ *
4
+ * OpenTelemetry tracing integration for Effect GraphQL.
5
+ *
6
+ * Provides distributed tracing using Effect's native OpenTelemetry support.
7
+ * Works with any OpenTelemetry-compatible backend (Jaeger, Tempo, Honeycomb, etc.).
8
+ *
9
+ * @example
10
+ * ```typescript
11
+ * import { GraphQLSchemaBuilder } from "@effect-gql/core"
12
+ * import { serve } from "@effect-gql/node"
13
+ * import { withTracing } from "@effect-gql/opentelemetry"
14
+ * import { NodeSdk } from "@effect/opentelemetry"
15
+ * import { BatchSpanProcessor } from "@opentelemetry/sdk-trace-base"
16
+ * import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-http"
17
+ *
18
+ * // Add tracing to schema
19
+ * const builder = GraphQLSchemaBuilder.empty
20
+ * .query("users", { ... })
21
+ * .pipe(withTracing({
22
+ * extension: { exposeTraceIdInResponse: true },
23
+ * resolver: { excludePatterns: [/^Query\.__/] }
24
+ * }))
25
+ *
26
+ * // Configure OpenTelemetry
27
+ * const TracingLayer = NodeSdk.layer(() => ({
28
+ * resource: { serviceName: "my-graphql-api" },
29
+ * spanProcessor: new BatchSpanProcessor(
30
+ * new OTLPTraceExporter({ url: "http://localhost:4318/v1/traces" })
31
+ * )
32
+ * }))
33
+ *
34
+ * // Serve with tracing
35
+ * serve(builder, TracingLayer.pipe(Layer.merge(serviceLayer)))
36
+ * ```
37
+ *
38
+ * @packageDocumentation
39
+ */
40
+
41
+ import type { GraphQLSchemaBuilder } from "@effect-gql/core"
42
+ import { tracingExtension, type TracingExtensionConfig } from "./tracing-extension"
43
+ import { resolverTracingMiddleware, type ResolverTracingConfig } from "./tracing-middleware"
44
+
45
+ /**
46
+ * Complete configuration for GraphQL tracing
47
+ */
48
+ export interface GraphQLTracingConfig {
49
+ /**
50
+ * Configuration for phase-level tracing (parse, validate, execute).
51
+ * Uses the Extensions system.
52
+ */
53
+ readonly extension?: TracingExtensionConfig
54
+
55
+ /**
56
+ * Configuration for resolver-level tracing.
57
+ * Uses the Middleware system.
58
+ */
59
+ readonly resolver?: ResolverTracingConfig
60
+ }
61
+
62
+ /**
63
+ * Add OpenTelemetry tracing to a GraphQL schema builder.
64
+ *
65
+ * This is a convenience function that registers both the tracing extension
66
+ * (for phase-level spans) and resolver middleware (for field-level spans).
67
+ *
68
+ * **Span Hierarchy:**
69
+ * ```
70
+ * graphql.request (if using traced router)
71
+ * ├── graphql.parse
72
+ * ├── graphql.validate
73
+ * └── graphql.execute
74
+ * ├── graphql.resolve Query.users
75
+ * ├── graphql.resolve User.posts
76
+ * └── graphql.resolve Post.author
77
+ * ```
78
+ *
79
+ * **Requirements:**
80
+ * - An OpenTelemetry tracer must be provided via Effect's tracing layer
81
+ * - Use `@effect/opentelemetry` NodeSdk.layer or OtlpTracer.layer
82
+ *
83
+ * @example
84
+ * ```typescript
85
+ * import { GraphQLSchemaBuilder } from "@effect-gql/core"
86
+ * import { withTracing } from "@effect-gql/opentelemetry"
87
+ *
88
+ * const builder = GraphQLSchemaBuilder.empty
89
+ * .query("hello", {
90
+ * type: S.String,
91
+ * resolve: () => Effect.succeed("world")
92
+ * })
93
+ * .pipe(withTracing({
94
+ * extension: {
95
+ * exposeTraceIdInResponse: true, // Add traceId to response extensions
96
+ * includeQuery: false, // Don't include query in spans (security)
97
+ * },
98
+ * resolver: {
99
+ * minDepth: 0, // Trace all resolvers
100
+ * excludePatterns: [/^Query\.__/], // Skip introspection
101
+ * includeArgs: false, // Don't include args (security)
102
+ * }
103
+ * }))
104
+ * ```
105
+ *
106
+ * @param config - Optional tracing configuration
107
+ * @returns A function that adds tracing to a GraphQLSchemaBuilder
108
+ */
109
+ export const withTracing =
110
+ <R>(config?: GraphQLTracingConfig) =>
111
+ (builder: GraphQLSchemaBuilder<R>): GraphQLSchemaBuilder<R> => {
112
+ // Add tracing extension for phase-level spans
113
+ let result = builder.extension(tracingExtension(config?.extension))
114
+
115
+ // Add resolver tracing middleware for field-level spans
116
+ result = result.middleware(resolverTracingMiddleware(config?.resolver))
117
+
118
+ return result as GraphQLSchemaBuilder<R>
119
+ }
120
+
121
+ // Re-export components for individual use
122
+ export { tracingExtension, type TracingExtensionConfig } from "./tracing-extension"
123
+ export { resolverTracingMiddleware, type ResolverTracingConfig } from "./tracing-middleware"
124
+ export {
125
+ extractTraceContext,
126
+ parseTraceParent,
127
+ formatTraceParent,
128
+ isSampled,
129
+ TraceContextTag,
130
+ TRACEPARENT_HEADER,
131
+ TRACESTATE_HEADER,
132
+ type TraceContext,
133
+ } from "./context-propagation"
134
+ export { pathToString, getFieldDepth, isIntrospectionField } from "./utils"
135
+ export {
136
+ makeTracedGraphQLRouter,
137
+ withTracedRouter,
138
+ type TracedRouterOptions,
139
+ } from "./traced-router"
@@ -0,0 +1,240 @@
1
+ import { HttpRouter, HttpServerRequest, HttpServerResponse } from "@effect/platform"
2
+ import { Effect, Layer, Tracer } from "effect"
3
+ import type { GraphQLSchema } from "graphql"
4
+ import { makeGraphQLRouter, type MakeGraphQLRouterOptions } from "@effect-gql/core/server"
5
+ import { extractTraceContext, type TraceContext } from "./context-propagation"
6
+
7
+ /**
8
+ * Options for the traced GraphQL router
9
+ */
10
+ export interface TracedRouterOptions extends MakeGraphQLRouterOptions {
11
+ /**
12
+ * Name for the root HTTP span.
13
+ * Default: "graphql.http"
14
+ */
15
+ readonly rootSpanName?: string
16
+
17
+ /**
18
+ * Additional attributes to add to the root span.
19
+ */
20
+ readonly rootSpanAttributes?: Record<string, string | number | boolean>
21
+
22
+ /**
23
+ * Whether to propagate trace context from incoming HTTP headers.
24
+ * Uses W3C Trace Context (traceparent header).
25
+ * Default: true
26
+ */
27
+ readonly propagateContext?: boolean
28
+ }
29
+
30
+ /**
31
+ * Create an Effect span options object from trace context
32
+ */
33
+ const createSpanOptions = (
34
+ traceContext: TraceContext | null,
35
+ request: HttpServerRequest.HttpServerRequest,
36
+ config: TracedRouterOptions
37
+ ): {
38
+ attributes?: Record<string, unknown>
39
+ parent?: Tracer.ExternalSpan
40
+ } => {
41
+ const attributes: Record<string, unknown> = {
42
+ "http.method": request.method,
43
+ "http.url": request.url,
44
+ "http.target": request.url,
45
+ ...config.rootSpanAttributes,
46
+ }
47
+
48
+ if (traceContext && config.propagateContext !== false) {
49
+ return {
50
+ attributes,
51
+ parent: Tracer.externalSpan({
52
+ traceId: traceContext.traceId,
53
+ spanId: traceContext.parentSpanId,
54
+ sampled: (traceContext.traceFlags & 0x01) === 0x01,
55
+ }),
56
+ }
57
+ }
58
+
59
+ return { attributes }
60
+ }
61
+
62
+ /**
63
+ * Creates a GraphQL router with OpenTelemetry tracing at the HTTP level.
64
+ *
65
+ * This wraps the standard makeGraphQLRouter to:
66
+ * 1. Extract trace context from incoming HTTP headers (W3C Trace Context)
67
+ * 2. Create a root span for the entire HTTP request
68
+ * 3. Propagate trace context to child spans created by extensions/middleware
69
+ *
70
+ * **Span Hierarchy:**
71
+ * ```
72
+ * graphql.http (created by this router)
73
+ * ├── graphql.parse (from tracing extension)
74
+ * ├── graphql.validate (from tracing extension)
75
+ * └── graphql.resolve Query.* (from tracing middleware)
76
+ * ```
77
+ *
78
+ * @example
79
+ * ```typescript
80
+ * import { makeTracedGraphQLRouter } from "@effect-gql/opentelemetry"
81
+ * import { NodeSdk } from "@effect/opentelemetry"
82
+ *
83
+ * const router = makeTracedGraphQLRouter(schema, serviceLayer, {
84
+ * path: "/graphql",
85
+ * graphiql: { path: "/graphiql" },
86
+ * rootSpanName: "graphql.http",
87
+ * rootSpanAttributes: {
88
+ * "service.name": "my-api"
89
+ * }
90
+ * })
91
+ *
92
+ * // Provide OpenTelemetry layer when serving
93
+ * const TracingLayer = NodeSdk.layer(() => ({
94
+ * resource: { serviceName: "my-graphql-api" },
95
+ * spanProcessor: new BatchSpanProcessor(new OTLPTraceExporter())
96
+ * }))
97
+ * ```
98
+ *
99
+ * @param schema - The GraphQL schema
100
+ * @param layer - Effect layer providing services required by resolvers
101
+ * @param options - Router and tracing configuration
102
+ * @returns An HttpRouter with tracing enabled
103
+ */
104
+ export const makeTracedGraphQLRouter = <R>(
105
+ schema: GraphQLSchema,
106
+ layer: Layer.Layer<R>,
107
+ options: TracedRouterOptions = {}
108
+ ): HttpRouter.HttpRouter<never, never> => {
109
+ const rootSpanName = options.rootSpanName ?? "graphql.http"
110
+
111
+ // Create the base router (handles GraphQL logic)
112
+ const baseRouter = makeGraphQLRouter(schema, layer, options)
113
+
114
+ // Convert base router to an HttpApp for Effect-based handling
115
+ const baseApp = HttpRouter.toHttpApp(baseRouter)
116
+
117
+ // Wrap with tracing
118
+ return HttpRouter.empty.pipe(
119
+ HttpRouter.all(
120
+ "*",
121
+ Effect.gen(function* () {
122
+ const request = yield* HttpServerRequest.HttpServerRequest
123
+
124
+ // Extract trace context from headers (if enabled)
125
+ const traceContext =
126
+ options.propagateContext !== false
127
+ ? yield* extractTraceContext.pipe(Effect.catchAll(() => Effect.succeed(null)))
128
+ : null
129
+
130
+ // Create span options with parent context if available
131
+ const spanOptions = createSpanOptions(traceContext, request, options)
132
+
133
+ // Execute the request inside a root span
134
+ return yield* Effect.withSpan(
135
+ rootSpanName,
136
+ spanOptions
137
+ )(
138
+ Effect.gen(function* () {
139
+ // Delegate to the base app (which handles the request from context)
140
+ const app = yield* baseApp
141
+ const response = yield* app.pipe(
142
+ Effect.catchTag("RouteNotFound", () =>
143
+ HttpServerResponse.text(JSON.stringify({ errors: [{ message: "Not Found" }] }), {
144
+ status: 404,
145
+ headers: { "content-type": "application/json" },
146
+ })
147
+ )
148
+ )
149
+
150
+ // Annotate span with response info
151
+ yield* Effect.annotateCurrentSpan("http.status_code", response.status)
152
+
153
+ return response
154
+ })
155
+ )
156
+ })
157
+ )
158
+ )
159
+ }
160
+
161
+ /**
162
+ * Wrap an existing HttpRouter with OpenTelemetry tracing.
163
+ *
164
+ * This is useful when you already have a router and want to add
165
+ * request-level tracing without recreating it.
166
+ *
167
+ * @example
168
+ * ```typescript
169
+ * import { toRouter } from "@effect-gql/core/server"
170
+ * import { withTracedRouter } from "@effect-gql/opentelemetry"
171
+ *
172
+ * const baseRouter = toRouter(builder, serviceLayer)
173
+ * const tracedRouter = withTracedRouter(baseRouter, {
174
+ * rootSpanName: "graphql.http"
175
+ * })
176
+ * ```
177
+ */
178
+ export const withTracedRouter = (
179
+ router: HttpRouter.HttpRouter<any, any>,
180
+ options: {
181
+ rootSpanName?: string
182
+ rootSpanAttributes?: Record<string, string | number | boolean>
183
+ propagateContext?: boolean
184
+ } = {}
185
+ ): HttpRouter.HttpRouter<any, any> => {
186
+ const rootSpanName = options.rootSpanName ?? "graphql.http"
187
+
188
+ // Convert router to an HttpApp for Effect-based handling
189
+ const baseApp = HttpRouter.toHttpApp(router)
190
+
191
+ return HttpRouter.empty.pipe(
192
+ HttpRouter.all(
193
+ "*",
194
+ Effect.gen(function* () {
195
+ const request = yield* HttpServerRequest.HttpServerRequest
196
+
197
+ // Extract trace context from headers
198
+ const traceContext =
199
+ options.propagateContext !== false
200
+ ? yield* extractTraceContext.pipe(Effect.catchAll(() => Effect.succeed(null)))
201
+ : null
202
+
203
+ const spanOptions: {
204
+ attributes: Record<string, unknown>
205
+ parent?: Tracer.ExternalSpan
206
+ } = {
207
+ attributes: {
208
+ "http.method": request.method,
209
+ "http.url": request.url,
210
+ ...options.rootSpanAttributes,
211
+ },
212
+ }
213
+
214
+ if (traceContext && options.propagateContext !== false) {
215
+ spanOptions.parent = Tracer.externalSpan({
216
+ traceId: traceContext.traceId,
217
+ spanId: traceContext.parentSpanId,
218
+ sampled: (traceContext.traceFlags & 0x01) === 0x01,
219
+ })
220
+ }
221
+
222
+ return yield* Effect.withSpan(
223
+ rootSpanName,
224
+ spanOptions
225
+ )(
226
+ Effect.gen(function* () {
227
+ // Delegate to the base app (which handles the request from context)
228
+ const app = yield* baseApp
229
+ const response = yield* app
230
+
231
+ // Annotate span with response info
232
+ yield* Effect.annotateCurrentSpan("http.status_code", response.status)
233
+
234
+ return response
235
+ })
236
+ )
237
+ })
238
+ )
239
+ )
240
+ }