Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@opentelemetry/instrumentation-http

Package Overview
Dependencies
Maintainers
2
Versions
192
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@opentelemetry/instrumentation-http - npm Package Compare versions

Comparing version 0.32.0 to 0.33.0

7

build/src/http.d.ts
/// <reference types="node" />
import { MeterProvider } from '@opentelemetry/api-metrics';
import type * as http from 'http';

@@ -14,3 +15,7 @@ import * as url from 'url';

private _headerCapture;
private _httpServerDurationHistogram;
private _httpClientDurationHistogram;
constructor(config?: HttpInstrumentationConfig);
setMeterProvider(meterProvider: MeterProvider): void;
private _updateMetricInstruments;
private _getConfig;

@@ -42,2 +47,4 @@ setConfig(config?: HttpInstrumentationConfig): void;

* @param span representing the current operation
* @param startTime representing the start time of the request to calculate duration in Metric
* @param metricAttributes metric attributes
*/

@@ -44,0 +51,0 @@ private _traceClientRequest;

67

build/src/http.js

@@ -20,2 +20,3 @@ "use strict";

const api_1 = require("@opentelemetry/api");
const api_metrics_1 = require("@opentelemetry/api-metrics");
const core_1 = require("@opentelemetry/core");

@@ -38,3 +39,20 @@ const semver = require("semver");

this._headerCapture = this._createHeaderCapture();
this._updateMetricInstruments();
}
setMeterProvider(meterProvider) {
super.setMeterProvider(meterProvider);
this._updateMetricInstruments();
}
_updateMetricInstruments() {
this._httpServerDurationHistogram = this.meter.createHistogram('http.server.duration', {
description: 'measures the duration of the inbound HTTP requests',
unit: 'ms',
valueType: api_metrics_1.ValueType.DOUBLE
});
this._httpClientDurationHistogram = this.meter.createHistogram('http.client.duration', {
description: 'measures the duration of the outbound HTTP requests',
unit: 'ms',
valueType: api_metrics_1.ValueType.DOUBLE
});
}
_getConfig() {

@@ -176,4 +194,6 @@ return this._config;

* @param span representing the current operation
* @param startTime representing the start time of the request to calculate duration in Metric
* @param metricAttributes metric attributes
*/
_traceClientRequest(request, hostname, span) {
_traceClientRequest(request, hostname, span, startTime, metricAttributes) {
if (this._getConfig().requestHook) {

@@ -190,2 +210,3 @@ this._callRequestHook(span, request);

span.setAttributes(responseAttributes);
metricAttributes = Object.assign(metricAttributes, utils.getOutgoingRequestMetricAttributesOnResponse(responseAttributes));
if (this._getConfig().responseHook) {

@@ -211,3 +232,3 @@ this._callResponseHook(span, response);

}
this._closeHttpSpan(span);
this._closeHttpSpan(span, api_1.SpanKind.CLIENT, startTime, metricAttributes);
});

@@ -219,3 +240,3 @@ response.on('error', (error) => {

span.setStatus({ code, message: error.message });
this._closeHttpSpan(span);
this._closeHttpSpan(span, api_1.SpanKind.CLIENT, startTime, metricAttributes);
});

@@ -226,3 +247,3 @@ });

if (!request.aborted) {
this._closeHttpSpan(span);
this._closeHttpSpan(span, api_1.SpanKind.CLIENT, startTime, metricAttributes);
}

@@ -233,3 +254,3 @@ });

utils.setSpanWithError(span, error);
this._closeHttpSpan(span);
this._closeHttpSpan(span, api_1.SpanKind.CLIENT, startTime, metricAttributes);
});

@@ -266,10 +287,13 @@ this._diag.debug('http.ClientRequest return request');

const headers = request.headers;
const spanAttributes = utils.getIncomingRequestAttributes(request, {
component: component,
serverName: instrumentation._getConfig().serverName,
hookAttributes: instrumentation._callStartSpanHook(request, instrumentation._getConfig().startIncomingSpanHook),
});
const spanOptions = {
kind: api_1.SpanKind.SERVER,
attributes: utils.getIncomingRequestAttributes(request, {
component: component,
serverName: instrumentation._getConfig().serverName,
hookAttributes: instrumentation._callStartSpanHook(request, instrumentation._getConfig().startIncomingSpanHook),
}),
attributes: spanAttributes,
};
const startTime = (0, core_1.hrTime)();
let metricAttributes = utils.getIncomingRequestMetricAttributes(spanAttributes);
const ctx = api_1.propagation.extract(api_1.ROOT_CONTEXT, headers);

@@ -300,3 +324,3 @@ const span = instrumentation._startHttpSpan(`${component.toLocaleUpperCase()} ${method}`, spanOptions, ctx);

utils.setSpanWithError(span, error);
instrumentation._closeHttpSpan(span);
instrumentation._closeHttpSpan(span, api_1.SpanKind.SERVER, startTime, metricAttributes);
throw error;

@@ -306,2 +330,3 @@ }

const attributes = utils.getIncomingRequestAttributesOnResponse(request, response);
metricAttributes = Object.assign(metricAttributes, utils.getIncomingRequestMetricAttributesOnResponse(attributes));
instrumentation._headerCapture.server.captureResponseHeaders(span, header => response.getHeader(header));

@@ -314,3 +339,3 @@ span

}
instrumentation._closeHttpSpan(span);
instrumentation._closeHttpSpan(span, api_1.SpanKind.SERVER, startTime, metricAttributes);
return returned;

@@ -321,3 +346,3 @@ };

utils.setSpanWithError(span, error);
instrumentation._closeHttpSpan(span);
instrumentation._closeHttpSpan(span, api_1.SpanKind.SERVER, startTime, metricAttributes);
throw error;

@@ -366,2 +391,4 @@ }

});
const startTime = (0, core_1.hrTime)();
const metricAttributes = utils.getOutgoingRequestMetricAttributes(attributes);
const spanOptions = {

@@ -390,3 +417,3 @@ kind: api_1.SpanKind.CLIENT,

utils.setSpanWithError(span, error);
instrumentation._closeHttpSpan(span);
instrumentation._closeHttpSpan(span, api_1.SpanKind.CLIENT, startTime, metricAttributes);
throw error;

@@ -397,3 +424,3 @@ }

api_1.context.bind(parentContext, request);
return instrumentation._traceClientRequest(request, hostname, span);
return instrumentation._traceClientRequest(request, hostname, span, startTime, metricAttributes);
});

@@ -424,3 +451,3 @@ };

}
_closeHttpSpan(span) {
_closeHttpSpan(span, spanKind, startTime, metricAttributes) {
if (!this._spanNotEnded.has(span)) {

@@ -431,2 +458,10 @@ return;

this._spanNotEnded.delete(span);
// Record metrics
const duration = (0, core_1.hrTimeToMilliseconds)((0, core_1.hrTimeDuration)(startTime, (0, core_1.hrTime)()));
if (spanKind === api_1.SpanKind.SERVER) {
this._httpServerDurationHistogram.record(duration, metricAttributes);
}
else if (spanKind === api_1.SpanKind.CLIENT) {
this._httpClientDurationHistogram.record(duration, metricAttributes);
}
}

@@ -433,0 +468,0 @@ _callResponseHook(span, response) {

@@ -5,2 +5,3 @@ import { SpanAttributes, SpanStatusCode, Span, SpanKind } from '@opentelemetry/api';

import { Err, IgnoreMatcher, ParsedRequestOptions } from './types';
import { MetricAttributes } from '@opentelemetry/api-metrics';
/**

@@ -81,2 +82,7 @@ * Get an absolute url

/**
* Returns outgoing request Metric attributes scoped to the request data
* @param {SpanAttributes} spanAttributes the span attributes
*/
export declare const getOutgoingRequestMetricAttributes: (spanAttributes: SpanAttributes) => MetricAttributes;
/**
* Returns attributes related to the kind of HTTP protocol used

@@ -93,2 +99,7 @@ * @param {string} [kind] Kind of HTTP protocol used: "1.0", "1.1", "2", "SPDY" or "QUIC".

/**
* Returns outgoing request Metric attributes scoped to the response data
* @param {SpanAttributes} spanAttributes the span attributes
*/
export declare const getOutgoingRequestMetricAttributesOnResponse: (spanAttributes: SpanAttributes) => MetricAttributes;
/**
* Returns incoming request attributes scoped to the request data

@@ -104,2 +115,8 @@ * @param {IncomingMessage} request the request object

/**
* Returns incoming request Metric attributes scoped to the request data
* @param {SpanAttributes} spanAttributes the span attributes
* @param {{ component: string }} options used to pass data needed to create attributes
*/
export declare const getIncomingRequestMetricAttributes: (spanAttributes: SpanAttributes) => MetricAttributes;
/**
* Returns incoming request attributes scoped to the response data

@@ -109,3 +126,8 @@ * @param {(ServerResponse & { socket: Socket; })} response the response object

export declare const getIncomingRequestAttributesOnResponse: (request: IncomingMessage, response: ServerResponse) => SpanAttributes;
/**
* Returns incoming request Metric attributes scoped to the request data
* @param {SpanAttributes} spanAttributes the span attributes
*/
export declare const getIncomingRequestMetricAttributesOnResponse: (spanAttributes: SpanAttributes) => MetricAttributes;
export declare function headerCapture(type: 'request' | 'response', headers: string[]): (span: Span, getHeader: (key: string) => undefined | string | string[] | number) => void;
//# sourceMappingURL=utils.d.ts.map
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.headerCapture = exports.getIncomingRequestAttributesOnResponse = exports.getIncomingRequestAttributes = exports.getOutgoingRequestAttributesOnResponse = exports.getAttributesFromHttpKind = exports.getOutgoingRequestAttributes = exports.extractHostnameAndPort = exports.isValidOptionsType = exports.getRequestInfo = exports.isCompressed = exports.setResponseContentLengthAttribute = exports.setRequestContentLengthAttribute = exports.setSpanWithError = exports.isIgnored = exports.satisfiesPattern = exports.parseResponseStatus = exports.getAbsoluteUrl = void 0;
exports.headerCapture = exports.getIncomingRequestMetricAttributesOnResponse = exports.getIncomingRequestAttributesOnResponse = exports.getIncomingRequestMetricAttributes = exports.getIncomingRequestAttributes = exports.getOutgoingRequestMetricAttributesOnResponse = exports.getOutgoingRequestAttributesOnResponse = exports.getAttributesFromHttpKind = exports.getOutgoingRequestMetricAttributes = exports.getOutgoingRequestAttributes = exports.extractHostnameAndPort = exports.isValidOptionsType = exports.getRequestInfo = exports.isCompressed = exports.setResponseContentLengthAttribute = exports.setRequestContentLengthAttribute = exports.setSpanWithError = exports.isIgnored = exports.satisfiesPattern = exports.parseResponseStatus = exports.getAbsoluteUrl = void 0;
/*

@@ -290,2 +290,14 @@ * Copyright The OpenTelemetry Authors

/**
* Returns outgoing request Metric attributes scoped to the request data
* @param {SpanAttributes} spanAttributes the span attributes
*/
const getOutgoingRequestMetricAttributes = (spanAttributes) => {
const metricAttributes = {};
metricAttributes[semantic_conventions_1.SemanticAttributes.HTTP_METHOD] = spanAttributes[semantic_conventions_1.SemanticAttributes.HTTP_METHOD];
metricAttributes[semantic_conventions_1.SemanticAttributes.NET_PEER_NAME] = spanAttributes[semantic_conventions_1.SemanticAttributes.NET_PEER_NAME];
//TODO: http.url attribute, it should susbtitute any parameters to avoid high cardinality.
return metricAttributes;
};
exports.getOutgoingRequestMetricAttributes = getOutgoingRequestMetricAttributes;
/**
* Returns attributes related to the kind of HTTP protocol used

@@ -330,2 +342,14 @@ * @param {string} [kind] Kind of HTTP protocol used: "1.0", "1.1", "2", "SPDY" or "QUIC".

/**
* Returns outgoing request Metric attributes scoped to the response data
* @param {SpanAttributes} spanAttributes the span attributes
*/
const getOutgoingRequestMetricAttributesOnResponse = (spanAttributes) => {
const metricAttributes = {};
metricAttributes[semantic_conventions_1.SemanticAttributes.NET_PEER_PORT] = spanAttributes[semantic_conventions_1.SemanticAttributes.NET_PEER_PORT];
metricAttributes[semantic_conventions_1.SemanticAttributes.HTTP_STATUS_CODE] = spanAttributes[semantic_conventions_1.SemanticAttributes.HTTP_STATUS_CODE];
metricAttributes[semantic_conventions_1.SemanticAttributes.HTTP_FLAVOR] = spanAttributes[semantic_conventions_1.SemanticAttributes.HTTP_FLAVOR];
return metricAttributes;
};
exports.getOutgoingRequestMetricAttributesOnResponse = getOutgoingRequestMetricAttributesOnResponse;
/**
* Returns incoming request attributes scoped to the request data

@@ -352,2 +376,3 @@ * @param {IncomingMessage} request the request object

[semantic_conventions_1.SemanticAttributes.HTTP_METHOD]: method,
[semantic_conventions_1.SemanticAttributes.HTTP_SCHEME]: options.component,
};

@@ -372,2 +397,17 @@ if (typeof ips === 'string') {

/**
* Returns incoming request Metric attributes scoped to the request data
* @param {SpanAttributes} spanAttributes the span attributes
* @param {{ component: string }} options used to pass data needed to create attributes
*/
const getIncomingRequestMetricAttributes = (spanAttributes) => {
const metricAttributes = {};
metricAttributes[semantic_conventions_1.SemanticAttributes.HTTP_SCHEME] = spanAttributes[semantic_conventions_1.SemanticAttributes.HTTP_SCHEME];
metricAttributes[semantic_conventions_1.SemanticAttributes.HTTP_METHOD] = spanAttributes[semantic_conventions_1.SemanticAttributes.HTTP_METHOD];
metricAttributes[semantic_conventions_1.SemanticAttributes.NET_HOST_NAME] = spanAttributes[semantic_conventions_1.SemanticAttributes.NET_HOST_NAME];
metricAttributes[semantic_conventions_1.SemanticAttributes.HTTP_FLAVOR] = spanAttributes[semantic_conventions_1.SemanticAttributes.HTTP_FLAVOR];
//TODO: http.target attribute, it should susbtitute any parameters to avoid high cardinality.
return metricAttributes;
};
exports.getIncomingRequestMetricAttributes = getIncomingRequestMetricAttributes;
/**
* Returns incoming request attributes scoped to the response data

@@ -397,2 +437,13 @@ * @param {(ServerResponse & { socket: Socket; })} response the response object

exports.getIncomingRequestAttributesOnResponse = getIncomingRequestAttributesOnResponse;
/**
* Returns incoming request Metric attributes scoped to the request data
* @param {SpanAttributes} spanAttributes the span attributes
*/
const getIncomingRequestMetricAttributesOnResponse = (spanAttributes) => {
const metricAttributes = {};
metricAttributes[semantic_conventions_1.SemanticAttributes.HTTP_STATUS_CODE] = spanAttributes[semantic_conventions_1.SemanticAttributes.HTTP_STATUS_CODE];
metricAttributes[semantic_conventions_1.SemanticAttributes.NET_HOST_PORT] = spanAttributes[semantic_conventions_1.SemanticAttributes.NET_HOST_PORT];
return metricAttributes;
};
exports.getIncomingRequestMetricAttributesOnResponse = getIncomingRequestMetricAttributesOnResponse;
function headerCapture(type, headers) {

@@ -399,0 +450,0 @@ const normalizedHeaders = new Map(headers.map(header => [header.toLowerCase(), header.toLowerCase().replace(/-/g, '_')]));

@@ -1,2 +0,2 @@

export declare const VERSION = "0.32.0";
export declare const VERSION = "0.33.0";
//# sourceMappingURL=version.d.ts.map

@@ -20,3 +20,3 @@ "use strict";

// this is autogenerated file, see scripts/version-update.js
exports.VERSION = '0.32.0';
exports.VERSION = '0.33.0';
//# sourceMappingURL=version.js.map
{
"name": "@opentelemetry/instrumentation-http",
"version": "0.32.0",
"version": "0.33.0",
"description": "OpenTelemetry http/https automatic instrumentation package.",

@@ -49,5 +49,5 @@ "main": "build/src/index.js",

"@opentelemetry/api": "^1.0.0",
"@opentelemetry/context-async-hooks": "1.6.0",
"@opentelemetry/sdk-trace-base": "1.6.0",
"@opentelemetry/sdk-trace-node": "1.6.0",
"@opentelemetry/context-async-hooks": "1.7.0",
"@opentelemetry/sdk-trace-base": "1.7.0",
"@opentelemetry/sdk-trace-node": "1.7.0",
"@types/got": "9.6.12",

@@ -78,9 +78,11 @@ "@types/mocha": "9.1.1",

"dependencies": {
"@opentelemetry/core": "1.6.0",
"@opentelemetry/instrumentation": "0.32.0",
"@opentelemetry/semantic-conventions": "1.6.0",
"@opentelemetry/api-metrics": "0.33.0",
"@opentelemetry/core": "1.7.0",
"@opentelemetry/instrumentation": "0.33.0",
"@opentelemetry/sdk-metrics": "0.33.0",
"@opentelemetry/semantic-conventions": "1.7.0",
"semver": "^7.3.5"
},
"homepage": "https://github.com/open-telemetry/opentelemetry-js/tree/main/experimental/packages/opentelemetry-instrumentation-http",
"gitHead": "a5abee69119cc41d9d34f6beb5c1826eef1ac0dd"
"gitHead": "ad88c3d9aa0100fe259b93f4b660e84417b757ac"
}

@@ -6,2 +6,4 @@ # OpenTelemetry HTTP and HTTPS Instrumentation for Node.js

**Note: This is an experimental package under active development. New releases may include breaking changes.**
This module provides automatic instrumentation for [`http`](https://nodejs.org/api/http.html) and [`https`](https://nodejs.org/api/https.html).

@@ -8,0 +10,0 @@

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc