Skip to content

Commit

Permalink
Merge branch 'main' into fix-2389-LoggerUnformattedStrings
Browse files Browse the repository at this point in the history
  • Loading branch information
PaurushGarg authored Sep 29, 2021
2 parents 5f4146e + b66c650 commit 7b134c6
Show file tree
Hide file tree
Showing 133 changed files with 356 additions and 144 deletions.
8 changes: 4 additions & 4 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[submodule "packages/opentelemetry-exporter-otlp-grpc/protos"]
path = packages/opentelemetry-exporter-otlp-grpc/protos
[submodule "experimental/packages/opentelemetry-exporter-otlp-proto/protos"]
path = experimental/packages/opentelemetry-exporter-otlp-proto/protos
url = https://github.com/open-telemetry/opentelemetry-proto.git
[submodule "packages/opentelemetry-exporter-otlp-proto/protos"]
path = packages/opentelemetry-exporter-otlp-proto/protos
[submodule "experimental/packages/opentelemetry-exporter-otlp-grpc/protos"]
path = experimental/packages/opentelemetry-exporter-otlp-grpc/protos
url = https://github.com/open-telemetry/opentelemetry-proto.git
4 changes: 2 additions & 2 deletions examples/otlp-exporter-node/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Overview

This example shows how to use [@opentelemetry/exporter-otlp-http](https://github.com/open-telemetry/opentelemetry-js/tree/main/packages/opentelemetry-exporter-otlp-http) to instrument a simple Node.js application.
This example shows how to use [@opentelemetry/exporter-otlp-http](https://github.com/open-telemetry/opentelemetry-js/tree/main/experimental/packages/opentelemetry-exporter-otlp-http) to instrument a simple Node.js application.

This example will export spans data simultaneously using [Exporter Collector](https://github.com/open-telemetry/opentelemetry-js/tree/main/packages/opentelemetry-exporter-otlp-http) and grpc. It will use [proto format](https://github.com/open-telemetry/opentelemetry-proto).
This example will export spans data simultaneously using [Exporter Collector](https://github.com/open-telemetry/opentelemetry-js/tree/main/experimental/packages/opentelemetry-exporter-otlp-http) and grpc. It will use [proto format](https://github.com/open-telemetry/opentelemetry-proto).

## Installation

Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": "../../tsconfig.es5.json",
"extends": "../../../tsconfig.es5.json",
"compilerOptions": {
"rootDir": ".",
"outDir": "build"
Expand All @@ -9,7 +9,7 @@
],
"references": [
{
"path": "../../packages/opentelemetry-sdk-trace-base"
"path": "../../packages/opentelemetry-sdk-node"
}
]
}
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": "../../tsconfig.es5.json",
"extends": "../../../tsconfig.es5.json",
"compilerOptions": {
"rootDir": ".",
"outDir": "build"
Expand All @@ -9,7 +9,7 @@
],
"references": [
{
"path": "../../packages/opentelemetry-sdk-trace-base"
"path": "../../packages/opentelemetry-sdk-node"
}
]
}
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": "../../tsconfig.es5.json",
"extends": "../../../tsconfig.es5.json",
"compilerOptions": {
"rootDir": ".",
"outDir": "build"
Expand All @@ -9,7 +9,7 @@
],
"references": [
{
"path": "../../packages/opentelemetry-sdk-trace-base"
"path": "../../packages/opentelemetry-sdk-node"
}
]
}
3 changes: 2 additions & 1 deletion experimental/lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "0.25.0",
"npmClient": "npm",
"packages": [
"packages/*"
"packages/*",
"backwards-compatability/*"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ module.exports = {
"commonjs": true,
"node": true,
},
...require('../../eslint.config.js')
...require('../../../eslint.config.js')
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"submodule": "git submodule sync --recursive && git submodule update --init --recursive",
"tdd": "npm run test -- --watch-extensions ts --watch",
"test": "nyc ts-mocha -p tsconfig.json 'test/**/*.test.ts'",
"version": "node ../../scripts/version-update.js",
"version": "node ../../../scripts/version-update.js",
"watch": "npm run protos:copy && tsc -w",
"precompile": "lerna run version --scope $(npm pkg get name) --include-filtered-dependencies",
"prewatch": "npm run precompile"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": "../../tsconfig.base.json",
"extends": "../../../tsconfig.base.json",
"compilerOptions": {
"rootDir": ".",
"outDir": "build"
Expand All @@ -10,16 +10,13 @@
],
"references": [
{
"path": "../opentelemetry-core"
"path": "../opentelemetry-api-metrics"
},
{
"path": "../opentelemetry-exporter-otlp-http"
},
{
"path": "../opentelemetry-resources"
},
{
"path": "../opentelemetry-sdk-trace-base"
"path": "../opentelemetry-sdk-metrics-base"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ module.exports = {
"node": true,
"browser": true
},
...require('../../eslint.config.js')
...require('../../../eslint.config.js')
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
* limitations under the License.
*/

const karmaWebpackConfig = require('../../karma.webpack');
const karmaBaseConfig = require('../../karma.base');
const karmaWebpackConfig = require('../../../karma.webpack');
const karmaBaseConfig = require('../../../karma.base');

module.exports = (config) => {
config.set(Object.assign({}, karmaBaseConfig, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"tdd:browser": "karma start",
"test": "nyc ts-mocha -p tsconfig.json 'test/**/*.test.ts' --exclude 'test/browser/**/*.ts'",
"test:browser": "nyc karma start --single-run",
"version": "node ../../scripts/version-update.js",
"version": "node ../../../scripts/version-update.js",
"watch": "tsc --build --watch tsconfig.json tsconfig.esm.json",
"precompile": "lerna run version --scope $(npm pkg get name) --include-filtered-dependencies",
"prewatch": "npm run precompile"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"extends": "../../../tsconfig.base.esm.json",
"compilerOptions": {
"rootDir": "src",
"outDir": "build/esm",
"tsBuildInfoFile": "build/esm/tsconfig.esm.tsbuildinfo"
},
"include": [
"src/**/*.ts"
],
"references": [
{
"path": "../opentelemetry-api-metrics/tsconfig.esm.json"
},
{
"path": "../opentelemetry-sdk-metrics-base/tsconfig.esm.json"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"extends": "../../../tsconfig.base.json",
"compilerOptions": {
"rootDir": ".",
"outDir": "build"
},
"include": [
"src/**/*.ts",
"test/**/*.ts"
],
"references": [
{
"path": "../opentelemetry-api-metrics"
},
{
"path": "../opentelemetry-sdk-metrics-base"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ module.exports = {
"commonjs": true,
"node": true,
},
...require('../../eslint.config.js')
...require('../../../eslint.config.js')
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"submodule": "git submodule sync --recursive && git submodule update --init --recursive",
"tdd": "npm run test -- --watch-extensions ts --watch",
"test": "nyc ts-mocha -p tsconfig.json 'test/**/*.test.ts'",
"version": "node ../../scripts/version-update.js",
"version": "node ../../../scripts/version-update.js",
"watch": "npm run protos:copy && tsc -w",
"precompile": "lerna run version --scope $(npm pkg get name) --include-filtered-dependencies",
"prewatch": "npm run precompile"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": "../../tsconfig.base.json",
"extends": "../../../tsconfig.base.json",
"compilerOptions": {
"rootDir": ".",
"outDir": "build"
Expand All @@ -10,16 +10,13 @@
],
"references": [
{
"path": "../opentelemetry-core"
"path": "../opentelemetry-api-metrics"
},
{
"path": "../opentelemetry-exporter-otlp-http"
},
{
"path": "../opentelemetry-resources"
},
{
"path": "../opentelemetry-sdk-trace-base"
"path": "../opentelemetry-sdk-metrics-base"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
"references": [
{
"path": "../opentelemetry-api-metrics"
},
{
"path": "../opentelemetry-sdk-metrics-base"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ Http instrumentation has few options available to choose from. You can set the f
| [`serverName`](https://github.com/open-telemetry/opentelemetry-js/blob/main/packages/opentelemetry-instrumentation-http/src/types.ts#L101) | `string` | The primary server name of the matched virtual host. |
| [`requireParentforOutgoingSpans`](https://github.com/open-telemetry/opentelemetry-js/blob/main/packages/opentelemetry-instrumentation-http/src/types.ts#L103) | Boolean | Require that is a parent span to create new span for outgoing requests. |
| [`requireParentforIncomingSpans`](https://github.com/open-telemetry/opentelemetry-js/blob/main/packages/opentelemetry-instrumentation-http/src/types.ts#L105) | Boolean | Require that is a parent span to create new span for incoming requests. |
| [`headersToSpanAttributes`](https://github.com/open-telemetry/opentelemetry-js/blob/main/packages/opentelemetry-instrumentation-http/src/types.ts#L107) | `object` | List of case insensitive HTTP headers to convert to span attributes. Client (outgoing requests, incoming responses) and server (incoming requests, outgoing responses) headers will be converted to span attributes in the form of `http.{request\|response}.header.header_name`, e.g. `http.response.header.content_length` |

## Useful links

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,16 @@ export class HttpInstrumentation extends InstrumentationBase<Http> {
/** keep track on spans not ended */
private readonly _spanNotEnded: WeakSet<Span> = new WeakSet<Span>();
private readonly _version = process.versions.node;
private _headerCapture;

constructor(config: HttpInstrumentationConfig & InstrumentationConfig = {}) {
super(
'@opentelemetry/instrumentation-http',
VERSION,
Object.assign({}, config)
);

this._headerCapture = this._createHeaderCapture();
}

private _getConfig(): HttpInstrumentationConfig {
Expand All @@ -73,6 +76,7 @@ export class HttpInstrumentation extends InstrumentationBase<Http> {

override setConfig(config: HttpInstrumentationConfig & InstrumentationConfig = {}): void {
this._config = Object.assign({}, config);
this._headerCapture = this._createHeaderCapture();
}

init(): [InstrumentationNodeModuleDefinition<Https>, InstrumentationNodeModuleDefinition<Http>] {
Expand Down Expand Up @@ -296,6 +300,9 @@ export class HttpInstrumentation extends InstrumentationBase<Http> {
this._callResponseHook(span, response);
}

this._headerCapture.client.captureRequestHeaders(span, header => request.getHeader(header));
this._headerCapture.client.captureResponseHeaders(span, header => response.headers[header]);

context.bind(context.active(), response);
this._diag.debug('outgoingRequest on response()');
response.on('end', () => {
Expand Down Expand Up @@ -424,6 +431,8 @@ export class HttpInstrumentation extends InstrumentationBase<Http> {
instrumentation._callResponseHook(span, response);
}

instrumentation._headerCapture.server.captureRequestHeaders(span, header => request.headers[header]);

// Wraps end (inspired by:
// https://github.com/GoogleCloudPlatform/cloud-trace-nodejs/blob/master/src/instrumentations/instrumentation-connect.ts#L75)
const originalEnd = response.end;
Expand All @@ -449,6 +458,8 @@ export class HttpInstrumentation extends InstrumentationBase<Http> {
response
);

instrumentation._headerCapture.server.captureResponseHeaders(span, header => response.getHeader(header));

span
.setAttributes(attributes)
.setStatus(utils.parseResponseStatus(response.statusCode));
Expand Down Expand Up @@ -662,4 +673,19 @@ export class HttpInstrumentation extends InstrumentationBase<Http> {
);
}
}

private _createHeaderCapture() {
const config = this._getConfig();

return {
client: {
captureRequestHeaders: utils.headerCapture('request', config.headersToSpanAttributes?.client?.requestHeaders ?? []),
captureResponseHeaders: utils.headerCapture('response', config.headersToSpanAttributes?.client?.responseHeaders ?? [])
},
server: {
captureRequestHeaders: utils.headerCapture('request', config.headersToSpanAttributes?.server?.requestHeaders ?? []),
captureResponseHeaders: utils.headerCapture('response', config.headersToSpanAttributes?.server?.responseHeaders ?? []),
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ export interface HttpInstrumentationConfig extends InstrumentationConfig {
requireParentforOutgoingSpans?: boolean;
/** Require parent to create span for incoming requests */
requireParentforIncomingSpans?: boolean;
/** Map the following HTTP headers to span attributes. */
headersToSpanAttributes?: {
client?: { requestHeaders?: string[]; responseHeaders?: string[]; },
server?: { requestHeaders?: string[]; responseHeaders?: string[]; },
}
}

export interface Err extends Error {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,6 @@ export const getIncomingRequestAttributes = (
}

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

Expand Down Expand Up @@ -495,3 +494,27 @@ export const getIncomingRequestAttributesOnResponse = (
}
return attributes;
};

export function headerCapture(type: 'request' | 'response', headers: string[]) {
const normalizedHeaders = new Map(headers.map(header => [header.toLowerCase(), header.toLowerCase().replace(/-/g, '_')]));

return (span: Span, getHeader: (key: string) => undefined | string | string[] | number) => {
for (const [capturedHeader, normalizedHeader] of normalizedHeaders) {
const value = getHeader(capturedHeader);

if (value === undefined) {
continue;
}

const key = `http.${type}.header.${normalizedHeader}`;

if (typeof value === 'string') {
span.setAttribute(key, [value]);
} else if (Array.isArray(value)) {
span.setAttribute(key, value);
} else {
span.setAttribute(key, [value]);
}
}
};
}
Loading

0 comments on commit 7b134c6

Please sign in to comment.