@opencensus/instrumentation-http
Advanced tools
Comparing version 0.0.8 to 0.0.9
@@ -1,2 +0,1 @@ | ||
/// <reference types="node" /> | ||
/** | ||
@@ -17,4 +16,6 @@ * Copyright 2018, OpenCensus Authors | ||
*/ | ||
/// <reference types="node" /> | ||
import { BasePlugin, Func } from '@opencensus/core'; | ||
import * as httpModule from 'http'; | ||
import { HttpPluginConfig, IgnoreMatcher } from './types'; | ||
export declare type HttpModule = typeof httpModule; | ||
@@ -36,2 +37,3 @@ export declare type RequestFunction = typeof httpModule.request; | ||
static ATTRIBUTE_HTTP_ERROR_MESSAGE: string; | ||
protected options: HttpPluginConfig; | ||
/** Constructs a new HttpPlugin instance. */ | ||
@@ -46,2 +48,16 @@ constructor(moduleName: string); | ||
/** | ||
* Check whether the given request is ignored by configuration | ||
* @param url URL of request | ||
* @param request Request to inspect | ||
* @param list List of ignore patterns | ||
*/ | ||
protected isIgnored<T>(url: string, request: T, list: Array<IgnoreMatcher<T>>): boolean; | ||
/** | ||
* Check whether the given request match pattern | ||
* @param url URL of request | ||
* @param request Request to inspect | ||
* @param pattern Match pattern | ||
*/ | ||
protected isSatisfyPattern<T>(url: string, request: T, pattern: IgnoreMatcher<T>): boolean; | ||
/** | ||
* Creates spans for incoming requests, restoring spans' context if applied. | ||
@@ -61,25 +77,10 @@ */ | ||
*/ | ||
private getMakeRequestTraceFunction(request, options, plugin); | ||
private getMakeRequestTraceFunction; | ||
/** | ||
* Converts an HTTP status code to an OpenCensus Trace status code. | ||
* @param statusCode The HTTP status code to convert. | ||
* Parse OpenCensus Status from HTTP response status code. | ||
* @param statusCode The HTTP response status code. | ||
*/ | ||
static convertTraceStatus(statusCode: number): number; | ||
static parseResponseStatus(statusCode: number): number; | ||
} | ||
/** | ||
* An enumeration of OpenCensus Trace status codes. | ||
*/ | ||
export declare enum TraceStatusCodes { | ||
UNKNOWN = 2, | ||
OK = 0, | ||
INVALID_ARGUMENT = 3, | ||
DEADLINE_EXCEEDED = 4, | ||
NOT_FOUND = 5, | ||
PERMISSION_DENIED = 7, | ||
UNAUTHENTICATED = 16, | ||
RESOURCE_EXHAUSTED = 8, | ||
UNIMPLEMENTED = 12, | ||
UNAVAILABLE = 14, | ||
} | ||
declare const plugin: HttpPlugin; | ||
export { plugin }; |
@@ -61,2 +61,40 @@ "use strict"; | ||
/** | ||
* Check whether the given request is ignored by configuration | ||
* @param url URL of request | ||
* @param request Request to inspect | ||
* @param list List of ignore patterns | ||
*/ | ||
isIgnored(url, request, list) { | ||
if (!list) { | ||
// No ignored urls - trace everything | ||
return false; | ||
} | ||
for (const pattern of list) { | ||
if (this.isSatisfyPattern(url, request, pattern)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
/** | ||
* Check whether the given request match pattern | ||
* @param url URL of request | ||
* @param request Request to inspect | ||
* @param pattern Match pattern | ||
*/ | ||
isSatisfyPattern(url, request, pattern) { | ||
if (typeof pattern === 'string') { | ||
return pattern === url; | ||
} | ||
else if (pattern instanceof RegExp) { | ||
return pattern.test(url); | ||
} | ||
else if (typeof pattern === 'function') { | ||
return pattern(url, request); | ||
} | ||
else { | ||
throw new TypeError('Pattern is in unsupported datatype'); | ||
} | ||
} | ||
/** | ||
* Creates spans for incoming requests, restoring spans' context if applied. | ||
@@ -77,3 +115,7 @@ */ | ||
const response = args[1]; | ||
const path = url.parse(request.url).pathname; | ||
plugin.logger.debug('%s plugin incomingRequest', plugin.moduleName); | ||
if (plugin.isIgnored(path, request, plugin.options.ignoreIncomingPaths)) { | ||
return original.apply(this, arguments); | ||
} | ||
const propagation = plugin.tracer.propagation; | ||
@@ -87,4 +129,4 @@ const headers = request.headers; | ||
const traceOptions = { | ||
name: url.parse(request.url).pathname, | ||
kind: 'SERVER', | ||
name: path, | ||
kind: core_1.SpanKind.SERVER, | ||
spanContext: propagation ? propagation.extract(getter) : null | ||
@@ -112,6 +154,5 @@ }; | ||
rootSpan.addAttribute(HttpPlugin.ATTRIBUTE_HTTP_STATUS_CODE, response.statusCode.toString()); | ||
rootSpan.status = | ||
HttpPlugin.convertTraceStatus(response.statusCode); | ||
rootSpan.setStatus(HttpPlugin.parseResponseStatus(response.statusCode)); | ||
// Message Event ID is not defined | ||
rootSpan.addMessageEvent('MessageEventTypeRecv', uuid.v4().split('-').join('')); | ||
rootSpan.addMessageEvent(core_1.MessageEventType.RECEIVED, uuid.v4().split('-').join('')); | ||
rootSpan.end(); | ||
@@ -138,6 +179,9 @@ return returned; | ||
let pathname = ''; | ||
let method = 'GET'; | ||
let origin = ''; | ||
if (typeof (options) === 'string') { | ||
options = url.parse(options); | ||
arguments[0] = options; | ||
pathname = options.pathname; | ||
const parsedUrl = url.parse(options); | ||
options = parsedUrl; | ||
pathname = parsedUrl.pathname; | ||
origin = `${parsedUrl.protocol || 'http:'}//${parsedUrl.host}`; | ||
} | ||
@@ -152,3 +196,6 @@ else { | ||
try { | ||
pathname = options.pathname || url.parse(options.path).pathname; | ||
pathname = options.pathname || | ||
url.parse(options.path).pathname; | ||
method = options.method; | ||
origin = `${options.protocol || 'http:'}//${options.host}`; | ||
} | ||
@@ -159,7 +206,10 @@ catch (e) { | ||
const request = original.apply(this, arguments); | ||
if (plugin.isIgnored(origin + pathname, request, plugin.options.ignoreOutgoingUrls)) { | ||
return request; | ||
} | ||
plugin.tracer.wrapEmitter(request); | ||
plugin.logger.debug('%s plugin outgoingRequest', plugin.moduleName); | ||
const traceOptions = { | ||
name: `${request.method ? request.method : 'GET'} ${pathname}`, | ||
kind: 'CLIENT', | ||
name: `${method || 'GET'} ${pathname}`, | ||
kind: core_1.SpanKind.CLIENT, | ||
}; | ||
@@ -222,5 +272,5 @@ // Checks if this outgoing request is part of an operation by checking | ||
span.addAttribute(HttpPlugin.ATTRIBUTE_HTTP_STATUS_CODE, response.statusCode.toString()); | ||
span.status = HttpPlugin.convertTraceStatus(response.statusCode); | ||
span.setStatus(HttpPlugin.parseResponseStatus(response.statusCode)); | ||
// Message Event ID is not defined | ||
span.addMessageEvent('MessageEventTypeSent', uuid.v4().split('-').join('')); | ||
span.addMessageEvent(core_1.MessageEventType.SENT, uuid.v4().split('-').join('')); | ||
span.end(); | ||
@@ -231,3 +281,3 @@ }); | ||
span.addAttribute(HttpPlugin.ATTRIBUTE_HTTP_ERROR_MESSAGE, error.message); | ||
span.status = TraceStatusCodes.UNKNOWN; | ||
span.setStatus(core_1.CanonicalCode.UNKNOWN, error.message); | ||
span.end(); | ||
@@ -239,3 +289,3 @@ }); | ||
span.addAttribute(HttpPlugin.ATTRIBUTE_HTTP_ERROR_MESSAGE, error.message); | ||
span.status = TraceStatusCodes.UNKNOWN; | ||
span.setStatus(core_1.CanonicalCode.UNKNOWN, error.message); | ||
span.end(); | ||
@@ -248,11 +298,11 @@ }); | ||
/** | ||
* Converts an HTTP status code to an OpenCensus Trace status code. | ||
* @param statusCode The HTTP status code to convert. | ||
* Parse OpenCensus Status from HTTP response status code. | ||
* @param statusCode The HTTP response status code. | ||
*/ | ||
static convertTraceStatus(statusCode) { | ||
static parseResponseStatus(statusCode) { | ||
if (statusCode < 200 || statusCode > 504) { | ||
return TraceStatusCodes.UNKNOWN; | ||
return core_1.CanonicalCode.UNKNOWN; | ||
} | ||
else if (statusCode >= 200 && statusCode < 400) { | ||
return TraceStatusCodes.OK; | ||
return core_1.CanonicalCode.OK; | ||
} | ||
@@ -262,19 +312,19 @@ else { | ||
case (400): | ||
return TraceStatusCodes.INVALID_ARGUMENT; | ||
return core_1.CanonicalCode.INVALID_ARGUMENT; | ||
case (504): | ||
return TraceStatusCodes.DEADLINE_EXCEEDED; | ||
return core_1.CanonicalCode.DEADLINE_EXCEEDED; | ||
case (404): | ||
return TraceStatusCodes.NOT_FOUND; | ||
return core_1.CanonicalCode.NOT_FOUND; | ||
case (403): | ||
return TraceStatusCodes.PERMISSION_DENIED; | ||
return core_1.CanonicalCode.PERMISSION_DENIED; | ||
case (401): | ||
return TraceStatusCodes.UNAUTHENTICATED; | ||
return core_1.CanonicalCode.UNAUTHENTICATED; | ||
case (429): | ||
return TraceStatusCodes.RESOURCE_EXHAUSTED; | ||
return core_1.CanonicalCode.RESOURCE_EXHAUSTED; | ||
case (501): | ||
return TraceStatusCodes.UNIMPLEMENTED; | ||
return core_1.CanonicalCode.UNIMPLEMENTED; | ||
case (503): | ||
return TraceStatusCodes.UNAVAILABLE; | ||
return core_1.CanonicalCode.UNAVAILABLE; | ||
default: | ||
return TraceStatusCodes.UNKNOWN; | ||
return core_1.CanonicalCode.UNKNOWN; | ||
} | ||
@@ -298,20 +348,4 @@ } | ||
exports.HttpPlugin = HttpPlugin; | ||
/** | ||
* An enumeration of OpenCensus Trace status codes. | ||
*/ | ||
var TraceStatusCodes; | ||
(function (TraceStatusCodes) { | ||
TraceStatusCodes[TraceStatusCodes["UNKNOWN"] = 2] = "UNKNOWN"; | ||
TraceStatusCodes[TraceStatusCodes["OK"] = 0] = "OK"; | ||
TraceStatusCodes[TraceStatusCodes["INVALID_ARGUMENT"] = 3] = "INVALID_ARGUMENT"; | ||
TraceStatusCodes[TraceStatusCodes["DEADLINE_EXCEEDED"] = 4] = "DEADLINE_EXCEEDED"; | ||
TraceStatusCodes[TraceStatusCodes["NOT_FOUND"] = 5] = "NOT_FOUND"; | ||
TraceStatusCodes[TraceStatusCodes["PERMISSION_DENIED"] = 7] = "PERMISSION_DENIED"; | ||
TraceStatusCodes[TraceStatusCodes["UNAUTHENTICATED"] = 16] = "UNAUTHENTICATED"; | ||
TraceStatusCodes[TraceStatusCodes["RESOURCE_EXHAUSTED"] = 8] = "RESOURCE_EXHAUSTED"; | ||
TraceStatusCodes[TraceStatusCodes["UNIMPLEMENTED"] = 12] = "UNIMPLEMENTED"; | ||
TraceStatusCodes[TraceStatusCodes["UNAVAILABLE"] = 14] = "UNAVAILABLE"; | ||
})(TraceStatusCodes = exports.TraceStatusCodes || (exports.TraceStatusCodes = {})); | ||
const plugin = new HttpPlugin('http'); | ||
exports.plugin = plugin; | ||
//# sourceMappingURL=http.js.map |
{ | ||
"name": "@opencensus/instrumentation-http", | ||
"version": "0.0.8", | ||
"version": "0.0.9", | ||
"description": "Opencensus http automatic instrumentation package.", | ||
@@ -9,3 +9,4 @@ "main": "build/src/index.js", | ||
"scripts": { | ||
"test": "nyc -x '**/test/**' --reporter=html --reporter=text mocha 'build/test/**/*.js'", | ||
"test": "nyc mocha build/test/**/*.js", | ||
"codecov": "nyc report --reporter=json && codecov -f coverage/*.json", | ||
"clean": "rimraf build/*", | ||
@@ -49,13 +50,14 @@ "check": "gts check", | ||
"@types/uuid": "^3.4.3", | ||
"codecov": "^3.1.0", | ||
"gts": "^0.9.0", | ||
"mocha": "^5.0.4", | ||
"ncp": "^2.0.0", | ||
"nock": "^9.2.6", | ||
"nyc": "^11.7.1", | ||
"nock": "^10.0.0", | ||
"nyc": "^13.0.0", | ||
"rimraf": "^2.6.2", | ||
"ts-node": "^7.0.1", | ||
"typescript": "~2.7.2" | ||
"typescript": "~2.9.0" | ||
}, | ||
"dependencies": { | ||
"@opencensus/core": "^0.0.8", | ||
"@opencensus/core": "^0.0.9", | ||
"end-of-stream": "^1.4.1", | ||
@@ -62,0 +64,0 @@ "semver": "^5.5.0", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
34744
9
465
0
16
+ Added@opencensus/core@0.0.9(transitive)
- Removed@opencensus/core@0.0.8(transitive)
Updated@opencensus/core@^0.0.9