Socket
Socket
Sign inDemoInstall

@opencensus/instrumentation-http

Package Overview
Dependencies
Maintainers
3
Versions
23
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@opencensus/instrumentation-http - npm Package Compare versions

Comparing version 0.0.1 to 0.0.2

23

build/src/http.d.ts

@@ -36,3 +36,2 @@ /// <reference types="node" />

static ATTRIBUTE_HTTP_ERROR_MESSAGE: string;
logger: types.Logger;
/** Constructs a new HttpPlugin instance. */

@@ -42,13 +41,10 @@ constructor(moduleName: string);

* Patches HTTP incoming and outcoming request functions.
* @param moduleExports The http module exports
* @param tracer A tracer instance to create spans on.
* @param version The package version.
*/
applyPatch(moduleExports: any, tracer: types.Tracer, version: string): any;
protected applyPatch(): any;
/** Unpatches all HTTP patched function. */
applyUnpatch(): void;
protected applyUnpatch(): void;
/**
* Creates spans for incoming requests, restoring spans' context if applied.
*/
patchIncomingRequest(): (original: typeof httpModule.request) => types.Func<httpModule.ClientRequest>;
protected getPatchIncomingRequestFunction(): (original: (event: string) => boolean) => (event: string, ...args: any[]) => boolean;
/**

@@ -58,3 +54,3 @@ * Creates spans for outgoing requests, sending spans' context for distributed

*/
patchOutgoingRequest(): (original: types.Func<httpModule.ClientRequest>) => types.Func<httpModule.ClientRequest>;
protected getPatchOutgoingRequestFunction(): (original: types.Func<httpModule.ClientRequest>) => types.Func<httpModule.ClientRequest>;
/**

@@ -66,5 +62,12 @@ * Injects span's context to header for distributed tracing and finshes the

*/
makeRequestTrace(request: httpModule.ClientRequest, options: any, plugin: HttpPlugin): types.Func<httpModule.ClientRequest>;
traceStatus(statusCode: number): number;
private getMakeRequestTraceFunction(request, options, plugin);
/**
* Converts an HTTP status code to an OpenCensus Trace status code.
* @param statusCode The HTTP status code to convert.
*/
static convertTraceStatus(statusCode: number): number;
}
/**
* An enumeration of OpenCensus Trace status codes.
*/
export declare enum TraceStatusCodes {

@@ -71,0 +74,0 @@ UNKNOWN = 2,

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

const opencensus_core_1 = require("@opencensus/opencensus-core");
const opencensus_core_2 = require("@opencensus/opencensus-core");
const semver = require("semver");

@@ -31,24 +30,16 @@ const shimmer = require("shimmer");

}
// TODO: moduleExports should use type HttpModule instead of any
/**
* Patches HTTP incoming and outcoming request functions.
* @param moduleExports The http module exports
* @param tracer A tracer instance to create spans on.
* @param version The package version.
*/
// tslint:disable-next-line:no-any
applyPatch(moduleExports, tracer, version) {
this.setPluginContext(moduleExports, tracer, version);
this.logger = tracer.logger || opencensus_core_2.logger.logger('debug');
applyPatch() {
this.logger.debug('applying pacth to %s@%s', this.moduleName, this.version);
shimmer.wrap(moduleExports, 'request', this.patchOutgoingRequest());
shimmer.wrap(this.moduleExports, 'request', this.getPatchOutgoingRequestFunction());
// In Node 8, http.get calls a private request method, therefore we patch it
// here too.
if (semver.satisfies(version, '>=8.0.0')) {
shimmer.wrap(moduleExports, 'get', this.patchOutgoingRequest());
if (semver.satisfies(this.version, '>=8.0.0')) {
shimmer.wrap(this.moduleExports, 'get', this.getPatchOutgoingRequestFunction());
}
if (moduleExports && moduleExports.Server &&
moduleExports.Server.prototype) {
shimmer.wrap(moduleExports && moduleExports.Server &&
moduleExports.Server.prototype, 'emit', this.patchIncomingRequest());
if (this.moduleExports && this.moduleExports.Server &&
this.moduleExports.Server.prototype) {
shimmer.wrap(this.moduleExports.Server.prototype, 'emit', this.getPatchIncomingRequestFunction());
}

@@ -58,3 +49,3 @@ else {

}
return moduleExports;
return this.moduleExports;
}

@@ -69,4 +60,3 @@ /** Unpatches all HTTP patched function. */

this.moduleExports.Server.prototype) {
shimmer.unwrap(this.moduleExports && this.moduleExports.Server &&
this.moduleExports.Server.prototype, 'emit');
shimmer.unwrap(this.moduleExports.Server.prototype, 'emit');
}

@@ -77,7 +67,9 @@ }

*/
patchIncomingRequest() {
// TODO: evaluate if this function should return RequestFunction
getPatchIncomingRequestFunction() {
return (original) => {
const plugin = this;
return function incomingRequest(event, request, response) {
// This function's signature is that of an event listener, which can have
// any number of variable-type arguments.
// tslint:disable-next-line:no-any
return function incomingRequest(event, ...args) {
// Only traces request events

@@ -87,2 +79,4 @@ if (event !== 'request') {

}
const request = args[0];
const response = args[1];
plugin.logger.debug('%s plugin incomingRequest', plugin.moduleName);

@@ -98,3 +92,3 @@ const propagation = plugin.tracer.propagation;

name: url.parse(request.url).pathname,
type: 'SERVER',
kind: 'SERVER',
spanContext: propagation ? propagation.extract(getter) : null

@@ -122,3 +116,4 @@ };

rootSpan.addAttribute(HttpPlugin.ATTRIBUTE_HTTP_STATUS_CODE, response.statusCode.toString());
rootSpan.status = plugin.traceStatus(response.statusCode);
rootSpan.status =
HttpPlugin.convertTraceStatus(response.statusCode);
// Message Event ID is not defined

@@ -138,3 +133,3 @@ rootSpan.addMessageEvent('MessageEventTypeRecv', uuid.v4().split('-').join(''));

*/
patchOutgoingRequest() {
getPatchOutgoingRequestFunction() {
return (original) => {

@@ -164,3 +159,3 @@ const plugin = this;

name: `${request.method ? request.method : 'GET'} ${options.pathname}`,
type: 'CLIENT',
kind: 'CLIENT',
};

@@ -173,8 +168,8 @@ // Checks if this outgoing request is part of an operation by checking

plugin.logger.debug('outgoingRequest starting a root span');
return plugin.tracer.startRootSpan(traceOptions, plugin.makeRequestTrace(request, options, plugin));
return plugin.tracer.startRootSpan(traceOptions, plugin.getMakeRequestTraceFunction(request, options, plugin));
}
else {
plugin.logger.debug('outgoingRequest starting a child span');
const span = plugin.tracer.startChildSpan(traceOptions.name, traceOptions.type);
return (plugin.makeRequestTrace(request, options, plugin))(span);
const span = plugin.tracer.startChildSpan(traceOptions.name, traceOptions.kind);
return (plugin.getMakeRequestTraceFunction(request, options, plugin))(span);
}

@@ -184,3 +179,2 @@ };

}
// TODO: type of options shold be better define
/**

@@ -192,3 +186,3 @@ * Injects span's context to header for distributed tracing and finshes the

*/
makeRequestTrace(
getMakeRequestTraceFunction(
// tslint:disable-next-line:no-any

@@ -218,11 +212,12 @@ request, options, plugin) {

const method = response.method ? response.method : 'GET';
const reqUrl = url.parse(options);
const userAgent = headers ? (headers['user-agent'] || headers['User-Agent']) : null;
span.addAttribute(HttpPlugin.ATTRIBUTE_HTTP_HOST, reqUrl.hostname);
span.addAttribute(HttpPlugin.ATTRIBUTE_HTTP_HOST, options.hostname);
span.addAttribute(HttpPlugin.ATTRIBUTE_HTTP_METHOD, method);
span.addAttribute(HttpPlugin.ATTRIBUTE_HTTP_PATH, reqUrl.pathname);
span.addAttribute(HttpPlugin.ATTRIBUTE_HTTP_ROUTE, reqUrl.path);
span.addAttribute(HttpPlugin.ATTRIBUTE_HTTP_USER_AGENT, userAgent);
span.addAttribute(HttpPlugin.ATTRIBUTE_HTTP_PATH, options.path);
span.addAttribute(HttpPlugin.ATTRIBUTE_HTTP_ROUTE, options.path);
if (userAgent) {
span.addAttribute(HttpPlugin.ATTRIBUTE_HTTP_USER_AGENT, userAgent.toString());
}
span.addAttribute(HttpPlugin.ATTRIBUTE_HTTP_STATUS_CODE, response.statusCode.toString());
span.status = plugin.traceStatus(response.statusCode);
span.status = HttpPlugin.convertTraceStatus(response.statusCode);
// Message Event ID is not defined

@@ -243,3 +238,7 @@ span.addMessageEvent('MessageEventTypeSent', uuid.v4().split('-').join(''));

}
traceStatus(statusCode) {
/**
* Converts an HTTP status code to an OpenCensus Trace status code.
* @param statusCode The HTTP status code to convert.
*/
static convertTraceStatus(statusCode) {
if (statusCode < 200 || statusCode > 504) {

@@ -289,2 +288,5 @@ return TraceStatusCodes.UNKNOWN;

exports.HttpPlugin = HttpPlugin;
/**
* An enumeration of OpenCensus Trace status codes.
*/
var TraceStatusCodes;

@@ -291,0 +293,0 @@ (function (TraceStatusCodes) {

{
"name": "@opencensus/instrumentation-http",
"version": "0.0.1",
"version": "0.0.2",
"description": "Opencensus http automatic instrumentation package.",

@@ -53,3 +53,3 @@ "main": "build/src/index.js",

"nyc": "^11.7.1",
"rimraf": "^2.6.2",
"rimraf": "^2.6.2",
"ts-node": "^4.0.0",

@@ -59,3 +59,3 @@ "typescript": "~2.7.2"

"dependencies": {
"@opencensus/opencensus-core": "^0.0.1",
"@opencensus/opencensus-core": "^0.0.2",
"end-of-stream": "^1.4.1",

@@ -62,0 +62,0 @@ "semver": "^5.5.0",

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