Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@graphql-hive/plugin-opentelemetry': patch
---

dependencies updates:

- Added dependency [`@graphql-tools/executor@^1.4.9` ↗︎](https://www.npmjs.com/package/@graphql-tools/executor/v/1.4.9) (to `dependencies`)
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@
"@graphql-mesh/types": "0.104.13",
"@graphql-mesh/utils": "0.104.13",
"@graphql-tools/delegate": "workspace:^",
"@graphql-tools/executor": "1.5.0-alpha-20251028202445-48fa8e9d55723fbb44d9d07fdcd9585d7c50607b",
"@graphql-tools/utils": "10.10.0-alpha-20251028202445-48fa8e9d55723fbb44d9d07fdcd9585d7c50607b",
"@opentelemetry/otlp-exporter-base@npm:0.203.0": "patch:@opentelemetry/otlp-exporter-base@npm%3A0.203.0#~/.yarn/patches/@opentelemetry-otlp-exporter-base-npm-0.203.0-183dcac0e6.patch",
"@rollup/plugin-node-resolve@npm:^15.2.3": "patch:@rollup/plugin-node-resolve@npm%3A16.0.1#~/.yarn/patches/@rollup-plugin-node-resolve-npm-16.0.1-2936474bab.patch",
"@vitest/snapshot": "patch:@vitest/snapshot@npm:3.1.2#~/.yarn/patches/@vitest-snapshot-npm-3.1.1-4d18cf86dc.patch",
Expand Down
10 changes: 10 additions & 0 deletions packages/batch-execute/src/mergeRequests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
VariableNode,
visit,
} from 'graphql';
import { abortSignalAll } from '../../signal/src/abortSignalAll.js';
import { createPrefix } from './prefix.js';

/**
Expand Down Expand Up @@ -70,12 +71,19 @@ export function mergeRequests(
const mergedSelections: Array<SelectionNode> = [];
const mergedFragmentDefinitions: Array<FragmentDefinitionNode> = [];
let mergedExtensions: Record<string, any> = Object.create(null);
let schemaCoordinateInErrors: boolean | undefined = false;
const signals: AbortSignal[] = [];

for (let index = 0; index < requests.length; index++) {
const request = requests[index];
if (request) {
schemaCoordinateInErrors ||= request.schemaCoordinateInErrors;
const prefixedRequests = prefixRequest(createPrefix(index), request);

if (request.signal) {
signals.push(request.signal);
}

for (const def of prefixedRequests.document.definitions) {
if (isOperationDefinition(def)) {
mergedSelections.push(...def.selectionSet.selections);
Expand Down Expand Up @@ -129,6 +137,8 @@ export function mergeRequests(
info: firstRequest.info,
operationType,
rootValue: firstRequest.rootValue,
signal: abortSignalAll(signals),
schemaCoordinateInErrors,
};
}

Expand Down
1 change: 1 addition & 0 deletions packages/plugins/opentelemetry/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"@graphql-mesh/transport-common": "workspace:^",
"@graphql-mesh/types": "^0.104.13",
"@graphql-mesh/utils": "^0.104.13",
"@graphql-tools/executor": "^1.4.9",
"@graphql-tools/utils": "^10.9.1",
"@opentelemetry/api": "^1.9.0",
"@opentelemetry/api-logs": "^0.207.0",
Expand Down
9 changes: 9 additions & 0 deletions packages/plugins/opentelemetry/src/attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ export const SEMATTRS_HIVE_GRAPHQL_OPERATION_HASH =
'hive.graphql.operation.hash';
export const SEMATTRS_HIVE_GRAPHQL_ERROR_COUNT = 'hive.graphql.error.count';
export const SEMATTRS_HIVE_GRAPHQL_ERROR_CODES = 'hive.graphql.error.codes';
export const SEMATTRS_HIVE_GRAPHQL_ERROR_SCHEMA_COORDINATES =
'hive.graphql.error.coordinates';
export const SEMATTRS_HIVE_GRAPHQL_ERROR_CODE = 'hive.graphql.error.code';
export const SEMATTRS_HIVE_GRAPHQL_ERROR_SCHEMA_COORDINATE =
'hive.graphql.error.coordinate';
export const SEMATTRS_HIVE_GRAPHQL_ERROR_PATH = 'hive.graphql.error.path';
export const SEMATTRS_HIVE_GRAPHQL_ERROR_MESSAGE = 'hive.graphql.error.message';
export const SEMATTRS_HIVE_GRAPHQL_ERROR_LOCATIONS =
'hive.graphql.error.locations';

// Gateway-specific attributes
export const SEMATTRS_HIVE_GATEWAY_UPSTREAM_SUBGRAPH_NAME =
Expand Down
17 changes: 1 addition & 16 deletions packages/plugins/opentelemetry/src/hive-span-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@ import {
} from '@opentelemetry/sdk-trace-base';
import type { SpanImpl } from '@opentelemetry/sdk-trace-base/build/src/Span';
import { SEMATTRS_HTTP_METHOD } from '@opentelemetry/semantic-conventions';
import {
SEMATTRS_HIVE_GATEWAY_OPERATION_SUBGRAPH_NAMES,
SEMATTRS_HIVE_GRAPHQL_ERROR_CODES,
SEMATTRS_HIVE_GRAPHQL_ERROR_COUNT,
} from './attributes';
import { SEMATTRS_HIVE_GATEWAY_OPERATION_SUBGRAPH_NAMES } from './attributes';

export type HiveTracingSpanProcessorOptions =
| {
Expand Down Expand Up @@ -152,11 +148,6 @@ export class HiveTracingSpanProcessor implements SpanProcessor {
return;
}

if (SPANS_WITH_ERRORS.includes(span.name)) {
copyAttribute(span, operationSpan, SEMATTRS_HIVE_GRAPHQL_ERROR_CODES);
copyAttribute(span, operationSpan, SEMATTRS_HIVE_GRAPHQL_ERROR_COUNT);
}

if (span.name === 'graphql.execute') {
copyAttribute(
span,
Expand Down Expand Up @@ -208,9 +199,3 @@ function isOperationSpan(span: Span): boolean {
const followingChar = span.name.at(17);
return !followingChar || followingChar === ' ';
}

const SPANS_WITH_ERRORS = [
'graphql.parse',
'graphql.validate',
'graphql.execute',
];
11 changes: 10 additions & 1 deletion packages/plugins/opentelemetry/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
type GatewayPlugin,
} from '@graphql-hive/gateway-runtime';
import { getHeadersObj } from '@graphql-mesh/utils';
import { ExecutionArgs } from '@graphql-tools/executor';
import { ExecutionRequest, fakePromise } from '@graphql-tools/utils';
import { unfakePromise } from '@whatwg-node/promise-helpers';
import {
Expand Down Expand Up @@ -36,6 +37,7 @@ import {
createSchemaLoadingSpan,
createSubgraphExecuteSpan,
createUpstreamHttpFetchSpan,
isGraphQLError,
OperationHashingFn,
recordCacheError,
recordCacheEvent,
Expand Down Expand Up @@ -465,7 +467,9 @@ export function useOpenTelemetry(
try {
wrapped();
} catch (err) {
registerException(forOperation.otel!.current, err);
if (err instanceof Error && !isGraphQLError(err)) {
registerException(forOperation.otel!.current, err);
}
throw err;
} finally {
trace.getSpan(forOperation.otel!.current)?.end();
Expand Down Expand Up @@ -791,6 +795,7 @@ export function useOpenTelemetry(
return ({ result }) => {
setGraphQLParseAttributes({
ctx: getContext(state),
operationCtx: state.forOperation.otel!.root,
operationName: gqlCtx.params.operationName,
query: gqlCtx.params.query?.trim(),
result,
Expand All @@ -816,6 +821,7 @@ export function useOpenTelemetry(
return ({ result }) => {
setGraphQLValidateAttributes({
ctx: getContext(state),
operationCtx: state.forOperation.otel!.root,
result,
document: params.documentAST,
operationName: gqlCtx.params.operationName,
Expand All @@ -832,6 +838,8 @@ export function useOpenTelemetry(
return;
}

(args as ExecutionArgs).schemaCoordinateInErrors = true;

const ctx = getContext(state);
setGraphQLExecutionAttributes({
ctx,
Expand All @@ -847,6 +855,7 @@ export function useOpenTelemetry(
setGraphQLExecutionResultAttributes({
ctx,
result,
operationCtx: state.forOperation.otel!.root,
subgraphNames: state.forOperation.subgraphNames,
});
},
Expand Down
16 changes: 12 additions & 4 deletions packages/plugins/opentelemetry/src/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
ATTR_SERVICE_VERSION,
} from '@opentelemetry/semantic-conventions';
import { getEnvBool, getEnvStr } from '~internal/env';
import { LogLevel } from 'rollup';
import {
HiveTracingSpanProcessor,
HiveTracingSpanProcessorOptions,
Expand Down Expand Up @@ -144,7 +145,7 @@ type OpentelemetrySetupOptions = TracingOptions &
* The Logger to be used by this utility.
* A child of this logger will be used for OTEL diag API, unless `configureDiagLogger` is false
*/
log?: Logger;
log?: Logger | false | LogLevel;
/**
* Configure Opentelemetry `diag` API to use Gateway's logger.
*
Expand All @@ -157,7 +158,10 @@ type OpentelemetrySetupOptions = TracingOptions &
};

export function openTelemetrySetup(options: OpentelemetrySetupOptions) {
const log = options.log || new Logger();
const log =
!options.log || typeof options.log === 'string'
? new Logger({ level: options.log })
: options.log;

if (getEnvBool('OTEL_SDK_DISABLED')) {
log.warn(
Expand Down Expand Up @@ -303,10 +307,14 @@ export type HiveTracingOptions = { target?: string } & (
export function hiveTracingSetup(
config: HiveTracingOptions & {
contextManager: ContextManager | null;
log?: Logger;
log?: Logger | false | LogLevel;
},
) {
const log = config.log || new Logger();
const log =
!config.log || typeof config.log === 'string'
? new Logger({ level: config.log })
: config.log;

config.target ??= getEnvStr('HIVE_TARGET');

if (!config.target) {
Expand Down
Loading
Loading