Skip to content

Commit 74ffe4c

Browse files
authored
docs(opentelemtry): improve OpenTelemetry documentation (#6947)
1 parent b97cf77 commit 74ffe4c

File tree

1 file changed

+33
-15
lines changed

1 file changed

+33
-15
lines changed

packages/web/docs/src/content/gateway/monitoring-tracing.mdx

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,6 @@ npm i dd-trace
741741
```ts filename="telemetry.ts"
742742
import ddTrace from 'dd-trace'
743743
import { openTelemetrySetup } from '@graphql-hive/gateway/opentelemetry/setup'
744-
import { AsyncLocalStorageContextManager } from '@opentelemetry/context-async-hooks'
745744

746745
const { TracerProvider } = ddTrace.init({
747746
// Your configuration
@@ -1866,7 +1865,7 @@ export const gatewayConfig = defineConfig({
18661865
traces: true,
18671866
},
18681867
genericAuth: {
1869-
resolveUserFn: ({ context }) => {
1868+
resolveUserFn: (context) => {
18701869
// `startActiveSpan` will rely on the current context to parent the new span correctly
18711870
// You can also use your own tracer instead of Hive Gateway's one.
18721871
return hive.tracer!.startActiveSpan('users.fetch', (span) => {
@@ -1912,7 +1911,7 @@ export const gateway = createGatewayRuntime({
19121911
traces: true
19131912
}),
19141913
useGenericAuth({
1915-
resolveUserFn: ({ context }) => {
1914+
resolveUserFn: (context) => {
19161915
// `startActiveSpan` will rely on the current context to parent the new span correctly
19171916
// You can also use your own tracer instead of Hive Gateway's one.
19181917
return hive.tracer!.startActiveSpan('users.fetch', (span) => {
@@ -1953,7 +1952,6 @@ to get the proper context.
19531952
import { defineConfig } from '@graphql-hive/gateway'
19541953
import { openTelemetrySetup } from '@graphql-hive/gateway/opentelemetry/setup'
19551954
import { hive } from '@graphql-hive/gateway/opentelemetry/api'
1956-
import { AsyncLocalStorageContextManager } from '@opentelemetry/context-async-hooks'
19571955
import { useGenericAuth } from '@envelop/generic-auth'
19581956

19591957
openTelemetrySetup({
@@ -1968,7 +1966,7 @@ export const gatewayConfig = defineConfig({
19681966
},
19691967
plugins: () => [
19701968
useGenericAuth({
1971-
resolveUserFn: ({ context }) => {
1969+
resolveUserFn: (context) => {
19721970
const otelCtx = hive.getActiveContext({ context });
19731971

19741972
// Explicitly pass the parent context as the third argument.
@@ -1992,7 +1990,6 @@ import { createGatewayRuntime } from '@graphql-hive/gateway-runtime'
19921990
import { useOpenTelemetry } from '@graphql-hive/plugin-opentelemetry'
19931991
import { hive } from '@graphql-hive/plugin-opentelemetry/api'
19941992
import { openTelemetrySetup } from '@graphql-hive/plugin-opentelemetry/setup'
1995-
import { AsyncLocalStorageContextManager } from '@opentelemetry/context-async-hooks'
19961993
import { useGenericAuth } from '@envelop/generic-auth'
19971994

19981995
openTelemetrySetup({
@@ -2009,7 +2006,7 @@ export const gateway = createGatewayRuntime({
20092006
traces: true
20102007
}),
20112008
useGenericAuth({
2012-
resolveUserFn: ({ context }) => {
2009+
resolveUserFn: (context) => {
20132010
const otelCtx = hive.getActiveContext({ context });
20142011

20152012
// Explicitly pass the parent context as the third argument.
@@ -2077,7 +2074,7 @@ export const gatewayConfig = defineConfig({
20772074
},
20782075
plugins: () => [
20792076
useGenericAuth({
2080-
resolveUserFn: ({ context }) => {
2077+
resolveUserFn: (context) => {
20812078
const span = trace.getActiveSpan();
20822079
const user = await fetchUser(extractUserIdFromContext(context))
20832080
span.setAttribute('user.id', user.id);
@@ -2106,7 +2103,6 @@ import { defineConfig } from '@graphql-hive/gateway'
21062103
import { openTelemetrySetup } from '@graphql-hive/plugin-opentelemetry/setup'
21072104
// This package re-export official @opentelemetry/api package for ease of use
21082105
import { trace, hive } from '@graphql-hive/plugin-opentelemetry/api'
2109-
import { AsyncLocalStorageContextManager } from '@opentelemetry/context-async-hooks'
21102106

21112107
openTelemetrySetup({
21122108
contextManager: null, // Don't register any context manager
@@ -2120,7 +2116,7 @@ export const gatewayConfig = defineConfig({
21202116
},
21212117
plugins: () => [
21222118
useGenericAuth({
2123-
resolveUserFn: ({ context }) => {
2119+
resolveUserFn: (context) => {
21242120
const user = await fetchUser(extractUserIdFromContext(context))
21252121

21262122
const otelCtx = hive.getActiveContext({ context })
@@ -2151,11 +2147,32 @@ You can access those spans by using `getHttpContext(request)`, `getOperationCont
21512147
`getExecutionRequestContext(executionRequest)` functions from
21522148
`@graphql-hive/gateway/opentelemetry/api`.
21532149

2154-
They are accessible under `openTelemetry` key of the graphql and configuration context, and on the
2155-
plugin instance if you don't have access to any of those contexts.
2150+
They are also accessible under `openTelemetry` key of the graphql and configuration context, and on
2151+
the plugin. When using the graphql context, the argument is optional and functions will return the
2152+
current appropriate root context.
21562153

2157-
When using the graphql context, the argument is optional and functions will return the current
2158-
appropriate root context.
2154+
```ts filename="gateway.config.ts"
2155+
import { hive, trace } '@graphql-hive/gateway/opentelemetry/api'
2156+
import { defineConfig } from '@graphql-hive/gateway'
2157+
2158+
export config gatewayConfig = defineConfig({
2159+
openTelemetry: {
2160+
traces: true
2161+
},
2162+
2163+
plugin: () => [{
2164+
onRequest({ request }) {
2165+
const httpSpan = trace.getSpan(hive.getHttpContext(request))
2166+
},
2167+
onExecute({ context }) {
2168+
const operationSpan = trace.getSpan(hive.getOperationContext(context))
2169+
},
2170+
onSubgraphExecute({ executionRequest }) {
2171+
const executionRequestSpan = trace.getSpan(hive.getExecutionRequestContext(executionRequest))
2172+
},
2173+
}]
2174+
})
2175+
```
21592176

21602177
### Troubleshooting
21612178

@@ -2176,7 +2193,7 @@ import { openTelemetrySetup } from '@graphql-hive/plugin-opentelemetry/setup'
21762193
import { AsyncLocalStorageContextManager } from '@opentelemetry/context-async-hooks'
21772194

21782195
openTelemetrySetup({
2179-
contextManager: new AsyncLocalStorageContextManager
2196+
contextManager: new AsyncLocalStorageContextManager(),
21802197
traces: {
21812198
console: true
21822199
}
@@ -2657,6 +2674,7 @@ ignore introspection queries for all metrics prefixed by `graphql_envelop_` by s
26572674
</Tabs.Tab>
26582675

26592676
<Tabs.Tab>
2677+
26602678
By default, all labels are enabled, but each one can be disabled to reduce cardinality:
26612679

26622680
```ts filename="gateway.config.ts"

0 commit comments

Comments
 (0)