Skip to content

Conversation

EmrysMyrddin
Copy link
Collaborator

@EmrysMyrddin EmrysMyrddin commented Jul 25, 2025

Adds getters to the graphql context and the plugin instance to ease the access to the root spans (http, graphql operation and subgraph executions).

The goal is to allow users to easily modify (like adding custom attributes) to those spans.

There is 3 ways to access those utility functions:

  • via the graphql context (so that it is accessible from the resolvers too).
  • via the Gateway Configuration Context, so that Hive plugins can easily have access to it
  • via the plugin instance itself, so that other Yoga plugins can easily have access to it

Usage Examples

From Graphql Context

import './telemetry.ts'
import { defineConfig } from '@graphql-hive/gateway'
import { trace } from '@opentelemetry/api'

export const gatewayConfig = defineConfig({
  openTelemetry: { traces: true },

  genericAuth: {
    mode: 'protect-granular',
    async resolveUserFn(ctx) {
      const token = ctx.request.headers.get('authorization')
      const user = await fetch(`https://auth/validate?token=${token}`).then(r => r.json)

      // When using from the graphql context, the function can automatically return 
      // the current request context without explicitly providing the request
      const httpSpan = trace.getSpan(ctx.opentelemetry.getHttpContext())
      httpSpan.setAttribute('auth.user.id', user?.id ?? '<unauthenticated>')

      return user
    }
  }
})

From Hive Gateway Configuration Context

import './telemetry.ts'
import { defineConfig } from '@graphql-hive/gateway'
import { trace } from '@opentelemetry/api'

export const gatewayConfig = defineConfig({
  openTelemetry: { traces: true },
  plugins: (ctx) => {
    return [{
      onRequestParse({ request }) {
        const httpSpan = trace.getSpan(ctx.openTelemetry.getHttpContext({ request }))
        httpSpan?.setAttribute('session_id', request.headers.get('session-id'))
      },
    }]
  })
})

From Yoga plugin

import './telemetry.ts'
import { createYoga } from 'graphql-yoga'
import { createServer } from 'node:http'
import { useOpenTelemetry } from '@graphql-mesh/plugin-opentelemetry'

const openTelemetry = useOpenTelemetry({ traces: true });

const yoga = createYoga({
  plugins: [
    openTelemetry,
    {
      onRequestParse({ request }) {
        // All utils functions are also available directly on the plugin instance
        const httpSpan = trace.getSpan(openTelemetry.getHttpContext({ request }))
        httpSpan?.setAttribute('session_id', request.headers.get('session-id'))
      },
    }
  ],
})

const server = createServer(yoga)
server.start(4000)

@theguild-bot
Copy link
Collaborator

theguild-bot commented Jul 25, 2025

🚀 Snapshot Release (alpha)

The latest changes of this PR are available as alpha on npm (based on the declared changesets):

Package Version Info
@graphql-tools/batch-delegate 10.0.0-alpha-572407000830b7aef9d4d7def6fa8139009a9e13 npm ↗︎ unpkg ↗︎
@graphql-tools/batch-execute 10.0.0-alpha-572407000830b7aef9d4d7def6fa8139009a9e13 npm ↗︎ unpkg ↗︎
@graphql-tools/delegate 11.0.0-alpha-572407000830b7aef9d4d7def6fa8139009a9e13 npm ↗︎ unpkg ↗︎
@graphql-tools/executor-common 1.0.0-alpha-572407000830b7aef9d4d7def6fa8139009a9e13 npm ↗︎ unpkg ↗︎
@graphql-tools/executor-graphql-ws 3.0.0-alpha-572407000830b7aef9d4d7def6fa8139009a9e13 npm ↗︎ unpkg ↗︎
@graphql-tools/executor-http 3.0.0-alpha-572407000830b7aef9d4d7def6fa8139009a9e13 npm ↗︎ unpkg ↗︎
@graphql-tools/federation 4.0.0-alpha-572407000830b7aef9d4d7def6fa8139009a9e13 npm ↗︎ unpkg ↗︎
@graphql-mesh/fusion-runtime 1.0.0-alpha-572407000830b7aef9d4d7def6fa8139009a9e13 npm ↗︎ unpkg ↗︎
@graphql-hive/gateway 2.0.0-alpha-572407000830b7aef9d4d7def6fa8139009a9e13 npm ↗︎ unpkg ↗︎
@graphql-hive/importer 2.0.0-alpha-572407000830b7aef9d4d7def6fa8139009a9e13 npm ↗︎ unpkg ↗︎
@graphql-hive/logger 1.0.1-alpha-572407000830b7aef9d4d7def6fa8139009a9e13 npm ↗︎ unpkg ↗︎
@graphql-hive/nestjs 2.0.0-alpha-572407000830b7aef9d4d7def6fa8139009a9e13 npm ↗︎ unpkg ↗︎
@graphql-hive/plugin-aws-sigv4 2.0.0-alpha-572407000830b7aef9d4d7def6fa8139009a9e13 npm ↗︎ unpkg ↗︎
@graphql-hive/plugin-deduplicate-request 2.0.0-alpha-572407000830b7aef9d4d7def6fa8139009a9e13 npm ↗︎ unpkg ↗︎
@graphql-mesh/hmac-upstream-signature 2.0.0-alpha-572407000830b7aef9d4d7def6fa8139009a9e13 npm ↗︎ unpkg ↗︎
@graphql-mesh/plugin-jwt-auth 2.0.0-alpha-572407000830b7aef9d4d7def6fa8139009a9e13 npm ↗︎ unpkg ↗︎
@graphql-mesh/plugin-opentelemetry 2.0.0-alpha-572407000830b7aef9d4d7def6fa8139009a9e13 npm ↗︎ unpkg ↗︎
@graphql-mesh/plugin-prometheus 2.0.0-alpha-572407000830b7aef9d4d7def6fa8139009a9e13 npm ↗︎ unpkg ↗︎
@graphql-hive/pubsub 2.0.0-alpha-572407000830b7aef9d4d7def6fa8139009a9e13 npm ↗︎ unpkg ↗︎
@graphql-hive/gateway-runtime 2.0.0-alpha-572407000830b7aef9d4d7def6fa8139009a9e13 npm ↗︎ unpkg ↗︎
@graphql-hive/signal 2.0.0-alpha-572407000830b7aef9d4d7def6fa8139009a9e13 npm ↗︎ unpkg ↗︎
@graphql-tools/stitch 10.0.0-alpha-572407000830b7aef9d4d7def6fa8139009a9e13 npm ↗︎ unpkg ↗︎
@graphql-tools/stitching-directives 4.0.0-alpha-572407000830b7aef9d4d7def6fa8139009a9e13 npm ↗︎ unpkg ↗︎
@graphql-mesh/transport-common 1.0.0-alpha-572407000830b7aef9d4d7def6fa8139009a9e13 npm ↗︎ unpkg ↗︎
@graphql-mesh/transport-http 1.0.0-alpha-572407000830b7aef9d4d7def6fa8139009a9e13 npm ↗︎ unpkg ↗︎
@graphql-mesh/transport-http-callback 1.0.0-alpha-572407000830b7aef9d4d7def6fa8139009a9e13 npm ↗︎ unpkg ↗︎
@graphql-mesh/transport-ws 2.0.0-alpha-572407000830b7aef9d4d7def6fa8139009a9e13 npm ↗︎ unpkg ↗︎
@graphql-tools/wrap 11.0.0-alpha-572407000830b7aef9d4d7def6fa8139009a9e13 npm ↗︎ unpkg ↗︎

@EmrysMyrddin EmrysMyrddin force-pushed the feat/otel/root-context-getters branch 2 times, most recently from cfc7f06 to 2015eee Compare July 25, 2025 21:16
@theguild-bot
Copy link
Collaborator

theguild-bot commented Jul 25, 2025

🚀 Snapshot Release (Binary for macOS-ARM64)

The latest changes of this PR are available for download (based on the declared changesets).

Download

@theguild-bot
Copy link
Collaborator

theguild-bot commented Jul 25, 2025

🚀 Snapshot Release (Binary for Linux-X64)

The latest changes of this PR are available for download (based on the declared changesets).

Download

@theguild-bot
Copy link
Collaborator

theguild-bot commented Jul 25, 2025

🚀 Snapshot Release (Binary for Linux-ARM64)

The latest changes of this PR are available for download (based on the declared changesets).

Download

@theguild-bot
Copy link
Collaborator

theguild-bot commented Jul 25, 2025

🚀 Snapshot Release (Binary for macOS-X64)

The latest changes of this PR are available for download (based on the declared changesets).

Download

@theguild-bot
Copy link
Collaborator

theguild-bot commented Jul 25, 2025

🚀 Snapshot Release (Binary for Windows-X64)

The latest changes of this PR are available for download (based on the declared changesets).

Download

@theguild-bot
Copy link
Collaborator

theguild-bot commented Jul 25, 2025

🚀 Snapshot Release (Bun Docker Image)

The latest changes of this PR are available as image on GitHub Container Registry (based on the declared changesets):

ghcr.io/graphql-hive/gateway:2.0.0-alpha-572407000830b7aef9d4d7def6fa8139009a9e13-bun

@theguild-bot
Copy link
Collaborator

theguild-bot commented Jul 25, 2025

🚀 Snapshot Release (Node Docker Image)

The latest changes of this PR are available as image on GitHub Container Registry (based on the declared changesets):

ghcr.io/graphql-hive/gateway:2.0.0-alpha-572407000830b7aef9d4d7def6fa8139009a9e13

@EmrysMyrddin EmrysMyrddin force-pushed the feat/otel/root-context-getters branch from d6054fe to 5724070 Compare August 19, 2025 12:52
@enisdenjo enisdenjo merged commit d44e0ca into v2 Aug 21, 2025
64 checks passed
@enisdenjo enisdenjo deleted the feat/otel/root-context-getters branch August 21, 2025 11:31
enisdenjo pushed a commit that referenced this pull request Aug 22, 2025
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
enisdenjo pushed a commit that referenced this pull request Aug 27, 2025
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
enisdenjo pushed a commit that referenced this pull request Aug 27, 2025
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
enisdenjo pushed a commit that referenced this pull request Aug 29, 2025
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
enisdenjo pushed a commit that referenced this pull request Sep 2, 2025
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants