Skip to content

Commit

Permalink
Merge branch 'main' into expohisto-p2-accumulation
Browse files Browse the repository at this point in the history
  • Loading branch information
mwear authored Mar 3, 2023
2 parents 7f917b0 + 2f715bd commit b6f1cb1
Show file tree
Hide file tree
Showing 21 changed files with 214 additions and 21 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ For experimental package changes, see the [experimental CHANGELOG](experimental/
### :bug: (Bug Fix)

* fix(core): added falsy check to make otel core work with browser where webpack config had process as false or null [#3613](https://github.com/open-telemetry/opentelemetry-js/issues/3613) @ravindra-dyte
* fix(instrumentation-http): include query params in http.target [#3646](https://github.com/open-telemetry/opentelemetry-js/pull/3646) @kobi-co

### :books: (Refine Doc)

* chore: update http example [#3651](https://github.com/open-telemetry/opentelemetry-js/pull/3651) @JamieDanielson

### :house: (Internal)

* fix(sdk-metrics): fix flaky LastValueAggregator test by using fake timer [#3587](https://github.com/open-telemetry/opentelemetry-js/pull/3587) @pichlermarc
Expand Down
2 changes: 2 additions & 0 deletions api/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ All notable changes to this project will be documented in this file.
### :bug: (Bug Fix)

* fix(metrics): export `MetricsAPI` type [#3535](https://github.com/open-telemetry/opentelemetry-js/pull/3535)
* fix(api): rename `LoggerOptions` to `DiagLoggerOptions` [#3641](https://github.com/open-telemetry/opentelemetry-js/pull/3641)
* fix(api): export `DiagLoggerOptions` type [#3639](https://github.com/open-telemetry/opentelemetry-js/pull/3639)

## 1.4.0

Expand Down
6 changes: 3 additions & 3 deletions api/src/diag/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export interface ComponentLoggerOptions {
namespace: string;
}

export interface LoggerOptions {
export interface DiagLoggerOptions {
/**
* The {@link DiagLogLevel} used to filter logs sent to the logger.
*
Expand All @@ -117,10 +117,10 @@ export interface DiagLoggerApi {
* If a global diag logger is already set, this will override it.
*
* @param logger - The {@link DiagLogger} instance to set as the default logger.
* @param options - A {@link LoggerOptions} object. If not provided, default values will be set.
* @param options - A {@link DiagLoggerOptions} object. If not provided, default values will be set.
* @returns `true` if the logger was successfully registered, else `false`
*/
setLogger(logger: DiagLogger, options?: LoggerOptions): boolean;
setLogger(logger: DiagLogger, options?: DiagLoggerOptions): boolean;

/**
*
Expand Down
1 change: 1 addition & 0 deletions api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export {
DiagLogger,
DiagLogLevel,
ComponentLoggerOptions,
DiagLoggerOptions,
} from './diag/types';
export type { DiagAPI } from './api/diag';

Expand Down
3 changes: 1 addition & 2 deletions examples/http/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ function makeRequest() {
// span corresponds to outgoing requests. Here, we have manually created
// the span, which is created to track work that happens outside of the
// request lifecycle entirely.
const span = tracer.startSpan('makeRequest');
api.context.with(api.trace.setSpan(api.context.active(), span), () => {
tracer.startActiveSpan('makeRequest', (span) => {
http.get({
host: 'localhost',
port: 8080,
Expand Down
20 changes: 10 additions & 10 deletions examples/http/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "http-example",
"private": true,
"version": "0.25.0",
"version": "0.35.1",
"description": "Example of HTTP integration with OpenTelemetry",
"main": "index.js",
"scripts": {
Expand All @@ -28,15 +28,15 @@
"url": "https://github.com/open-telemetry/opentelemetry-js/issues"
},
"dependencies": {
"@opentelemetry/api": "^1.0.2",
"@opentelemetry/exporter-jaeger": "0.25.0",
"@opentelemetry/exporter-zipkin": "0.25.0",
"@opentelemetry/instrumentation": "0.25.0",
"@opentelemetry/instrumentation-http": "0.25.0",
"@opentelemetry/resources": "0.25.0",
"@opentelemetry/semantic-conventions": "0.25.0",
"@opentelemetry/sdk-trace-node": "0.25.0",
"@opentelemetry/sdk-trace-base": "0.25.0"
"@opentelemetry/api": "^1.3.0",
"@opentelemetry/exporter-jaeger": "1.9.1",
"@opentelemetry/exporter-zipkin": "1.9.1",
"@opentelemetry/instrumentation": "0.35.1",
"@opentelemetry/instrumentation-http": "0.35.1",
"@opentelemetry/resources": "1.9.1",
"@opentelemetry/semantic-conventions": "1.9.1",
"@opentelemetry/sdk-trace-node": "1.9.1",
"@opentelemetry/sdk-trace-base": "1.9.1"
},
"homepage": "https://github.com/open-telemetry/opentelemetry-js/tree/main/examples/http",
"devDependencies": {
Expand Down
5 changes: 3 additions & 2 deletions examples/http/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ function startServer(port) {

/** A function which handles requests and send response. */
function handleRequest(request, response) {
const currentSpan = api.trace.getSpan(api.context.active());
const currentSpan = api.trace.getActiveSpan();
// display traceid in the terminal
console.log(`traceid: ${currentSpan.spanContext().traceId}`);
const traceId = currentSpan.spanContext().traceId;
console.log(`traceId: ${traceId}`);
const span = tracer.startSpan('handleRequest', {
kind: 1, // server
attributes: { key: 'value' },
Expand Down
5 changes: 4 additions & 1 deletion experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@ All notable changes to experimental packages in this project will be documented

* feat: use HTTP_ROUTE in span name [#3603](https://github.com/open-telemetry/opentelemetry-js/pull/3603) @Flarna
* feat: add HTTP_ROUTE attribute to http incoming metrics if present [#3581](https://github.com/open-telemetry/opentelemetry-js/pull/3581) @hermogenes
* feat(opentelemetry-instrumentation-grpc): allow to add attributes from grpc metadata in the patched server [#3589](https://github.com/open-telemetry/opentelemetry-js/pull/3589) @zombispormedio
* feat(sdk-node): install diag logger with OTEL_LOG_LEVEL [#3627](https://github.com/open-telemetry/opentelemetry-js/pull/3627) @legendecas
* feat(otlp-exporter-base): add retries [#3207](https://github.com/open-telemetry/opentelemetry-js/pull/3207) @svetlanabrennan
* feat(sdk-node): override IdGenerator when using NodeSDK [#3645](https://github.com/open-telemetry/opentelemetry-js/pull/3645) @haddasbronfman

### :bug: (Bug Fix)

* fix(prometheus-exporter): add possibility to respond to errors returned by `server.listen()` [#3552](https://github.com/open-telemetry/opentelemetry-js/pull/3402) @pichlermarc
fix(sdk-node): update instrumentations once MeterProvider is initialized [#3624](https://github.com/open-telemetry/opentelemetry-js/pull/3624) @pichlermarc

### :books: (Refine Doc)

Expand Down Expand Up @@ -75,7 +79,6 @@ All notable changes to experimental packages in this project will be documented
* deps: remove unused proto-loader dependencies and update grpc-js and proto-loader versions [#3337](https://github.com/open-telemetry/opentelemetry-js/pull/3337) @seemk
* feat(metrics-exporters): configure temporality via environment variable [#3305](https://github.com/open-telemetry/opentelemetry-js/pull/3305) @pichlermarc
* feat(console-metric-exporter): add temporality configuration [#3387](https://github.com/open-telemetry/opentelemetry-js/pull/3387) @pichlermarc
* feat(otlp-exporter-base): add retries [#3207](https://github.com/open-telemetry/opentelemetry-js/pull/3207) @svetlanabrennan

### :bug: (Bug Fix)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ gRPC instrumentation accepts the following configuration:
| Options | Type | Description |
|----------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [`ignoreGrpcMethods`](https://github.com/open-telemetry/opentelemetry-js/blob/main/experimental/packages/opentelemetry-instrumentation-grpc/src/types.ts#L25) | `IgnoreMatcher[]` | gRPC instrumentation will not trace any methods that match anything in this list. You may pass a string (case-insensitive match), a `RegExp` object, or a filter function. |
| [`metadataToSpanAttributes`](https://github.com/open-telemetry/opentelemetry-js/blob/main/experimental/packages/opentelemetry-instrumentation-grpc/src/types.ts#L27) | `object` | List of case insensitive metadata to convert to span attributes. Client (outgoing requests, incoming responses) metadata attributes will be converted to span attributes in the form of `rpc.{request\response}.metadata.metadata_key`, e.g. `rpc.response.metadata.date` |
| [`metadataToSpanAttributes`](https://github.com/open-telemetry/opentelemetry-js/blob/main/experimental/packages/opentelemetry-instrumentation-grpc/src/types.ts#L27) | `object` | List of case insensitive metadata to convert to span attributes. Client and server (outgoing requests, incoming responses) metadata attributes will be converted to span attributes in the form of `rpc.{request\response}.metadata.metadata_key`, e.g. `rpc.response.metadata.date` |

## Useful links

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,24 @@ export class GrpcJsInstrumentation extends InstrumentationBase {
[SemanticAttributes.RPC_SERVICE]: service,
});

instrumentation._metadataCapture.server.captureRequestMetadata(
span,
call.metadata
);

instrumentation._wrap(
call,
'sendMetadata',
originalSendMetadata =>
(responseMetadata: grpcJs.Metadata) => {
instrumentation._metadataCapture.server.captureResponseMetadata(
span,
responseMetadata
);
originalSendMetadata.call(call, responseMetadata);
}
);

context.with(trace.setSpan(context.active(), span), () => {
handleServerFunction.call(
self,
Expand Down Expand Up @@ -385,6 +403,16 @@ export class GrpcJsInstrumentation extends InstrumentationBase {
config.metadataToSpanAttributes?.client?.responseMetadata ?? []
),
},
server: {
captureRequestMetadata: metadataCapture(
'request',
config.metadataToSpanAttributes?.server?.requestMetadata ?? []
),
captureResponseMetadata: metadataCapture(
'response',
config.metadataToSpanAttributes?.server?.responseMetadata ?? []
),
},
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,24 @@ export class GrpcNativeInstrumentation extends InstrumentationBase<
[SemanticAttributes.RPC_SERVICE]: service,
});

instrumentation._metadataCapture.server.captureRequestMetadata(
span,
call.metadata
);

instrumentation._wrap(
call as any,
'sendMetadata',
originalSendMetadata =>
(responseMetadata: grpcTypes.Metadata) => {
instrumentation._metadataCapture.server.captureResponseMetadata(
span,
responseMetadata
);
originalSendMetadata.call(call, responseMetadata);
}
);

context.with(trace.setSpan(context.active(), span), () => {
switch (type) {
case 'unary':
Expand Down Expand Up @@ -370,6 +388,16 @@ export class GrpcNativeInstrumentation extends InstrumentationBase<
config.metadataToSpanAttributes?.client?.responseMetadata ?? []
),
},
server: {
captureRequestMetadata: metadataCapture(
'request',
config.metadataToSpanAttributes?.server?.requestMetadata ?? []
),
captureResponseMetadata: metadataCapture(
'response',
config.metadataToSpanAttributes?.server?.responseMetadata ?? []
),
},
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,14 @@ export type metadataCaptureType = {
metadata: grpcJsTypes.Metadata | grpcTypes.Metadata
) => void;
};
server: {
captureRequestMetadata: (
span: Span,
metadata: grpcJsTypes.Metadata | grpcTypes.Metadata
) => void;
captureResponseMetadata: (
span: Span,
metadata: grpcJsTypes.Metadata | grpcTypes.Metadata
) => void;
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,9 @@ export interface GrpcInstrumentationConfig extends InstrumentationConfig {
responseMetadata?: string[];
requestMetadata?: string[];
};
server?: {
responseMetadata?: string[];
requestMetadata?: string[];
};
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -978,6 +978,10 @@ export const runTests = (
requestMetadata: ['client_metadata_key'],
responseMetadata: ['server_metadata_key'],
},
server: {
requestMetadata: ['client_metadata_key'],
responseMetadata: ['server_metadata_key'],
},
},
});

Expand All @@ -999,13 +1003,18 @@ export const runTests = (
});
});

describe('Capture request/response metadata in client span', () => {
describe('Capture request/response metadata in client and server spans', () => {
const attributeValidation = {
clientAttributes: {
'rpc.request.metadata.client_metadata_key': 'client_metadata_value',
'rpc.response.metadata.server_metadata_key':
'server_metadata_value',
},
serverAttributes: {
'rpc.request.metadata.client_metadata_key': 'client_metadata_value',
'rpc.response.metadata.server_metadata_key':
'server_metadata_value',
},
};

runTestWithAttributeValidation(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ export const getIncomingRequestAttributes = (
}

if (requestUrl) {
attributes[SemanticAttributes.HTTP_TARGET] = requestUrl.pathname || '/';
attributes[SemanticAttributes.HTTP_TARGET] = requestUrl.path || '/';
}

if (userAgent !== undefined) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,23 @@ describe('Utility', () => {
});
assert.strictEqual(attributes[SemanticAttributes.HTTP_ROUTE], undefined);
});

it('should set http.target as path in http span attributes', () => {
const request = {
url: 'http://hostname/user/?q=val',
method: 'GET',
} as IncomingMessage;
request.headers = {
'user-agent': 'chrome',
};
const attributes = utils.getIncomingRequestAttributes(request, {
component: 'http',
});
assert.strictEqual(
attributes[SemanticAttributes.HTTP_TARGET],
'/user/?q=val'
);
});
});

describe('headers to span attributes capture', () => {
Expand Down
14 changes: 14 additions & 0 deletions experimental/packages/opentelemetry-sdk-node/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions'
import { NodeSDKConfiguration } from './types';
import { TracerProviderWithEnvExporters } from './TracerProviderWithEnvExporter';
import { getEnv } from '@opentelemetry/core';
import { parseInstrumentationOptions } from './utils';

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

Expand All @@ -61,6 +62,7 @@ export type MeterProviderConfig = {
*/
views?: View[];
};

export class NodeSDK {
private _tracerProviderConfig?: {
tracerConfig: NodeTracerConfig;
Expand Down Expand Up @@ -117,6 +119,9 @@ export class NodeSDK {
if (configuration.spanLimits) {
tracerProviderConfig.spanLimits = configuration.spanLimits;
}
if (configuration.idGenerator) {
tracerProviderConfig.idGenerator = configuration.idGenerator;
}

const spanProcessor =
configuration.spanProcessor ??
Expand Down Expand Up @@ -272,6 +277,15 @@ export class NodeSDK {
this._meterProvider = meterProvider;

metrics.setGlobalMeterProvider(meterProvider);

// TODO: This is a workaround to fix https://github.com/open-telemetry/opentelemetry-js/issues/3609
// If the MeterProvider is not yet registered when instrumentations are registered, all metrics are dropped.
// This code is obsolete once https://github.com/open-telemetry/opentelemetry-js/issues/3622 is implemented.
for (const instrumentation of parseInstrumentationOptions(
this._instrumentations
)) {
instrumentation.setMeterProvider(metrics.getMeterProvider());
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions experimental/packages/opentelemetry-sdk-node/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
SpanExporter,
SpanLimits,
SpanProcessor,
IdGenerator,
} from '@opentelemetry/sdk-trace-base';

export interface NodeSDKConfiguration {
Expand All @@ -41,4 +42,5 @@ export interface NodeSDKConfiguration {
spanProcessor: SpanProcessor;
traceExporter: SpanExporter;
spanLimits: SpanLimits;
idGenerator: IdGenerator;
}
Loading

0 comments on commit b6f1cb1

Please sign in to comment.