Skip to content

Commit 031b0f4

Browse files
feat: add semconv generator for semantic-conventions-package (#2083)
Co-authored-by: Weyert de Boer <[email protected]>
1 parent 7f7afa7 commit 031b0f4

File tree

41 files changed

+1735
-914
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1735
-914
lines changed

packages/opentelemetry-instrumentation-fetch/src/enums/AttributeNames.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,6 @@
1919
*/
2020
export enum AttributeNames {
2121
COMPONENT = 'component',
22+
HTTP_ERROR_NAME = 'http.error_name',
23+
HTTP_STATUS_TEXT = 'http.status_text',
2224
}

packages/opentelemetry-instrumentation-fetch/src/fetch.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {
2323
import * as core from '@opentelemetry/core';
2424
import * as web from '@opentelemetry/web';
2525
import { AttributeNames } from './enums/AttributeNames';
26-
import { HttpAttribute } from '@opentelemetry/semantic-conventions';
26+
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
2727
import { FetchError, FetchResponse, SpanData } from './types';
2828
import { VERSION } from './version';
2929

@@ -121,16 +121,16 @@ export class FetchInstrumentation extends InstrumentationBase<
121121
response: FetchResponse
122122
): void {
123123
const parsedUrl = web.parseUrl(response.url);
124-
span.setAttribute(HttpAttribute.HTTP_STATUS_CODE, response.status);
124+
span.setAttribute(SemanticAttributes.HTTP_STATUS_CODE, response.status);
125125
if (response.statusText != null) {
126-
span.setAttribute(HttpAttribute.HTTP_STATUS_TEXT, response.statusText);
126+
span.setAttribute(AttributeNames.HTTP_STATUS_TEXT, response.statusText);
127127
}
128-
span.setAttribute(HttpAttribute.HTTP_HOST, parsedUrl.host);
128+
span.setAttribute(SemanticAttributes.HTTP_HOST, parsedUrl.host);
129129
span.setAttribute(
130-
HttpAttribute.HTTP_SCHEME,
130+
SemanticAttributes.HTTP_SCHEME,
131131
parsedUrl.protocol.replace(':', '')
132132
);
133-
span.setAttribute(HttpAttribute.HTTP_USER_AGENT, navigator.userAgent);
133+
span.setAttribute(SemanticAttributes.HTTP_USER_AGENT, navigator.userAgent);
134134
}
135135

136136
/**
@@ -196,8 +196,8 @@ export class FetchInstrumentation extends InstrumentationBase<
196196
kind: api.SpanKind.CLIENT,
197197
attributes: {
198198
[AttributeNames.COMPONENT]: this.moduleName,
199-
[HttpAttribute.HTTP_METHOD]: method,
200-
[HttpAttribute.HTTP_URL]: url,
199+
[SemanticAttributes.HTTP_METHOD]: method,
200+
[SemanticAttributes.HTTP_URL]: url,
201201
},
202202
});
203203
}

packages/opentelemetry-instrumentation-fetch/test/fetch.test.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import * as assert from 'assert';
3737
import * as sinon from 'sinon';
3838
import { FetchInstrumentation, FetchInstrumentationConfig } from '../src';
3939
import { AttributeNames } from '../src/enums/AttributeNames';
40-
import { HttpAttribute } from '@opentelemetry/semantic-conventions';
40+
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
4141

4242
class DummySpanExporter implements tracing.SpanExporter {
4343
export(spans: any) {}
@@ -335,37 +335,37 @@ describe('fetch', () => {
335335
assert.strictEqual(
336336
attributes[keys[1]],
337337
'GET',
338-
`attributes ${HttpAttribute.HTTP_METHOD} is wrong`
338+
`attributes ${SemanticAttributes.HTTP_METHOD} is wrong`
339339
);
340340
assert.strictEqual(
341341
attributes[keys[2]],
342342
url,
343-
`attributes ${HttpAttribute.HTTP_URL} is wrong`
343+
`attributes ${SemanticAttributes.HTTP_URL} is wrong`
344344
);
345345
assert.strictEqual(
346346
attributes[keys[3]],
347347
200,
348-
`attributes ${HttpAttribute.HTTP_STATUS_CODE} is wrong`
348+
`attributes ${SemanticAttributes.HTTP_STATUS_CODE} is wrong`
349349
);
350350
assert.ok(
351351
attributes[keys[4]] === 'OK' || attributes[keys[4]] === '',
352-
`attributes ${HttpAttribute.HTTP_STATUS_TEXT} is wrong`
352+
`attributes ${AttributeNames.HTTP_STATUS_TEXT} is wrong`
353353
);
354354
assert.ok(
355355
(attributes[keys[5]] as string).indexOf('localhost') === 0,
356-
`attributes ${HttpAttribute.HTTP_HOST} is wrong`
356+
`attributes ${SemanticAttributes.HTTP_HOST} is wrong`
357357
);
358358
assert.ok(
359359
attributes[keys[6]] === 'http' || attributes[keys[6]] === 'https',
360-
`attributes ${HttpAttribute.HTTP_SCHEME} is wrong`
360+
`attributes ${SemanticAttributes.HTTP_SCHEME} is wrong`
361361
);
362362
assert.ok(
363363
attributes[keys[7]] !== '',
364-
`attributes ${HttpAttribute.HTTP_USER_AGENT} is not defined`
364+
`attributes ${SemanticAttributes.HTTP_USER_AGENT} is not defined`
365365
);
366366
assert.ok(
367367
(attributes[keys[8]] as number) > 0,
368-
`attributes ${HttpAttribute.HTTP_RESPONSE_CONTENT_LENGTH} is <= 0`
368+
`attributes ${SemanticAttributes.HTTP_RESPONSE_CONTENT_LENGTH} is <= 0`
369369
);
370370

371371
assert.strictEqual(keys.length, 9, 'number of attributes is wrong');
@@ -764,7 +764,7 @@ describe('fetch', () => {
764764
assert.strictEqual(
765765
attributes[keys[3]],
766766
200,
767-
`Missing basic attribute ${HttpAttribute.HTTP_STATUS_CODE}`
767+
`Missing basic attribute ${SemanticAttributes.HTTP_STATUS_CODE}`
768768
);
769769
});
770770
});
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/**
18+
* https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/semantic_conventions/http.md
19+
*/
20+
export enum AttributeNames {
21+
GRPC_KIND = 'grpc.kind', // SERVER or CLIENT
22+
GRPC_METHOD = 'grpc.method',
23+
GRPC_ERROR_NAME = 'grpc.error_name',
24+
GRPC_ERROR_MESSAGE = 'grpc.error_message',
25+
}

packages/opentelemetry-instrumentation-grpc/src/grpc-js/clientUtils.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import {
2424
propagation,
2525
context,
2626
} from '@opentelemetry/api';
27-
import { RpcAttribute } from '@opentelemetry/semantic-conventions';
2827
import type * as grpcJs from '@grpc/grpc-js';
2928
import {
3029
_grpcStatusCodeToSpanStatus,
@@ -33,6 +32,8 @@ import {
3332
} from '../utils';
3433
import { CALL_SPAN_ENDED } from './serverUtils';
3534
import { EventEmitter } from 'events';
35+
import { AttributeNames } from '../enums';
36+
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
3637

3738
/**
3839
* Parse a package method list and return a list of methods to patch
@@ -89,16 +90,19 @@ export function makeGrpcClientRemoteCall(
8990
if (err) {
9091
if (err.code) {
9192
span.setStatus(_grpcStatusCodeToSpanStatus(err.code));
92-
span.setAttribute(RpcAttribute.GRPC_STATUS_CODE, err.code.toString());
93+
span.setAttribute(
94+
SemanticAttributes.RPC_GRPC_STATUS_CODE,
95+
err.code.toString()
96+
);
9397
}
9498
span.setAttributes({
95-
[RpcAttribute.GRPC_ERROR_NAME]: err.name,
96-
[RpcAttribute.GRPC_ERROR_MESSAGE]: err.message,
99+
[AttributeNames.GRPC_ERROR_NAME]: err.name,
100+
[AttributeNames.GRPC_ERROR_MESSAGE]: err.message,
97101
});
98102
} else {
99103
span.setStatus({ code: SpanStatusCode.UNSET });
100104
span.setAttribute(
101-
RpcAttribute.GRPC_STATUS_CODE,
105+
SemanticAttributes.RPC_GRPC_STATUS_CODE,
102106
SpanStatusCode.UNSET.toString()
103107
);
104108
}
@@ -124,8 +128,8 @@ export function makeGrpcClientRemoteCall(
124128
}
125129

126130
span.setAttributes({
127-
[RpcAttribute.GRPC_METHOD]: original.path,
128-
[RpcAttribute.GRPC_KIND]: SpanKind.CLIENT,
131+
[AttributeNames.GRPC_METHOD]: original.path,
132+
[AttributeNames.GRPC_KIND]: SpanKind.CLIENT,
129133
});
130134

131135
setSpanContext(metadata);
@@ -154,8 +158,8 @@ export function makeGrpcClientRemoteCall(
154158
message: err.message,
155159
});
156160
span.setAttributes({
157-
[RpcAttribute.GRPC_ERROR_NAME]: err.name,
158-
[RpcAttribute.GRPC_ERROR_MESSAGE]: err.message,
161+
[AttributeNames.GRPC_ERROR_NAME]: err.name,
162+
[AttributeNames.GRPC_ERROR_MESSAGE]: err.message,
159163
});
160164

161165
endSpan();

packages/opentelemetry-instrumentation-grpc/src/grpc-js/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ import {
4242
setSpan,
4343
diag,
4444
} from '@opentelemetry/api';
45-
import { RpcAttribute } from '@opentelemetry/semantic-conventions';
4645
import {
4746
shouldNotTraceServerCall,
4847
handleServerFunction,
@@ -54,6 +53,7 @@ import {
5453
getMetadata,
5554
} from './clientUtils';
5655
import { EventEmitter } from 'events';
56+
import { AttributeNames } from '../enums';
5757

5858
export class GrpcJsInstrumentation extends InstrumentationBase {
5959
constructor(
@@ -197,7 +197,7 @@ export class GrpcJsInstrumentation extends InstrumentationBase {
197197
const span = instrumentation.tracer
198198
.startSpan(spanName, spanOptions)
199199
.setAttributes({
200-
[RpcAttribute.GRPC_KIND]: spanOptions.kind,
200+
[AttributeNames.GRPC_KIND]: spanOptions.kind,
201201
});
202202

203203
context.with(setSpan(context.active(), span), () => {

packages/opentelemetry-instrumentation-grpc/src/grpc-js/serverUtils.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
*/
2222

2323
import { context, Span, SpanStatusCode } from '@opentelemetry/api';
24-
import { RpcAttribute } from '@opentelemetry/semantic-conventions';
2524
import type * as grpcJs from '@grpc/grpc-js';
2625
import type {
2726
ServerCallWithMeta,
@@ -34,6 +33,8 @@ import {
3433
_methodIsIgnored,
3534
} from '../utils';
3635
import { IgnoreMatcher } from '../types';
36+
import { AttributeNames } from '../enums';
37+
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
3738

3839
export const CALL_SPAN_ENDED = Symbol('opentelemetry call span ended');
3940

@@ -70,7 +71,7 @@ function serverStreamAndBidiHandler<RequestType, ResponseType>(
7071
code: SpanStatusCode.UNSET,
7172
});
7273
span.setAttribute(
73-
RpcAttribute.GRPC_STATUS_CODE,
74+
SemanticAttributes.RPC_GRPC_STATUS_CODE,
7475
SpanStatusCode.OK.toString()
7576
);
7677

@@ -90,8 +91,8 @@ function serverStreamAndBidiHandler<RequestType, ResponseType>(
9091
message: err.message,
9192
});
9293
span.setAttributes({
93-
[RpcAttribute.GRPC_ERROR_NAME]: err.name,
94-
[RpcAttribute.GRPC_ERROR_MESSAGE]: err.message,
94+
[AttributeNames.GRPC_ERROR_NAME]: err.name,
95+
[AttributeNames.GRPC_ERROR_MESSAGE]: err.message,
9596
});
9697
endSpan();
9798
});
@@ -121,16 +122,19 @@ function clientStreamAndUnaryHandler<RequestType, ResponseType>(
121122
code: _grpcStatusCodeToOpenTelemetryStatusCode(err.code),
122123
message: err.message,
123124
});
124-
span.setAttribute(RpcAttribute.GRPC_STATUS_CODE, err.code.toString());
125+
span.setAttribute(
126+
SemanticAttributes.RPC_GRPC_STATUS_CODE,
127+
err.code.toString()
128+
);
125129
}
126130
span.setAttributes({
127-
[RpcAttribute.GRPC_ERROR_NAME]: err.name,
128-
[RpcAttribute.GRPC_ERROR_MESSAGE]: err.message,
131+
[AttributeNames.GRPC_ERROR_NAME]: err.name,
132+
[AttributeNames.GRPC_ERROR_MESSAGE]: err.message,
129133
});
130134
} else {
131135
span.setStatus({ code: SpanStatusCode.UNSET });
132136
span.setAttribute(
133-
RpcAttribute.GRPC_STATUS_CODE,
137+
SemanticAttributes.RPC_GRPC_STATUS_CODE,
134138
SpanStatusCode.OK.toString()
135139
);
136140
}

packages/opentelemetry-instrumentation-grpc/src/grpc/clientUtils.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import type * as grpcTypes from 'grpc';
1818
import type * as events from 'events';
1919
import { SendUnaryDataCallback, GrpcClientFunc } from './types';
20-
import { RpcAttribute } from '@opentelemetry/semantic-conventions';
20+
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
2121
import {
2222
context,
2323
Span,
@@ -31,6 +31,7 @@ import {
3131
_grpcStatusCodeToOpenTelemetryStatusCode,
3232
findIndex,
3333
} from '../utils';
34+
import { AttributeNames } from '../enums';
3435

3536
/**
3637
* This method handles the client remote call
@@ -55,16 +56,19 @@ export const makeGrpcClientRemoteCall = function (
5556
if (err) {
5657
if (err.code) {
5758
span.setStatus(_grpcStatusCodeToSpanStatus(err.code));
58-
span.setAttribute(RpcAttribute.GRPC_STATUS_CODE, err.code.toString());
59+
span.setAttribute(
60+
SemanticAttributes.RPC_GRPC_STATUS_CODE,
61+
err.code.toString()
62+
);
5963
}
6064
span.setAttributes({
61-
[RpcAttribute.GRPC_ERROR_NAME]: err.name,
62-
[RpcAttribute.GRPC_ERROR_MESSAGE]: err.message,
65+
[AttributeNames.GRPC_ERROR_NAME]: err.name,
66+
[AttributeNames.GRPC_ERROR_MESSAGE]: err.message,
6367
});
6468
} else {
6569
span.setStatus({ code: SpanStatusCode.UNSET });
6670
span.setAttribute(
67-
RpcAttribute.GRPC_STATUS_CODE,
71+
SemanticAttributes.RPC_GRPC_STATUS_CODE,
6872
grpcClient.status.OK.toString()
6973
);
7074
}
@@ -96,8 +100,8 @@ export const makeGrpcClientRemoteCall = function (
96100

97101
span.addEvent('sent');
98102
span.setAttributes({
99-
[RpcAttribute.GRPC_METHOD]: original.path,
100-
[RpcAttribute.GRPC_KIND]: SpanKind.CLIENT,
103+
[AttributeNames.GRPC_METHOD]: original.path,
104+
[AttributeNames.GRPC_KIND]: SpanKind.CLIENT,
101105
});
102106

103107
setSpanContext(metadata);
@@ -123,8 +127,8 @@ export const makeGrpcClientRemoteCall = function (
123127
message: err.message,
124128
});
125129
span.setAttributes({
126-
[RpcAttribute.GRPC_ERROR_NAME]: err.name,
127-
[RpcAttribute.GRPC_ERROR_MESSAGE]: err.message,
130+
[AttributeNames.GRPC_ERROR_NAME]: err.name,
131+
[AttributeNames.GRPC_ERROR_MESSAGE]: err.message,
128132
});
129133
endSpan();
130134
}
@@ -135,7 +139,7 @@ export const makeGrpcClientRemoteCall = function (
135139
(status: SpanStatus) => {
136140
span.setStatus({ code: SpanStatusCode.UNSET });
137141
span.setAttribute(
138-
RpcAttribute.GRPC_STATUS_CODE,
142+
SemanticAttributes.RPC_GRPC_STATUS_CODE,
139143
status.code.toString()
140144
);
141145
endSpan();

packages/opentelemetry-instrumentation-grpc/src/grpc/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import {
2929
GrpcClientFunc,
3030
} from './types';
3131
import { GrpcInstrumentationConfig } from '../types';
32-
import { RpcAttribute } from '@opentelemetry/semantic-conventions';
3332
import {
3433
context,
3534
propagation,
@@ -45,6 +44,7 @@ import {
4544
} from './serverUtils';
4645
import { makeGrpcClientRemoteCall, getMetadata } from './clientUtils';
4746
import { _methodIsIgnored } from '../utils';
47+
import { AttributeNames } from '../enums';
4848

4949
/**
5050
* Holding reference to grpc module here to access constant of grpc modules
@@ -205,7 +205,7 @@ export class GrpcNativeInstrumentation extends InstrumentationBase<
205205
const span = instrumentation.tracer
206206
.startSpan(spanName, spanOptions)
207207
.setAttributes({
208-
[RpcAttribute.GRPC_KIND]: spanOptions.kind,
208+
[AttributeNames.GRPC_KIND]: spanOptions.kind,
209209
});
210210

211211
context.with(setSpan(context.active(), span), () => {

0 commit comments

Comments
 (0)