Skip to main content

otel_graphql 0.3.1 copy "otel_graphql: ^0.3.1" to clipboard
otel_graphql: ^0.3.1 copied to clipboard

OpenTelemetry instrumentation for gql by Dartastic.io. Adds standard OpenTelemetry observability to applications that use gql.

example/main.dart

// Licensed under the Apache License, Version 2.0
// Copyright 2025, Mindful Software LLC, All rights reserved.

// Minimal runnable example: put [OTelGraphqlLink] FIRST in the link
// chain so its span encloses everything downstream (auth, retry, the
// HTTP/WebSocket transport).
//
// In a real app the terminating link is your transport — e.g.
// `HttpLink('https://api.example.com/graphql')` from
// `package:graphql` / `package:gql_http_link`. Here a tiny in-memory
// link stands in so the example runs without a server.

import 'package:dartastic_opentelemetry/dartastic_opentelemetry.dart';
import 'package:gql/language.dart' show parseString;
import 'package:gql_exec/gql_exec.dart';
import 'package:gql_link/gql_link.dart';
import 'package:otel_graphql/otel_graphql.dart';

Future<void> main() async {
  // 1. Bring up OTel first so trace context is flowing before the
  //    first operation executes.
  await OTel.initialize(serviceName: 'graphql-example');

  // 2. Compose the chain: OTelGraphqlLink first, transport last.
  final link = Link.from([
    OTelGraphqlLink(),
    _EchoLink(),
  ]);

  // 3. Execute an operation as usual. One span per operation:
  //    name `query GetUser`, attributes graphql.operation.type /
  //    graphql.operation.name. Variables are never captured.
  final request = Request(
    operation: Operation(
      document: parseString('query GetUser { user { id name } }'),
      operationName: 'GetUser',
    ),
  );

  await for (final response in link.request(request)) {
    print('data: ${response.data}');
  }

  await OTel.shutdown();
}

/// Stand-in terminating link so the example runs offline.
class _EchoLink extends Link {
  @override
  Stream<Response> request(Request request, [NextLink? forward]) async* {
    yield const Response(
      data: <String, dynamic>{
        'user': <String, dynamic>{'id': '1', 'name': 'Ada'},
      },
      response: <String, dynamic>{},
    );
  }
}
0
likes
160
points
114
downloads

Documentation

API reference

Publisher

verified publisherdartastic.io

Weekly Downloads

OpenTelemetry instrumentation for gql by Dartastic.io. Adds standard OpenTelemetry observability to applications that use gql.

Homepage
Repository (GitHub)
View/report issues

License

Apache-2.0 (license)

Dependencies

dartastic_opentelemetry_api, gql, gql_exec, gql_link

More

Packages that depend on otel_graphql