Skip to content

Commit

Permalink
Merge branch 'main' into instrumentation-no-type
Browse files Browse the repository at this point in the history
  • Loading branch information
blumamir authored Apr 18, 2024
2 parents 4c7ca0e + 73fddf9 commit 872853d
Show file tree
Hide file tree
Showing 47 changed files with 27,226 additions and 16,709 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ For experimental package changes, see the [experimental CHANGELOG](experimental/

### :rocket: (Enhancement)

feat(sdk-trace-base): log resource attributes in ConsoleSpanExporter [#4605](https://github.com/open-telemetry/opentelemetry-js/pull/4605) @pichlermarc
* feat(sdk-trace-base): log resource attributes in ConsoleSpanExporter [#4605](https://github.com/open-telemetry/opentelemetry-js/pull/4605) @pichlermarc
* feat(propagator-aws-xray): moved AWS Xray propagator from contrib [4603](https://github.com/open-telemetry/opentelemetry-js/pull/4603) @martinkuba
* feat(resources): new experimental detector ServiceInstanceIdDetectorSync that sets the value for `service.instance.id` as random UUID. [#4608](https://github.com/open-telemetry/opentelemetry-js/pull/4608) @maryliag

### :bug: (Bug Fix)

Expand Down
8 changes: 8 additions & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,19 @@ All notable changes to experimental packages in this project will be documented

### :rocket: (Enhancement)

* refactor(instrumentation-grpc): move to use SEMATTRS [#4633](https://github.com/open-telemetry/opentelemetry-js/pull/4633)
* feat(otlp-transformer): consolidate scope/resource creation in transformer [#4600](https://github.com/open-telemetry/opentelemetry-js/pull/4600)
* feat(sdk-logs): print message when attributes are dropped due to attribute count limit [#4614](https://github.com/open-telemetry/opentelemetry-js/pull/4614) @HyunnoH
* feat(sdk-node): add usage for the detector ServiceInstanceIdDetectorSync. [#4626](https://github.com/open-telemetry/opentelemetry-js/pull/4626) @maryliag
* The resource detector can be added to default resource detector list by adding the value `serviceinstance` to the list of resource detectors on the environment variable `OTEL_NODE_RESOURCE_DETECTORS`, e.g `OTEL_NODE_RESOURCE_DETECTORS=env,host,os,serviceinstance`
* The value can be overwritten by
* merging a resource containing the `service.instance.id` attribute
* using another resource detector which writes `service.instance.id`

### :bug: (Bug Fix)

* fix(otlp-grpc-exporter-base): avoid TypeError on exporter shutdown [#4612](https://github.com/open-telemetry/opentelemetry-js/pull/4612)
* fix(instrumentation): Don't use `require` to load `package.json` files

### :books: (Refine Doc)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import type {
} from './internal-types';

import { propagation, context } from '@opentelemetry/api';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import { SEMATTRS_RPC_GRPC_STATUS_CODE } from '@opentelemetry/semantic-conventions';
import { AttributeNames } from './enums/AttributeNames';
import { GRPC_STATUS_CODE_OK } from './status-code';
import {
Expand Down Expand Up @@ -81,17 +81,14 @@ export function patchedCallback(
if (err) {
if (err.code) {
span.setStatus(_grpcStatusCodeToSpanStatus(err.code));
span.setAttribute(SemanticAttributes.RPC_GRPC_STATUS_CODE, err.code);
span.setAttribute(SEMATTRS_RPC_GRPC_STATUS_CODE, err.code);
}
span.setAttributes({
[AttributeNames.GRPC_ERROR_NAME]: err.name,
[AttributeNames.GRPC_ERROR_MESSAGE]: err.message,
});
} else {
span.setAttribute(
SemanticAttributes.RPC_GRPC_STATUS_CODE,
GRPC_STATUS_CODE_OK
);
span.setAttribute(SEMATTRS_RPC_GRPC_STATUS_CODE, GRPC_STATUS_CODE_OK);
}

span.end();
Expand Down Expand Up @@ -133,7 +130,7 @@ export function patchResponseStreamEvents(span: Span, call: EventEmitter) {
span.setAttributes({
[AttributeNames.GRPC_ERROR_NAME]: err.name,
[AttributeNames.GRPC_ERROR_MESSAGE]: err.message,
[SemanticAttributes.RPC_GRPC_STATUS_CODE]: err.code,
[SEMATTRS_RPC_GRPC_STATUS_CODE]: err.code,
});

endSpan();
Expand All @@ -145,7 +142,7 @@ export function patchResponseStreamEvents(span: Span, call: EventEmitter) {
}

span.setStatus(_grpcStatusCodeToSpanStatus(status.code));
span.setAttribute(SemanticAttributes.RPC_GRPC_STATUS_CODE, status.code);
span.setAttribute(SEMATTRS_RPC_GRPC_STATUS_CODE, status.code);

endSpan();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,13 @@ import {
isWrapped,
InstrumentationBase,
} from '@opentelemetry/instrumentation';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import {
SEMATTRS_NET_PEER_NAME,
SEMATTRS_NET_PEER_PORT,
SEMATTRS_RPC_METHOD,
SEMATTRS_RPC_SERVICE,
SEMATTRS_RPC_SYSTEM,
} from '@opentelemetry/semantic-conventions';

import {
shouldNotTraceServerCall,
Expand Down Expand Up @@ -274,10 +280,9 @@ export class GrpcInstrumentation extends InstrumentationBase {
const span = instrumentation.tracer
.startSpan(spanName, spanOptions)
.setAttributes({
[SemanticAttributes.RPC_SYSTEM]:
AttributeValues.RPC_SYSTEM,
[SemanticAttributes.RPC_METHOD]: method,
[SemanticAttributes.RPC_SERVICE]: service,
[SEMATTRS_RPC_SYSTEM]: AttributeValues.RPC_SYSTEM,
[SEMATTRS_RPC_METHOD]: method,
[SEMATTRS_RPC_SERVICE]: service,
});

instrumentation._metadataCapture.server.captureRequestMetadata(
Expand Down Expand Up @@ -469,9 +474,9 @@ export class GrpcInstrumentation extends InstrumentationBase {
const span = instrumentation.tracer
.startSpan(name, { kind: SpanKind.CLIENT })
.setAttributes({
[SemanticAttributes.RPC_SYSTEM]: 'grpc',
[SemanticAttributes.RPC_METHOD]: method,
[SemanticAttributes.RPC_SERVICE]: service,
[SEMATTRS_RPC_SYSTEM]: 'grpc',
[SEMATTRS_RPC_METHOD]: method,
[SEMATTRS_RPC_SERVICE]: service,
});
instrumentation.extractNetMetadata(this, span);

Expand Down Expand Up @@ -514,9 +519,9 @@ export class GrpcInstrumentation extends InstrumentationBase {
const span = this.tracer
.startSpan(name, { kind: SpanKind.CLIENT })
.setAttributes({
[SemanticAttributes.RPC_SYSTEM]: 'grpc',
[SemanticAttributes.RPC_METHOD]: methodAttributeValue,
[SemanticAttributes.RPC_SERVICE]: service,
[SEMATTRS_RPC_SYSTEM]: 'grpc',
[SEMATTRS_RPC_METHOD]: methodAttributeValue,
[SEMATTRS_RPC_SERVICE]: service,
});

if (metadata != null) {
Expand All @@ -529,12 +534,9 @@ export class GrpcInstrumentation extends InstrumentationBase {
// set net.peer.* from target (e.g., "dns:otel-productcatalogservice:8080") as a hint to APMs
const parsedUri = URI_REGEX.exec(client.getChannel().getTarget());
if (parsedUri != null && parsedUri.groups != null) {
span.setAttribute(SEMATTRS_NET_PEER_NAME, parsedUri.groups['name']);
span.setAttribute(
SemanticAttributes.NET_PEER_NAME,
parsedUri.groups['name']
);
span.setAttribute(
SemanticAttributes.NET_PEER_PORT,
SEMATTRS_NET_PEER_PORT,
parseInt(parsedUri.groups['port'])
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import type {
import type { IgnoreMatcher } from './types';

import { context, SpanStatusCode } from '@opentelemetry/api';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import { SEMATTRS_RPC_GRPC_STATUS_CODE } from '@opentelemetry/semantic-conventions';

import {
_grpcStatusCodeToOpenTelemetryStatusCode,
Expand Down Expand Up @@ -81,10 +81,7 @@ function serverStreamAndBidiHandler<RequestType, ResponseType>(
span.setStatus({
code: SpanStatusCode.UNSET,
});
span.setAttribute(
SemanticAttributes.RPC_GRPC_STATUS_CODE,
GRPC_STATUS_CODE_OK
);
span.setAttribute(SEMATTRS_RPC_GRPC_STATUS_CODE, GRPC_STATUS_CODE_OK);

endSpan();
});
Expand All @@ -104,7 +101,7 @@ function serverStreamAndBidiHandler<RequestType, ResponseType>(
span.setAttributes({
[AttributeNames.GRPC_ERROR_NAME]: err.name,
[AttributeNames.GRPC_ERROR_MESSAGE]: err.message,
[SemanticAttributes.RPC_GRPC_STATUS_CODE]: err.code,
[SEMATTRS_RPC_GRPC_STATUS_CODE]: err.code,
});
endSpan();
});
Expand Down Expand Up @@ -134,18 +131,15 @@ function clientStreamAndUnaryHandler<RequestType, ResponseType>(
code: _grpcStatusCodeToOpenTelemetryStatusCode(err.code),
message: err.message,
});
span.setAttribute(SemanticAttributes.RPC_GRPC_STATUS_CODE, err.code);
span.setAttribute(SEMATTRS_RPC_GRPC_STATUS_CODE, err.code);
}
span.setAttributes({
[AttributeNames.GRPC_ERROR_NAME]: err.name,
[AttributeNames.GRPC_ERROR_MESSAGE]: err.message,
});
} else {
span.setStatus({ code: SpanStatusCode.UNSET });
span.setAttribute(
SemanticAttributes.RPC_GRPC_STATUS_CODE,
GRPC_STATUS_CODE_OK
);
span.setAttribute(SEMATTRS_RPC_GRPC_STATUS_CODE, GRPC_STATUS_CODE_OK);
}

span.end();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ import {
ReadableSpan,
} from '@opentelemetry/sdk-trace-base';
import { assertPropagation, assertSpan } from './utils/assertionUtils';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import {
SEMATTRS_RPC_METHOD,
SEMATTRS_RPC_SERVICE,
} from '@opentelemetry/semantic-conventions';

export type SpanAssertionFunction = (
exporter: InMemorySpanExporter,
Expand Down Expand Up @@ -60,14 +63,8 @@ function validateSpans(

assertSpan('grpc', serverSpan, SpanKind.SERVER, validations);
assertSpan('grpc', clientSpan, SpanKind.CLIENT, validations);
assert.strictEqual(
clientSpan.attributes[SemanticAttributes.RPC_METHOD],
rpcMethod
);
assert.strictEqual(
clientSpan.attributes[SemanticAttributes.RPC_SERVICE],
rpcService
);
assert.strictEqual(clientSpan.attributes[SEMATTRS_RPC_METHOD], rpcMethod);
assert.strictEqual(clientSpan.attributes[SEMATTRS_RPC_SERVICE], rpcService);
}

export function assertNoSpansExported(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ import {
hrTimeToMilliseconds,
hrTimeToMicroseconds,
} from '@opentelemetry/core';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import {
SEMATTRS_NET_PEER_NAME,
SEMATTRS_NET_PEER_PORT,
SEMATTRS_RPC_GRPC_STATUS_CODE,
} from '@opentelemetry/semantic-conventions';

export const grpcStatusCodeToOpenTelemetryStatusCode = (
status: GrpcStatus
Expand Down Expand Up @@ -66,11 +70,11 @@ export const assertSpan = (
validations.netPeerPort !== undefined
) {
assert.strictEqual(
span.attributes[SemanticAttributes.NET_PEER_NAME],
span.attributes[SEMATTRS_NET_PEER_NAME],
validations.netPeerName
);
assert.strictEqual(
span.attributes[SemanticAttributes.NET_PEER_PORT],
span.attributes[SEMATTRS_NET_PEER_PORT],
validations.netPeerPort
);
}
Expand All @@ -82,7 +86,7 @@ export const assertSpan = (
grpcStatusCodeToOpenTelemetryStatusCode(validations.status)
);
assert.strictEqual(
span.attributes[SemanticAttributes.RPC_GRPC_STATUS_CODE],
span.attributes[SEMATTRS_RPC_GRPC_STATUS_CODE],
validations.status
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"@types/semver": "7.5.6",
"@types/sinon": "10.0.20",
"@types/superagent": "4.1.24",
"axios": "1.5.1",
"axios": "1.6.0",
"codecov": "3.8.3",
"cross-var": "1.1.0",
"lerna": "6.6.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { InstrumentationModuleDefinition } from '../../types';
import { diag } from '@opentelemetry/api';
import type { OnRequireFn } from 'require-in-the-middle';
import { Hook } from 'require-in-the-middle';
import { readFileSync } from 'fs';

/**
* Base abstract class for instrumenting node plugins
Expand Down Expand Up @@ -160,8 +161,10 @@ export abstract class InstrumentationBase

private _extractPackageVersion(baseDir: string): string | undefined {
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const version = require(path.join(baseDir, 'package.json')).version;
const json = readFileSync(path.join(baseDir, 'package.json'), {
encoding: 'utf8',
});
const version = JSON.parse(json).version;
return typeof version === 'string' ? version : undefined;
} catch (error) {
diag.warn('Failed extracting version', baseDir);
Expand Down
2 changes: 1 addition & 1 deletion experimental/packages/opentelemetry-sdk-node/LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright 2020 The OpenTelemetry Authors
Copyright [yyyy] [name of copyright owner]

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
19 changes: 18 additions & 1 deletion experimental/packages/opentelemetry-sdk-node/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,26 @@ Configure a resource. Resources may also be detected by using the `autoDetectRes

### resourceDetectors

Configure resource detectors. By default, the resource detectors are [envDetector, processDetector].
Configure resource detectors. By default, the resource detectors are [envDetector, processDetector, hostDetector].
NOTE: In order to enable the detection, the parameter `autoDetectResources` has to be `true`.

If `resourceDetectors` was not set, you can also use the environment variable `OTEL_NODE_RESOURCE_DETECTORS` to enable only certain detectors, or completely disable them:

- `env`
- `host`
- `os`
- `process`
- `serviceinstance` (experimental)
- `all` - enable all resource detectors above
- **NOTE:** future versions of `@opentelemetry/sdk-node` may include additional detectors that will be covered by this scope.
- `none` - disable resource detection

For example, to enable only the `env`, `host` detectors:

```shell
export OTEL_NODE_RESOURCE_DETECTORS="env,host"
```

### sampler

Configure a custom sampler. By default, all traces will be sampled.
Expand Down
20 changes: 14 additions & 6 deletions experimental/packages/opentelemetry-sdk-node/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ import { SEMRESATTRS_SERVICE_NAME } from '@opentelemetry/semantic-conventions';
import { NodeSDKConfiguration } from './types';
import { TracerProviderWithEnvExporters } from './TracerProviderWithEnvExporter';
import { getEnv, getEnvWithoutDefaults } from '@opentelemetry/core';
import { parseInstrumentationOptions } from './utils';
import {
getResourceDetectorsFromEnv,
parseInstrumentationOptions,
} from './utils';

/** This class represents everything needed to register a fully configured OpenTelemetry Node.js SDK */

Expand Down Expand Up @@ -121,11 +124,15 @@ export class NodeSDK {
this._configuration = configuration;

this._resource = configuration.resource ?? new Resource({});
this._resourceDetectors = configuration.resourceDetectors ?? [
envDetector,
processDetector,
hostDetector,
];
let defaultDetectors: (Detector | DetectorSync)[] = [];
if (process.env.OTEL_NODE_RESOURCE_DETECTORS != null) {
defaultDetectors = getResourceDetectorsFromEnv();
} else {
defaultDetectors = [envDetector, processDetector, hostDetector];
}

this._resourceDetectors =
configuration.resourceDetectors ?? defaultDetectors;

this._serviceName = configuration.serviceName;

Expand Down Expand Up @@ -157,6 +164,7 @@ export class NodeSDK {

const spanProcessor =
configuration.spanProcessor ??
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
new BatchSpanProcessor(configuration.traceExporter!);

const spanProcessors = configuration.spanProcessors ?? [spanProcessor];
Expand Down
Loading

0 comments on commit 872853d

Please sign in to comment.