| 
2 | 2 | import { defineConfig } from '@graphql-hive/gateway';  | 
3 | 3 | // @ts-expect-error not a dependency  | 
4 | 4 | import { hiveTracingSetup } from '@graphql-hive/plugin-opentelemetry/setup';  | 
 | 5 | +import type { Context } from '@opentelemetry/api';  | 
5 | 6 | import { AsyncLocalStorageContextManager } from '@opentelemetry/context-async-hooks';  | 
 | 7 | +import { globalErrorHandler } from '@opentelemetry/core';  | 
6 | 8 | import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http';  | 
7 |  | -import { BatchSpanProcessor } from '@opentelemetry/sdk-trace-base';  | 
8 |  | -import { MultiSpanProcessor } from '@opentelemetry/sdk-trace-base/build/src/MultiSpanProcessor';  | 
 | 9 | +import {  | 
 | 10 | +  BatchSpanProcessor,  | 
 | 11 | +  SpanProcessor,  | 
 | 12 | +  type ReadableSpan,  | 
 | 13 | +  type Span,  | 
 | 14 | +} from '@opentelemetry/sdk-trace-base';  | 
9 | 15 | 
 
  | 
10 |  | -if (process.env['OPENTELEMETRY_COLLECTOR_ENDPOINT'] || process.env['HIVE_TRACING_ACCESS_TOKEN']) {  | 
 | 16 | +/** Note: this is inlined for now... */  | 
 | 17 | +class MultiSpanProcessor implements SpanProcessor {  | 
 | 18 | +  constructor(private readonly _spanProcessors: SpanProcessor[]) {}  | 
 | 19 | + | 
 | 20 | +  forceFlush(): Promise<void> {  | 
 | 21 | +    const promises: Promise<void>[] = [];  | 
 | 22 | + | 
 | 23 | +    for (const spanProcessor of this._spanProcessors) {  | 
 | 24 | +      promises.push(spanProcessor.forceFlush());  | 
 | 25 | +    }  | 
 | 26 | +    return new Promise(resolve => {  | 
 | 27 | +      Promise.all(promises)  | 
 | 28 | +        .then(() => {  | 
 | 29 | +          resolve();  | 
 | 30 | +        })  | 
 | 31 | +        .catch(error => {  | 
 | 32 | +          globalErrorHandler(error || new Error('MultiSpanProcessor: forceFlush failed'));  | 
 | 33 | +          resolve();  | 
 | 34 | +        });  | 
 | 35 | +    });  | 
 | 36 | +  }  | 
 | 37 | + | 
 | 38 | +  onStart(span: Span, context: Context): void {  | 
 | 39 | +    for (const spanProcessor of this._spanProcessors) {  | 
 | 40 | +      spanProcessor.onStart(span, context);  | 
 | 41 | +    }  | 
 | 42 | +  }  | 
 | 43 | + | 
 | 44 | +  onEnd(span: ReadableSpan): void {  | 
 | 45 | +    for (const spanProcessor of this._spanProcessors) {  | 
 | 46 | +      spanProcessor.onEnd(span);  | 
 | 47 | +    }  | 
 | 48 | +  }  | 
 | 49 | + | 
 | 50 | +  shutdown(): Promise<void> {  | 
 | 51 | +    const promises: Promise<void>[] = [];  | 
 | 52 | + | 
 | 53 | +    for (const spanProcessor of this._spanProcessors) {  | 
 | 54 | +      promises.push(spanProcessor.shutdown());  | 
 | 55 | +    }  | 
 | 56 | +    return new Promise((resolve, reject) => {  | 
 | 57 | +      Promise.all(promises).then(() => {  | 
 | 58 | +        resolve();  | 
 | 59 | +      }, reject);  | 
 | 60 | +    });  | 
 | 61 | +  }  | 
 | 62 | +}  | 
 | 63 | + | 
 | 64 | +if (process.env['OPENTELEMETRY_COLLECTOR_ENDPOINT'] || process.env['HIVE_TRACE_ACCESS_TOKEN']) {  | 
11 | 65 |   hiveTracingSetup({  | 
 | 66 | +    // Noop is only there to not raise an exception in case we do not hive console tracing.  | 
 | 67 | +    target: process.env['HIVE_TARGET'] ?? 'noop',  | 
12 | 68 |     contextManager: new AsyncLocalStorageContextManager(),  | 
13 | 69 |     processor: new MultiSpanProcessor([  | 
14 |  | -      ...(process.env['HIVE_TRACING_ACCESS_TOKEN']  | 
 | 70 | +      ...(process.env['HIVE_TRACE_ACCESS_TOKEN'] &&  | 
 | 71 | +      process.env['HIVE_TRACING_ENDPOINT'] &&  | 
 | 72 | +      process.env['HIVE_TARGET']  | 
15 | 73 |         ? [  | 
16 | 74 |             new BatchSpanProcessor(  | 
17 | 75 |               new OTLPTraceExporter({  | 
18 | 76 |                 url: process.env['HIVE_TRACING_ENDPOINT'],  | 
19 | 77 |                 headers: {  | 
20 |  | -                  Authorization: `Bearer ${process.env['HIVE_TRACING_ACCESS_TOKEN']}`,  | 
21 |  | -                  'X-Hive-Target-Ref': process.env.HIVE_TRACING_TARGET!,  | 
 | 78 | +                  Authorization: `Bearer ${process.env['HIVE_TRACE_ACCESS_TOKEN']}`,  | 
 | 79 | +                  'X-Hive-Target-Ref': process.env['HIVE_TARGET'],  | 
22 | 80 |                 },  | 
23 | 81 |               }),  | 
24 | 82 |             ),  | 
 | 
0 commit comments