Skip to content

Commit

Permalink
Merge 628bb71 into 7f7afa7
Browse files Browse the repository at this point in the history
  • Loading branch information
weyert authored Apr 10, 2021
2 parents 7f7afa7 + 628bb71 commit a5dc4fc
Show file tree
Hide file tree
Showing 41 changed files with 1,735 additions and 914 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@
*/
export enum AttributeNames {
COMPONENT = 'component',
HTTP_ERROR_NAME = 'http.error_name',
HTTP_STATUS_TEXT = 'http.status_text',
}
16 changes: 8 additions & 8 deletions packages/opentelemetry-instrumentation-fetch/src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
import * as core from '@opentelemetry/core';
import * as web from '@opentelemetry/web';
import { AttributeNames } from './enums/AttributeNames';
import { HttpAttribute } from '@opentelemetry/semantic-conventions';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import { FetchError, FetchResponse, SpanData } from './types';
import { VERSION } from './version';

Expand Down Expand Up @@ -121,16 +121,16 @@ export class FetchInstrumentation extends InstrumentationBase<
response: FetchResponse
): void {
const parsedUrl = web.parseUrl(response.url);
span.setAttribute(HttpAttribute.HTTP_STATUS_CODE, response.status);
span.setAttribute(SemanticAttributes.HTTP_STATUS_CODE, response.status);
if (response.statusText != null) {
span.setAttribute(HttpAttribute.HTTP_STATUS_TEXT, response.statusText);
span.setAttribute(AttributeNames.HTTP_STATUS_TEXT, response.statusText);
}
span.setAttribute(HttpAttribute.HTTP_HOST, parsedUrl.host);
span.setAttribute(SemanticAttributes.HTTP_HOST, parsedUrl.host);
span.setAttribute(
HttpAttribute.HTTP_SCHEME,
SemanticAttributes.HTTP_SCHEME,
parsedUrl.protocol.replace(':', '')
);
span.setAttribute(HttpAttribute.HTTP_USER_AGENT, navigator.userAgent);
span.setAttribute(SemanticAttributes.HTTP_USER_AGENT, navigator.userAgent);
}

/**
Expand Down Expand Up @@ -196,8 +196,8 @@ export class FetchInstrumentation extends InstrumentationBase<
kind: api.SpanKind.CLIENT,
attributes: {
[AttributeNames.COMPONENT]: this.moduleName,
[HttpAttribute.HTTP_METHOD]: method,
[HttpAttribute.HTTP_URL]: url,
[SemanticAttributes.HTTP_METHOD]: method,
[SemanticAttributes.HTTP_URL]: url,
},
});
}
Expand Down
20 changes: 10 additions & 10 deletions packages/opentelemetry-instrumentation-fetch/test/fetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import * as assert from 'assert';
import * as sinon from 'sinon';
import { FetchInstrumentation, FetchInstrumentationConfig } from '../src';
import { AttributeNames } from '../src/enums/AttributeNames';
import { HttpAttribute } from '@opentelemetry/semantic-conventions';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';

class DummySpanExporter implements tracing.SpanExporter {
export(spans: any) {}
Expand Down Expand Up @@ -335,37 +335,37 @@ describe('fetch', () => {
assert.strictEqual(
attributes[keys[1]],
'GET',
`attributes ${HttpAttribute.HTTP_METHOD} is wrong`
`attributes ${SemanticAttributes.HTTP_METHOD} is wrong`
);
assert.strictEqual(
attributes[keys[2]],
url,
`attributes ${HttpAttribute.HTTP_URL} is wrong`
`attributes ${SemanticAttributes.HTTP_URL} is wrong`
);
assert.strictEqual(
attributes[keys[3]],
200,
`attributes ${HttpAttribute.HTTP_STATUS_CODE} is wrong`
`attributes ${SemanticAttributes.HTTP_STATUS_CODE} is wrong`
);
assert.ok(
attributes[keys[4]] === 'OK' || attributes[keys[4]] === '',
`attributes ${HttpAttribute.HTTP_STATUS_TEXT} is wrong`
`attributes ${AttributeNames.HTTP_STATUS_TEXT} is wrong`
);
assert.ok(
(attributes[keys[5]] as string).indexOf('localhost') === 0,
`attributes ${HttpAttribute.HTTP_HOST} is wrong`
`attributes ${SemanticAttributes.HTTP_HOST} is wrong`
);
assert.ok(
attributes[keys[6]] === 'http' || attributes[keys[6]] === 'https',
`attributes ${HttpAttribute.HTTP_SCHEME} is wrong`
`attributes ${SemanticAttributes.HTTP_SCHEME} is wrong`
);
assert.ok(
attributes[keys[7]] !== '',
`attributes ${HttpAttribute.HTTP_USER_AGENT} is not defined`
`attributes ${SemanticAttributes.HTTP_USER_AGENT} is not defined`
);
assert.ok(
(attributes[keys[8]] as number) > 0,
`attributes ${HttpAttribute.HTTP_RESPONSE_CONTENT_LENGTH} is <= 0`
`attributes ${SemanticAttributes.HTTP_RESPONSE_CONTENT_LENGTH} is <= 0`
);

assert.strictEqual(keys.length, 9, 'number of attributes is wrong');
Expand Down Expand Up @@ -764,7 +764,7 @@ describe('fetch', () => {
assert.strictEqual(
attributes[keys[3]],
200,
`Missing basic attribute ${HttpAttribute.HTTP_STATUS_CODE}`
`Missing basic attribute ${SemanticAttributes.HTTP_STATUS_CODE}`
);
});
});
Expand Down
25 changes: 25 additions & 0 deletions packages/opentelemetry-instrumentation-grpc/src/enums.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/semantic_conventions/http.md
*/
export enum AttributeNames {
GRPC_KIND = 'grpc.kind', // SERVER or CLIENT
GRPC_METHOD = 'grpc.method',
GRPC_ERROR_NAME = 'grpc.error_name',
GRPC_ERROR_MESSAGE = 'grpc.error_message',
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import {
propagation,
context,
} from '@opentelemetry/api';
import { RpcAttribute } from '@opentelemetry/semantic-conventions';
import type * as grpcJs from '@grpc/grpc-js';
import {
_grpcStatusCodeToSpanStatus,
Expand All @@ -33,6 +32,8 @@ import {
} from '../utils';
import { CALL_SPAN_ENDED } from './serverUtils';
import { EventEmitter } from 'events';
import { AttributeNames } from '../enums';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';

/**
* Parse a package method list and return a list of methods to patch
Expand Down Expand Up @@ -89,16 +90,19 @@ export function makeGrpcClientRemoteCall(
if (err) {
if (err.code) {
span.setStatus(_grpcStatusCodeToSpanStatus(err.code));
span.setAttribute(RpcAttribute.GRPC_STATUS_CODE, err.code.toString());
span.setAttribute(
SemanticAttributes.RPC_GRPC_STATUS_CODE,
err.code.toString()
);
}
span.setAttributes({
[RpcAttribute.GRPC_ERROR_NAME]: err.name,
[RpcAttribute.GRPC_ERROR_MESSAGE]: err.message,
[AttributeNames.GRPC_ERROR_NAME]: err.name,
[AttributeNames.GRPC_ERROR_MESSAGE]: err.message,
});
} else {
span.setStatus({ code: SpanStatusCode.UNSET });
span.setAttribute(
RpcAttribute.GRPC_STATUS_CODE,
SemanticAttributes.RPC_GRPC_STATUS_CODE,
SpanStatusCode.UNSET.toString()
);
}
Expand All @@ -124,8 +128,8 @@ export function makeGrpcClientRemoteCall(
}

span.setAttributes({
[RpcAttribute.GRPC_METHOD]: original.path,
[RpcAttribute.GRPC_KIND]: SpanKind.CLIENT,
[AttributeNames.GRPC_METHOD]: original.path,
[AttributeNames.GRPC_KIND]: SpanKind.CLIENT,
});

setSpanContext(metadata);
Expand Down Expand Up @@ -154,8 +158,8 @@ export function makeGrpcClientRemoteCall(
message: err.message,
});
span.setAttributes({
[RpcAttribute.GRPC_ERROR_NAME]: err.name,
[RpcAttribute.GRPC_ERROR_MESSAGE]: err.message,
[AttributeNames.GRPC_ERROR_NAME]: err.name,
[AttributeNames.GRPC_ERROR_MESSAGE]: err.message,
});

endSpan();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ import {
setSpan,
diag,
} from '@opentelemetry/api';
import { RpcAttribute } from '@opentelemetry/semantic-conventions';
import {
shouldNotTraceServerCall,
handleServerFunction,
Expand All @@ -54,6 +53,7 @@ import {
getMetadata,
} from './clientUtils';
import { EventEmitter } from 'events';
import { AttributeNames } from '../enums';

export class GrpcJsInstrumentation extends InstrumentationBase {
constructor(
Expand Down Expand Up @@ -197,7 +197,7 @@ export class GrpcJsInstrumentation extends InstrumentationBase {
const span = instrumentation.tracer
.startSpan(spanName, spanOptions)
.setAttributes({
[RpcAttribute.GRPC_KIND]: spanOptions.kind,
[AttributeNames.GRPC_KIND]: spanOptions.kind,
});

context.with(setSpan(context.active(), span), () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
*/

import { context, Span, SpanStatusCode } from '@opentelemetry/api';
import { RpcAttribute } from '@opentelemetry/semantic-conventions';
import type * as grpcJs from '@grpc/grpc-js';
import type {
ServerCallWithMeta,
Expand All @@ -34,6 +33,8 @@ import {
_methodIsIgnored,
} from '../utils';
import { IgnoreMatcher } from '../types';
import { AttributeNames } from '../enums';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';

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

Expand Down Expand Up @@ -70,7 +71,7 @@ function serverStreamAndBidiHandler<RequestType, ResponseType>(
code: SpanStatusCode.UNSET,
});
span.setAttribute(
RpcAttribute.GRPC_STATUS_CODE,
SemanticAttributes.RPC_GRPC_STATUS_CODE,
SpanStatusCode.OK.toString()
);

Expand All @@ -90,8 +91,8 @@ function serverStreamAndBidiHandler<RequestType, ResponseType>(
message: err.message,
});
span.setAttributes({
[RpcAttribute.GRPC_ERROR_NAME]: err.name,
[RpcAttribute.GRPC_ERROR_MESSAGE]: err.message,
[AttributeNames.GRPC_ERROR_NAME]: err.name,
[AttributeNames.GRPC_ERROR_MESSAGE]: err.message,
});
endSpan();
});
Expand Down Expand Up @@ -121,16 +122,19 @@ function clientStreamAndUnaryHandler<RequestType, ResponseType>(
code: _grpcStatusCodeToOpenTelemetryStatusCode(err.code),
message: err.message,
});
span.setAttribute(RpcAttribute.GRPC_STATUS_CODE, err.code.toString());
span.setAttribute(
SemanticAttributes.RPC_GRPC_STATUS_CODE,
err.code.toString()
);
}
span.setAttributes({
[RpcAttribute.GRPC_ERROR_NAME]: err.name,
[RpcAttribute.GRPC_ERROR_MESSAGE]: err.message,
[AttributeNames.GRPC_ERROR_NAME]: err.name,
[AttributeNames.GRPC_ERROR_MESSAGE]: err.message,
});
} else {
span.setStatus({ code: SpanStatusCode.UNSET });
span.setAttribute(
RpcAttribute.GRPC_STATUS_CODE,
SemanticAttributes.RPC_GRPC_STATUS_CODE,
SpanStatusCode.OK.toString()
);
}
Expand Down
24 changes: 14 additions & 10 deletions packages/opentelemetry-instrumentation-grpc/src/grpc/clientUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import type * as grpcTypes from 'grpc';
import type * as events from 'events';
import { SendUnaryDataCallback, GrpcClientFunc } from './types';
import { RpcAttribute } from '@opentelemetry/semantic-conventions';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import {
context,
Span,
Expand All @@ -31,6 +31,7 @@ import {
_grpcStatusCodeToOpenTelemetryStatusCode,
findIndex,
} from '../utils';
import { AttributeNames } from '../enums';

/**
* This method handles the client remote call
Expand All @@ -55,16 +56,19 @@ export const makeGrpcClientRemoteCall = function (
if (err) {
if (err.code) {
span.setStatus(_grpcStatusCodeToSpanStatus(err.code));
span.setAttribute(RpcAttribute.GRPC_STATUS_CODE, err.code.toString());
span.setAttribute(
SemanticAttributes.RPC_GRPC_STATUS_CODE,
err.code.toString()
);
}
span.setAttributes({
[RpcAttribute.GRPC_ERROR_NAME]: err.name,
[RpcAttribute.GRPC_ERROR_MESSAGE]: err.message,
[AttributeNames.GRPC_ERROR_NAME]: err.name,
[AttributeNames.GRPC_ERROR_MESSAGE]: err.message,
});
} else {
span.setStatus({ code: SpanStatusCode.UNSET });
span.setAttribute(
RpcAttribute.GRPC_STATUS_CODE,
SemanticAttributes.RPC_GRPC_STATUS_CODE,
grpcClient.status.OK.toString()
);
}
Expand Down Expand Up @@ -96,8 +100,8 @@ export const makeGrpcClientRemoteCall = function (

span.addEvent('sent');
span.setAttributes({
[RpcAttribute.GRPC_METHOD]: original.path,
[RpcAttribute.GRPC_KIND]: SpanKind.CLIENT,
[AttributeNames.GRPC_METHOD]: original.path,
[AttributeNames.GRPC_KIND]: SpanKind.CLIENT,
});

setSpanContext(metadata);
Expand All @@ -123,8 +127,8 @@ export const makeGrpcClientRemoteCall = function (
message: err.message,
});
span.setAttributes({
[RpcAttribute.GRPC_ERROR_NAME]: err.name,
[RpcAttribute.GRPC_ERROR_MESSAGE]: err.message,
[AttributeNames.GRPC_ERROR_NAME]: err.name,
[AttributeNames.GRPC_ERROR_MESSAGE]: err.message,
});
endSpan();
}
Expand All @@ -135,7 +139,7 @@ export const makeGrpcClientRemoteCall = function (
(status: SpanStatus) => {
span.setStatus({ code: SpanStatusCode.UNSET });
span.setAttribute(
RpcAttribute.GRPC_STATUS_CODE,
SemanticAttributes.RPC_GRPC_STATUS_CODE,
status.code.toString()
);
endSpan();
Expand Down
4 changes: 2 additions & 2 deletions packages/opentelemetry-instrumentation-grpc/src/grpc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import {
GrpcClientFunc,
} from './types';
import { GrpcInstrumentationConfig } from '../types';
import { RpcAttribute } from '@opentelemetry/semantic-conventions';
import {
context,
propagation,
Expand All @@ -45,6 +44,7 @@ import {
} from './serverUtils';
import { makeGrpcClientRemoteCall, getMetadata } from './clientUtils';
import { _methodIsIgnored } from '../utils';
import { AttributeNames } from '../enums';

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

context.with(setSpan(context.active(), span), () => {
Expand Down
Loading

0 comments on commit a5dc4fc

Please sign in to comment.