Skip to content

Commit

Permalink
Merge branch 'main' into readme
Browse files Browse the repository at this point in the history
  • Loading branch information
obecny authored Apr 12, 2021
2 parents ea1308d + 9371029 commit 88bf67c
Show file tree
Hide file tree
Showing 50 changed files with 2,040 additions and 993 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class CompositePropagator implements TextMapPropagator {
this._propagators
// older propagators may not have fields function, null check to be sure
.map(p => (typeof p.fields === 'function' ? p.fields() : []))
.reduce((x, y) => x.concat(y))
.reduce((x, y) => x.concat(y), [])
)
);
}
Expand Down
14 changes: 9 additions & 5 deletions packages/opentelemetry-core/src/utils/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ function isEnvVarANumber(key: unknown): key is keyof ENVIRONMENT_NUMBERS {
);
}

const ENVIRONMENT_LISTS_KEYS = ['OTEL_NO_PATCH_MODULES'] as const;
const ENVIRONMENT_LISTS_KEYS = [
'OTEL_NO_PATCH_MODULES',
'OTEL_PROPAGATORS',
] as const;

type ENVIRONMENT_LISTS = {
[K in typeof ENVIRONMENT_LISTS_KEYS[number]]?: string[];
Expand Down Expand Up @@ -83,21 +86,22 @@ export const DEFAULT_ENVIRONMENT: Required<ENVIRONMENT> = {
HOSTNAME: '',
KUBERNETES_SERVICE_HOST: '',
NAMESPACE: '',
OTEL_BSP_EXPORT_TIMEOUT: 30000,
OTEL_BSP_MAX_EXPORT_BATCH_SIZE: 512,
OTEL_BSP_MAX_QUEUE_SIZE: 2048,
OTEL_BSP_SCHEDULE_DELAY: 5000,
OTEL_EXPORTER_JAEGER_AGENT_HOST: '',
OTEL_EXPORTER_JAEGER_ENDPOINT: '',
OTEL_EXPORTER_JAEGER_PASSWORD: '',
OTEL_EXPORTER_JAEGER_USER: '',
OTEL_LOG_LEVEL: DiagLogLevel.INFO,
OTEL_NO_PATCH_MODULES: [],
OTEL_PROPAGATORS: ['tracecontext', 'baggage'],
OTEL_RESOURCE_ATTRIBUTES: '',
OTEL_SAMPLING_PROBABILITY: 1,
OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT: 1000,
OTEL_SPAN_EVENT_COUNT_LIMIT: 1000,
OTEL_SPAN_LINK_COUNT_LIMIT: 1000,
OTEL_BSP_EXPORT_TIMEOUT: 30000,
OTEL_BSP_MAX_EXPORT_BATCH_SIZE: 512,
OTEL_BSP_MAX_QUEUE_SIZE: 2048,
OTEL_BSP_SCHEDULE_DELAY: 5000,
};

/**
Expand Down
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
Loading

0 comments on commit 88bf67c

Please sign in to comment.