Skip to content

Commit

Permalink
Merge branch 'main' into fix/host-metrics-bundling
Browse files Browse the repository at this point in the history
  • Loading branch information
Netail authored May 24, 2024
2 parents a3f9a61 + 96eb7dc commit fc23083
Show file tree
Hide file tree
Showing 59 changed files with 281 additions and 206 deletions.
31 changes: 14 additions & 17 deletions incubator/opentelemetry-sampler-aws-xray/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ const { AWSXRayPropagator } = require("@opentelemetry/propagator-aws-xray");
const { AWSXRayIdGenerator } = require("@opentelemetry/id-generator-aws-xray");


// Initialize resource, trace exporter, span processor, and ID generator
const _resource = Resource.default().merge(new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: "remote-sampler-app",
}));
// Initialize trace exporter, span processor, and ID generator
const _traceExporter = new OTLPTraceExporter();
const _spanProcessor = new BatchSpanProcessor(_traceExporter);
const _tracerConfig = {
Expand All @@ -32,19 +29,19 @@ const _tracerConfig = {
}

const sdk = new opentelemetry.NodeSDK({
textMapPropagator: new AWSXRayPropagator(),
instrumentations: [
new HttpInstrumentation(),
new AwsInstrumentation({
suppressInternalInstrumentation: true
}),
],
resource: _resource,
spanProcessor: _spanProcessor,
traceExporter: _traceExporter,
});

sdk.configureTracerProvider(_tracerConfig, _spanProcessor);
serviceName: "remote-sampler-app",
textMapPropagator: new AWSXRayPropagator(),
instrumentations: [
new HttpInstrumentation(),
new AwsInstrumentation({
suppressInternalInstrumentation: true
}),
],
spanProcessor: _spanProcessor,
traceExporter: _traceExporter,
});

sdk.configureTracerProvider(_tracerConfig, _spanProcessor);

```

Expand Down
4 changes: 2 additions & 2 deletions metapackages/auto-instrumentations-node/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,14 @@ const { NodeTracerProvider } = require('@opentelemetry/sdk-trace-node');
const { getNodeAutoInstrumentations } = require('@opentelemetry/auto-instrumentations-node');
const { CollectorTraceExporter } = require('@opentelemetry/exporter-collector');
const { Resource } = require('@opentelemetry/resources');
const { SemanticResourceAttributes } = require('@opentelemetry/semantic-conventions');
const { SEMRESATTRS_SERVICE_NAME } = require('@opentelemetry/semantic-conventions');
const { SimpleSpanProcessor } = require('@opentelemetry/sdk-trace-base');
const { registerInstrumentations } = require('@opentelemetry/instrumentation');

const exporter = new CollectorTraceExporter();
const provider = new NodeTracerProvider({
resource: new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: 'basic-service',
[SEMRESATTRS_SERVICE_NAME]: 'basic-service',
}),
});
provider.addSpanProcessor(new SimpleSpanProcessor(exporter));
Expand Down
6 changes: 3 additions & 3 deletions packages/opentelemetry-host-metrics/src/BaseMetrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import { Meter, diag, metrics } from '@opentelemetry/api';
import { MeterProvider } from '@opentelemetry/sdk-metrics';

import { VERSION } from './version';
import { PACKAGE_NAME, PACKAGE_VERSION } from './version';

/**
* Metrics Collector Configuration
Expand All @@ -33,7 +33,7 @@ export interface MetricsCollectorConfig {
url?: string;
}

const DEFAULT_NAME = '@opentelemetry/host-metrics';
const DEFAULT_NAME = PACKAGE_NAME;

/**
* Base Class for metrics
Expand All @@ -49,7 +49,7 @@ export abstract class BaseMetrics {
if (!config.meterProvider) {
this._logger.warn('No meter provider, using default');
}
this._meter = meterProvider.getMeter(this._name, VERSION);
this._meter = meterProvider.getMeter(this._name, PACKAGE_VERSION);
}

/**
Expand Down
42 changes: 21 additions & 21 deletions packages/opentelemetry-test-utils/src/resource-assertions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import { SDK_INFO } from '@opentelemetry/core';
import * as assert from 'assert';
import { Resource } from '@opentelemetry/resources';
import {
SemanticResourceAttributes,
SEMRESATTRS_CLOUD_ACCOUNT_ID,
SEMRESATTRS_CLOUD_AVAILABILITY_ZONE,
SEMRESATTRS_CLOUD_PROVIDER,
Expand Down Expand Up @@ -49,6 +48,7 @@ import {
SEMRESATTRS_TELEMETRY_SDK_NAME,
SEMRESATTRS_TELEMETRY_SDK_VERSION,
} from '@opentelemetry/semantic-conventions';
import * as semconv from '@opentelemetry/semantic-conventions';

/**
* Test utility method to validate a cloud resource
Expand All @@ -65,7 +65,7 @@ export const assertCloudResource = (
zone?: string;
}
) => {
assertHasOneLabel('CLOUD', resource);
assertHasOneLabel('cloud', resource);
if (validations.provider)
assert.strictEqual(
resource.attributes[SEMRESATTRS_CLOUD_PROVIDER],
Expand Down Expand Up @@ -103,7 +103,7 @@ export const assertContainerResource = (
imageTag?: string;
}
) => {
assertHasOneLabel('CONTAINER', resource);
assertHasOneLabel('container', resource);
if (validations.name)
assert.strictEqual(
resource.attributes[SEMRESATTRS_CONTAINER_NAME],
Expand Down Expand Up @@ -143,7 +143,7 @@ export const assertHostResource = (
imageVersion?: string;
}
) => {
assertHasOneLabel('HOST', resource);
assertHasOneLabel('host', resource);
if (validations.id)
assert.strictEqual(
resource.attributes[SEMRESATTRS_HOST_ID],
Expand Down Expand Up @@ -191,7 +191,7 @@ export const assertK8sResource = (
deploymentName?: string;
}
) => {
assertHasOneLabel('K8S', resource);
assertHasOneLabel('k8s', resource);
if (validations.clusterName)
assert.strictEqual(
resource.attributes[SEMRESATTRS_K8S_CLUSTER_NAME],
Expand Down Expand Up @@ -335,26 +335,26 @@ export const assertEmptyResource = (resource: Resource) => {
assert.strictEqual(Object.keys(resource.attributes).length, 0);
};

/**
* Assert that the `resource` has at least one known attribute with the given
* `prefix`. By "known", we mean it is an attribute defined in semconv.
*/
const assertHasOneLabel = (prefix: string, resource: Resource): void => {
const hasOne = Object.entries(SemanticResourceAttributes).find(
([key, value]) => {
return (
key.startsWith(prefix) &&
Object.prototype.hasOwnProperty.call(resource.attributes, value)
);
}
const semconvModPrefix = `SEMRESATTRS_${prefix.toUpperCase()}_`;
const knownAttrs: Set<string> = new Set(
Object.entries(semconv)
.filter(
([k, v]) => typeof v === 'string' && k.startsWith(semconvModPrefix)
)
.map(([, v]) => v as string)
);

const hasAttrs = Object.keys(resource.attributes).filter(k =>
knownAttrs.has(k)
);
assert.ok(
hasOne,
hasAttrs.length > 0,
'Resource must have one of the following attributes: ' +
Object.entries(SemanticResourceAttributes)
.reduce((result, [key, value]) => {
if (key.startsWith(prefix)) {
result.push(value);
}
return result;
})
.join(', ')
Array.from(knownAttrs).join(', ')
);
};
4 changes: 2 additions & 2 deletions packages/winston-transport/src/OpenTelemetryTransportV3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@

import { Logger, logs } from '@opentelemetry/api-logs';
import TransportStream = require('winston-transport');
import { VERSION } from './version';
import { PACKAGE_NAME, PACKAGE_VERSION } from './version';
import { emitLogRecord } from './utils';

export class OpenTelemetryTransportV3 extends TransportStream {
private _logger: Logger;

constructor(options?: TransportStream.TransportStreamOptions) {
super(options);
this._logger = logs.getLogger('@opentelemetry/winston-transport', VERSION);
this._logger = logs.getLogger(PACKAGE_NAME, PACKAGE_VERSION);
}

public override log(info: any, callback: () => void) {
Expand Down
6 changes: 3 additions & 3 deletions plugins/node/instrumentation-amqplib/src/amqplib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,15 @@ import {
normalizeExchange,
unmarkConfirmChannelTracing,
} from './utils';
import { VERSION } from './version';
import { PACKAGE_NAME, PACKAGE_VERSION } from './version';

export class AmqplibInstrumentation extends InstrumentationBase {
protected override _config!: AmqplibInstrumentationConfig;

constructor(config: AmqplibInstrumentationConfig = {}) {
super(
'@opentelemetry/instrumentation-amqplib',
VERSION,
PACKAGE_NAME,
PACKAGE_VERSION,
Object.assign({}, DEFAULT_CONFIG, config)
);
}
Expand Down
4 changes: 2 additions & 2 deletions plugins/node/instrumentation-cucumber/src/instrumentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import type {
} from '@cucumber/cucumber/lib/support_code_library_builder/types';

import { AttributeNames, CucumberInstrumentationConfig } from './types';
import { VERSION } from './version';
import { PACKAGE_NAME, PACKAGE_VERSION } from './version';

const hooks = ['Before', 'BeforeStep', 'AfterStep', 'After'] as const;
const steps = ['Given', 'When', 'Then'] as const;
Expand All @@ -50,7 +50,7 @@ export class CucumberInstrumentation extends InstrumentationBase {
private module: Cucumber | undefined;

constructor(config: CucumberInstrumentationConfig = {}) {
super('@opentelemetry/instrumentation-cucumber', VERSION, config);
super(PACKAGE_NAME, PACKAGE_VERSION, config);
}

init(): InstrumentationNodeModuleDefinition[] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
SpanKind,
} from '@opentelemetry/api';
import { DataloaderInstrumentationConfig } from './types';
import { VERSION } from './version';
import { PACKAGE_NAME, PACKAGE_VERSION } from './version';
import type * as Dataloader from 'dataloader';

const MODULE_NAME = 'dataloader';
Expand All @@ -45,7 +45,7 @@ type LoadManyFn = (typeof Dataloader.prototype)['loadMany'];

export class DataloaderInstrumentation extends InstrumentationBase {
constructor(config: DataloaderInstrumentationConfig = {}) {
super('@opentelemetry/instrumentation-dataloader', VERSION, config);
super(PACKAGE_NAME, PACKAGE_VERSION, config);
}

protected init() {
Expand Down
4 changes: 2 additions & 2 deletions plugins/node/instrumentation-fs/src/instrumentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
InstrumentationNodeModuleDefinition,
isWrapped,
} from '@opentelemetry/instrumentation';
import { VERSION } from './version';
import { PACKAGE_NAME, PACKAGE_VERSION } from './version';
import {
CALLBACK_FUNCTIONS,
PROMISE_FUNCTIONS,
Expand Down Expand Up @@ -53,7 +53,7 @@ function patchedFunctionWithOriginalProperties<

export default class FsInstrumentation extends InstrumentationBase {
constructor(config: FsInstrumentationConfig = {}) {
super('@opentelemetry/instrumentation-fs', VERSION, config);
super(PACKAGE_NAME, PACKAGE_VERSION, config);
}

init(): (
Expand Down
4 changes: 2 additions & 2 deletions plugins/node/instrumentation-kafkajs/src/instrumentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import type {
Consumer,
} from 'kafkajs';
import { KafkaJsInstrumentationConfig } from './types';
import { VERSION } from './version';
import { PACKAGE_NAME, PACKAGE_VERSION } from './version';
import { bufferTextMapGetter } from './propagator';
import {
InstrumentationBase,
Expand All @@ -57,7 +57,7 @@ export class KafkaJsInstrumentation extends InstrumentationBase {
protected override _config!: KafkaJsInstrumentationConfig;

constructor(config: KafkaJsInstrumentationConfig = {}) {
super('@opentelemetry/instrumentation-kafkajs', VERSION, config);
super(PACKAGE_NAME, PACKAGE_VERSION, config);
}

protected init() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ import {
InstrumentationConfig,
InstrumentationNodeModuleDefinition,
} from '@opentelemetry/instrumentation';
import { VERSION } from './version';
import { PACKAGE_NAME, PACKAGE_VERSION } from './version';

export default class LruMemoizerInstrumentation extends InstrumentationBase {
constructor(config: InstrumentationConfig = {}) {
super('@opentelemetry/instrumentation-lru-memoizer', VERSION, config);
super(PACKAGE_NAME, PACKAGE_VERSION, config);
}

init(): InstrumentationNodeModuleDefinition[] {
Expand Down
5 changes: 3 additions & 2 deletions plugins/node/instrumentation-mongoose/src/mongoose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
InstrumentationModuleDefinition,
InstrumentationNodeModuleDefinition,
} from '@opentelemetry/instrumentation';
import { VERSION } from './version';
import { PACKAGE_NAME, PACKAGE_VERSION } from './version';
import {
SEMATTRS_DB_OPERATION,
SEMATTRS_DB_STATEMENT,
Expand Down Expand Up @@ -61,7 +61,7 @@ export class MongooseInstrumentation extends InstrumentationBase {
protected override _config!: MongooseInstrumentationConfig;

constructor(config: MongooseInstrumentationConfig = {}) {
super('@opentelemetry/instrumentation-mongoose', VERSION, config);
super(PACKAGE_NAME, PACKAGE_VERSION, config);
}

override setConfig(config: MongooseInstrumentationConfig = {}) {
Expand Down Expand Up @@ -330,6 +330,7 @@ export class MongooseInstrumentation extends InstrumentationBase {
exec,
originalThis,
span,
args,
self._config.responseHook,
moduleVersion
)
Expand Down
25 changes: 16 additions & 9 deletions plugins/node/instrumentation-mongoose/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,23 @@ export function handleCallbackResponse(
exec: Function,
originalThis: any,
span: Span,
args: IArguments,
responseHook?: MongooseResponseCustomAttributesFunction,
moduleVersion: string | undefined = undefined
) {
return exec.apply(originalThis, [
(err: Error, response: any) => {
err
? setErrorStatus(span, err)
: applyResponseHook(span, response, responseHook, moduleVersion);
span.end();
return callback!(err, response);
},
]);
let callbackArgumentIndex = 0;
if (args.length === 2) {
callbackArgumentIndex = 1;
}

args[callbackArgumentIndex] = (err: Error, response: any): any => {
err
? setErrorStatus(span, err)
: applyResponseHook(span, response, responseHook, moduleVersion);

span.end();
return callback!(err, response);
};

return exec.apply(originalThis, args);
}
Loading

0 comments on commit fc23083

Please sign in to comment.