Skip to content

Commit 29a92db

Browse files
EmrysMyrddingithub-actions[bot]
authored andcommitted
feat(opentelemetry): Use env variables for resource attributes (#1036)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 838e985 commit 29a92db

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

packages/plugins/opentelemetry/src/plugin.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ import {
6565
setUpstreamFetchAttributes,
6666
setUpstreamFetchResponseAttributes,
6767
} from './spans';
68-
import { tryContextManagerSetup } from './utils';
68+
import { getEnvVar, tryContextManagerSetup } from './utils';
6969

7070
type BooleanOrPredicate<TInput = never> =
7171
| boolean
@@ -103,11 +103,18 @@ interface OpenTelemetryGatewayPluginOptionsWithInit {
103103
*/
104104
exporters: MaybePromise<SpanProcessor>[];
105105
/**
106-
* Service name to use for OpenTelemetry NodeSDK resource option (default: 'Gateway').
106+
* Service name to use for OpenTelemetry Resource option (default: 'Gateway').
107107
*
108108
* Does not apply when `initializeNodeSDK` is `false`.
109109
*/
110110
serviceName?: string;
111+
/**
112+
* Service version to use for OpenTelemetry Resource option (default: Hive Gateway version).
113+
*
114+
* Note: This can also be set by using `OTEL_SERVICE_VERSION` environment variable.
115+
*/
116+
serviceVersion?: string;
117+
111118
/**
112119
* Whether to rely on OTEL context api for span correlation.
113120
* - `undefined` (default): the plugin will try to enable context manager if possible.
@@ -315,8 +322,11 @@ export function useOpenTelemetry(
315322

316323
const resource = detectResources().merge(
317324
resourceFromAttributes({
318-
[SEMRESATTRS_SERVICE_NAME]: options.serviceName ?? 'Gateway',
319-
[ATTR_SERVICE_VERSION]: yogaVersion.promise,
325+
[SEMRESATTRS_SERVICE_NAME]:
326+
options.serviceName ?? getEnvVar('OTEL_SERVICE_NAME', 'Gateway'),
327+
[ATTR_SERVICE_VERSION]:
328+
options.serviceVersion ??
329+
getEnvVar('OTEL_SERVICE_VERSION', yogaVersion.promise),
320330
}),
321331
);
322332

packages/plugins/opentelemetry/src/utils.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,9 @@ export function isContextManagerCompatibleWithAsync(): Promise<boolean> {
3333
return (context.active().getValue(symbol) as boolean) || false;
3434
});
3535
}
36+
37+
export const getEnvVar =
38+
'process' in globalThis
39+
? <T>(name: string, defaultValue: T): string | T =>
40+
process.env[name] || defaultValue
41+
: <T>(_name: string, defaultValue: T): string | T => defaultValue;

0 commit comments

Comments
 (0)