Skip to content

Commit 84b99ef

Browse files
authored
observability: ensure that each span has otel.scope.name,version (#2105)
* observability: ensure that each span has otel.scope.name,version This change ensures that each span created by the code in this library is annotated with: * otel.scope.name = "cloud.google.com/nodejs/spanner" * otel.scope.version = <LIBRARY_VERSION> Updates #2079 * Address review feedback to use TRACER_* not LIB_*
1 parent a0ef202 commit 84b99ef

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

observability-test/observability.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,17 @@ const {SpanStatusCode, TracerProvider} = require('@opentelemetry/api');
2525
// eslint-disable-next-line n/no-extraneous-require
2626
const {SimpleSpanProcessor} = require('@opentelemetry/sdk-trace-base');
2727
const {
28+
TRACER_NAME,
29+
TRACER_VERSION,
2830
SPAN_NAMESPACE_PREFIX,
2931
getActiveOrNoopSpan,
3032
setSpanError,
3133
setSpanErrorAndException,
3234
startTrace,
3335
} = require('../src/instrument');
3436
const {
37+
ATTR_OTEL_SCOPE_NAME,
38+
ATTR_OTEL_SCOPE_VERSION,
3539
SEMATTRS_DB_NAME,
3640
SEMATTRS_DB_SQL_TABLE,
3741
SEMATTRS_DB_STATEMENT,
@@ -129,6 +133,18 @@ describe('startTrace', () => {
129133
it('with semantic attributes', () => {
130134
const opts = {tableName: 'table', dbName: 'db'};
131135
startTrace('aSpan', opts, span => {
136+
assert.equal(
137+
span.attributes[ATTR_OTEL_SCOPE_NAME],
138+
TRACER_NAME,
139+
'Missing OTEL_SCOPE_NAME attribute'
140+
);
141+
142+
assert.equal(
143+
span.attributes[ATTR_OTEL_SCOPE_VERSION],
144+
TRACER_VERSION,
145+
'Missing OTEL_SCOPE_VERSION attribute'
146+
);
147+
132148
assert.equal(
133149
span.attributes[SEMATTRS_DB_SYSTEM],
134150
'spanner',

src/instrument.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616

1717
import {
18+
ATTR_OTEL_SCOPE_NAME,
19+
ATTR_OTEL_SCOPE_VERSION,
1820
SEMATTRS_DB_NAME,
1921
SEMATTRS_DB_STATEMENT,
2022
SEMATTRS_DB_SYSTEM,
@@ -53,6 +55,8 @@ export type {observabilityOptions as ObservabilityOptions};
5355
const TRACER_NAME = 'cloud.google.com/nodejs/spanner';
5456
const TRACER_VERSION = '7.14.0'; // Manually hard coded, TODO: remove
5557

58+
export {TRACER_NAME, TRACER_VERSION}; // Only exported for testing.
59+
5660
/**
5761
* getTracer fetches the tracer from the provided tracerProvider.
5862
* @param {TracerProvider} [tracerProvider] optional custom tracer provider
@@ -102,6 +106,8 @@ export function startTrace<T>(
102106
{kind: SpanKind.CLIENT},
103107
span => {
104108
span.setAttribute(SEMATTRS_DB_SYSTEM, 'spanner');
109+
span.setAttribute(ATTR_OTEL_SCOPE_NAME, TRACER_NAME);
110+
span.setAttribute(ATTR_OTEL_SCOPE_VERSION, TRACER_VERSION);
105111

106112
if (config.tableName) {
107113
span.setAttribute(SEMATTRS_DB_SQL_TABLE, config.tableName);

0 commit comments

Comments
 (0)