datadog-tracer
Advanced tools
Comparing version 0.3.4 to 0.4.0
{ | ||
"name": "datadog-tracer", | ||
"version": "0.3.4", | ||
"version": "0.4.0", | ||
"description": "OpenTracing Tracer implementation for Datadog in JavaScript", | ||
"main": "src/index.js", | ||
"main": "index.js", | ||
"browser": "browser.js", | ||
"scripts": { | ||
"pbjs": "pbjs -t static-module src/propagation/state.proto > src/propagation/state.proto.js", | ||
"build": "gulp --gulpfile scripts/gulpfile.js", | ||
"browser": "live-server examples/browser --mount=/dist:./dist --proxy=/v0.3:http://localhost:8126/v0.3", | ||
"lint": "eslint .", | ||
@@ -21,2 +24,5 @@ "test": "eslint . && nyc --reporter=lcov --reporter=text mocha", | ||
}, | ||
"browserify-shim": { | ||
"protobufjs/minimal": "global:protobuf" | ||
}, | ||
"repository": { | ||
@@ -40,27 +46,46 @@ "type": "git", | ||
"@protobufjs/eventemitter": "^1.1.0", | ||
"json-bignum": "^0.0.3", | ||
"es6-promise": "^4.1.1", | ||
"long": "^3.2.0", | ||
"opentracing": "^0.14.0", | ||
"mersenne-twister": "^1.1.0", | ||
"opentracing": "^0.14.1", | ||
"performance-now": "^2.1.0", | ||
"protobufjs": "~6.7.3", | ||
"randombytes": "^2.0.3" | ||
"protobufjs": "^6.8.0", | ||
"typedarray": "0.0.6" | ||
}, | ||
"devDependencies": { | ||
"chai": "^3.5.0", | ||
"codecov": "^2.1.0", | ||
"eslint": "^3.19.0", | ||
"eslint-config-standard": "^10.2.0", | ||
"eslint-plugin-import": "^2.2.0", | ||
"eslint-plugin-node": "^4.2.2", | ||
"babel-plugin-transform-object-assign": "^6.22.0", | ||
"babel-preset-es2015": "^6.24.1", | ||
"babelify": "^7.3.0", | ||
"browserify": "^14.3.0", | ||
"browserify-shim": "^3.8.14", | ||
"bundle-collapser": "^1.2.1", | ||
"chai": "^4.0.1", | ||
"codecov": "^3.0.0", | ||
"eslint": "^4.8.0", | ||
"eslint-config-standard": "^10.2.1", | ||
"eslint-plugin-import": "^2.6.0", | ||
"eslint-plugin-node": "^5.0.0", | ||
"eslint-plugin-promise": "^3.5.0", | ||
"eslint-plugin-standard": "^3.0.1", | ||
"express": "^4.15.2", | ||
"mocha": "^3.2.0", | ||
"exorcist": "^0.4.0", | ||
"express": "^4.15.3", | ||
"gulp": "^3.9.1", | ||
"gulp-header": "^1.8.8", | ||
"gulp-if": "^2.0.2", | ||
"gulp-sourcemaps": "^2.6.0", | ||
"gulp-uglify": "^2.1.2", | ||
"gulp-util": "^3.0.8", | ||
"live-server": "^1.2.0", | ||
"mocha": "^4.0.0", | ||
"nock": "^9.0.13", | ||
"nyc": "^10.2.0", | ||
"proxyquire": "^1.7.11", | ||
"safe-buffer": "^5.0.1", | ||
"sinon": "^2.1.0", | ||
"sinon-chai": "^2.9.0" | ||
"node-zopfli": "^2.0.2", | ||
"nyc": "^11.0.2", | ||
"proxyquire": "^1.8.0", | ||
"safe-buffer": "^5.1.0", | ||
"sinon": "^4.0.0", | ||
"sinon-chai": "^2.14.0", | ||
"vinyl-buffer": "^1.0.0", | ||
"vinyl-fs": "^2.4.4", | ||
"vinyl-source-stream": "^1.1.0" | ||
} | ||
} |
@@ -11,3 +11,3 @@ # Datadog Tracer | ||
OpenTracing tracer implementation for Datadog in JavaScript. | ||
It is intended for use both on the server and (soon) in the browser. | ||
It is intended for use both on the server and in the browser. | ||
@@ -26,4 +26,20 @@ ## Installation | ||
*Not yet supported* | ||
The library supports CommonJS and AMD loaders and also exports globally as `DatadogTracer`. | ||
**NOTE:** If you want to use binary propagation, make sure to also include the minimal version of [protobuf.js](https://github.com/dcodeIO/protobuf.js/tree/master/dist/minimal) before this library. | ||
#### CDN | ||
```html | ||
<script src="//cdn.rawgit.com/rochdev/datadog-tracer-js/0.X.X/dist/datadog-tracer.min.js"></script> | ||
``` | ||
**NOTE:** Remember to replace the version tag with the exact [release](https://github.com/rochdev/datadog-tracer-js/tags) your project depends upon. | ||
#### Frontend | ||
```html | ||
<script src="node_modules/datadog-tracer/dist/datadog-tracer.min.js"></script> | ||
``` | ||
## Usage | ||
@@ -73,3 +89,3 @@ | ||
See the [example](example) folder for a more advanced version. | ||
See the [examples](examples) folder for more advanced examples. | ||
@@ -76,0 +92,0 @@ ## API Documentation |
'use strict' | ||
const protobuf = require('protobufjs/minimal') | ||
const SpanContext = require('../span_context') | ||
const TracerState = require('./state.proto.js').TracerState | ||
let TracerState | ||
if (protobuf) { | ||
protobuf.util.Long = require('long') | ||
protobuf.configure() | ||
TracerState = require('./state.proto.js').TracerState | ||
} | ||
class BinaryPropagator { | ||
inject (spanContext, carrier) { | ||
assertProtobuf() | ||
const err = TracerState.verify(spanContext) | ||
@@ -19,2 +28,4 @@ if (err) throw err | ||
extract (carrier) { | ||
assertProtobuf() | ||
let state | ||
@@ -41,2 +52,11 @@ | ||
function assertProtobuf () { | ||
if (!protobuf) { | ||
throw new Error( | ||
'Binary propagation is not available in your environment because Protobuf could not be found. ' + | ||
'Please make sure to import Protobuf when using binary propagation.' | ||
) | ||
} | ||
} | ||
module.exports = BinaryPropagator |
'use strict' | ||
const bignumJSON = require('json-bignum') | ||
const platform = require('./platform') | ||
const Long = require('long') | ||
@@ -11,6 +11,6 @@ class DatadogRecorder { | ||
const data = bignumJSON.stringify([[{ | ||
trace_id: new bignumJSON.BigNumber(spanContext.traceId.toString()), | ||
span_id: new bignumJSON.BigNumber(spanContext.spanId.toString()), | ||
parent_id: span._parentId ? new bignumJSON.BigNumber(span._parentId.toString()) : null, | ||
const data = stringify([[{ | ||
trace_id: spanContext.traceId, | ||
span_id: spanContext.spanId, | ||
parent_id: span._parentId || null, | ||
name: span._operationName, | ||
@@ -23,3 +23,3 @@ resource: span._tags.resource, | ||
start: Math.round(span._startTime * 1e6), | ||
duration: Math.round(span._duration * 1e6) | ||
duration: Math.max(Math.round(span._duration * 1e6), 1) | ||
}]]) | ||
@@ -38,2 +38,24 @@ | ||
function stringify (obj) { | ||
switch (typeof obj) { | ||
case 'object': | ||
if (Long.isLong(obj)) { | ||
return obj.toString() | ||
} else if (Array.isArray(obj)) { | ||
return '[' + obj.map(item => stringify(item)).join(',') + ']' | ||
} else if (obj !== null) { | ||
return '{' + Object.keys(obj) | ||
.map(key => `"${key}":` + stringify(obj[key])) | ||
.join(',') + '}' | ||
} | ||
return 'null' | ||
case 'string': | ||
return `"${obj}"` | ||
case 'number': | ||
case 'boolean': | ||
return String(obj) | ||
} | ||
} | ||
module.exports = DatadogRecorder |
@@ -24,2 +24,7 @@ 'use strict' | ||
this._endpoint = new Endpoint(endpoint || `${protocol}://${hostname}:${port}`) | ||
this._propagators = { | ||
[opentracing.FORMAT_TEXT_MAP]: new TextMapPropagator(), | ||
[opentracing.FORMAT_HTTP_HEADERS]: new TextMapPropagator(), | ||
[opentracing.FORMAT_BINARY]: new BinaryPropagator() | ||
} | ||
} | ||
@@ -37,3 +42,3 @@ | ||
_inject (spanContext, format, carrier) { | ||
getPropagator(format).inject(spanContext, carrier) | ||
this._propagators[format].inject(spanContext, carrier) | ||
return this | ||
@@ -43,3 +48,3 @@ } | ||
_extract (format, carrier) { | ||
return getPropagator(format).extract(carrier) | ||
return this._propagators[format].extract(carrier) | ||
} | ||
@@ -50,18 +55,2 @@ } | ||
function getPropagator (format) { | ||
let propagator | ||
switch (format) { | ||
case opentracing.FORMAT_HTTP_HEADERS: | ||
case opentracing.FORMAT_TEXT_MAP: | ||
propagator = new TextMapPropagator() | ||
break | ||
case opentracing.FORMAT_BINARY: | ||
propagator = new BinaryPropagator() | ||
break | ||
} | ||
return propagator | ||
} | ||
function getParent (references) { | ||
@@ -68,0 +57,0 @@ let parent = null |
'use strict' | ||
const apiCompatibilityChecks = require('opentracing/lib/test/api_compatibility').default | ||
const DatadogTracer = require('../src/tracer') | ||
const DatadogTracer = require('..') | ||
apiCompatibilityChecks(() => new DatadogTracer({ service: 'test' })) |
'use strict' | ||
const proxyquire = require('proxyquire') | ||
const Long = require('long') | ||
@@ -9,81 +10,137 @@ const TracerState = require('../../src/propagation/state.proto.js').TracerState | ||
beforeEach(() => { | ||
BinaryPropagator = require('../../src/propagation/binary') | ||
}) | ||
describe('when Protobuf is available', () => { | ||
beforeEach(() => { | ||
BinaryPropagator = require('../../src/propagation/binary') | ||
}) | ||
it('should inject the span context into the carrier', () => { | ||
const carrier = {} | ||
const spanContext = { | ||
traceId: new Long(123, 0, true), | ||
spanId: new Long(456, 0, true), | ||
sampled: true, | ||
baggageItems: { | ||
foo: 'bar', | ||
baz: 'qux' | ||
it('should inject the span context into the carrier', () => { | ||
const carrier = {} | ||
const spanContext = { | ||
traceId: new Long(123, 0, true), | ||
spanId: new Long(456, 0, true), | ||
sampled: true, | ||
baggageItems: { | ||
foo: 'bar', | ||
baz: 'qux' | ||
} | ||
} | ||
} | ||
const propagator = new BinaryPropagator() | ||
propagator.inject(spanContext, carrier) | ||
const propagator = new BinaryPropagator() | ||
propagator.inject(spanContext, carrier) | ||
const state = TracerState.toObject(TracerState.decode(carrier.buffer)) | ||
const state = TracerState.toObject(TracerState.decode(carrier.buffer)) | ||
expect(state).to.deep.equal(Object.assign({}, spanContext, { | ||
baggageItems: { | ||
foo: '"bar"', | ||
baz: '"qux"' | ||
expect(state).to.deep.equal(Object.assign({}, spanContext, { | ||
baggageItems: { | ||
foo: '"bar"', | ||
baz: '"qux"' | ||
} | ||
})) | ||
}) | ||
it('should extract a span context from the carrier', () => { | ||
const spanContext = { | ||
traceId: new Long(123, 0, true), | ||
spanId: new Long(456, 0, true), | ||
sampled: true, | ||
baggageItems: { | ||
foo: 'bar', | ||
baz: 'qux' | ||
} | ||
} | ||
})) | ||
}) | ||
const state = TracerState.create(Object.assign({}, spanContext, { | ||
baggageItems: { | ||
foo: '"bar"', | ||
baz: '"qux"' | ||
} | ||
})) | ||
const carrier = { | ||
buffer: TracerState.encode(state).finish() | ||
} | ||
const propagator = new BinaryPropagator() | ||
it('should extract a span context from the carrier', () => { | ||
const spanContext = { | ||
traceId: new Long(123, 0, true), | ||
spanId: new Long(456, 0, true), | ||
sampled: true, | ||
baggageItems: { | ||
foo: 'bar', | ||
baz: 'qux' | ||
expect(propagator.extract(carrier)).to.deep.equal(spanContext) | ||
}) | ||
it('should return null when the carrier does not contain a valid context', () => { | ||
const carrier = { | ||
buffer: null | ||
} | ||
} | ||
const state = TracerState.create(Object.assign({}, spanContext, { | ||
baggageItems: { | ||
foo: '"bar"', | ||
baz: '"qux"' | ||
const propagator = new BinaryPropagator() | ||
const spanContext = propagator.extract(carrier) | ||
expect(spanContext).to.be.null | ||
}) | ||
it('should throw when trying to inject an invalid span context', () => { | ||
const carrier = {} | ||
const spanContext = { | ||
traceId: 0, | ||
spanId: 0, | ||
sampled: 123, | ||
baggageItems: 'foo' | ||
} | ||
})) | ||
const carrier = { | ||
buffer: TracerState.encode(state).finish() | ||
} | ||
const propagator = new BinaryPropagator() | ||
expect(propagator.extract(carrier)).to.deep.equal(spanContext) | ||
const propagator = new BinaryPropagator() | ||
expect(() => { | ||
propagator.inject(spanContext, carrier) | ||
}).to.throw() | ||
}) | ||
}) | ||
it('should return null when the carrier does not contain a valid context', () => { | ||
const carrier = { | ||
buffer: null | ||
} | ||
describe('when Protobuf is not available', () => { | ||
beforeEach(() => { | ||
BinaryPropagator = proxyquire('../../src/propagation/binary', { | ||
'protobufjs/minimal': false | ||
}) | ||
}) | ||
const propagator = new BinaryPropagator() | ||
const spanContext = propagator.extract(carrier) | ||
it('should throw when trying to inject without Protobuf support', () => { | ||
const carrier = {} | ||
const spanContext = { | ||
traceId: new Long(123, 0, true), | ||
spanId: new Long(456, 0, true), | ||
sampled: true, | ||
baggageItems: { | ||
foo: 'bar', | ||
baz: 'qux' | ||
} | ||
} | ||
expect(spanContext).to.be.null | ||
}) | ||
const propagator = new BinaryPropagator() | ||
it('should throw when trying to inject an invalid span context', () => { | ||
const carrier = {} | ||
const spanContext = { | ||
traceId: 0, | ||
spanId: 0, | ||
sampled: 123, | ||
baggageItems: 'foo' | ||
} | ||
expect(() => { | ||
propagator.inject(spanContext, carrier) | ||
}).to.throw() | ||
}) | ||
const propagator = new BinaryPropagator() | ||
it('should throw when trying to extract without Protobuf support', () => { | ||
const spanContext = { | ||
traceId: new Long(123, 0, true), | ||
spanId: new Long(456, 0, true), | ||
sampled: true, | ||
baggageItems: { | ||
foo: 'bar', | ||
baz: 'qux' | ||
} | ||
} | ||
const state = TracerState.create(Object.assign({}, spanContext, { | ||
baggageItems: { | ||
foo: '"bar"', | ||
baz: '"qux"' | ||
} | ||
})) | ||
const carrier = { | ||
buffer: TracerState.encode(state).finish() | ||
} | ||
expect(() => { | ||
propagator.inject(spanContext, carrier) | ||
}).to.throw() | ||
const propagator = new BinaryPropagator() | ||
expect(() => { | ||
propagator.extract(carrier) | ||
}).to.throw() | ||
}) | ||
}) | ||
}) |
@@ -41,4 +41,2 @@ 'use strict' | ||
}) | ||
tracer = new Tracer({ service: 'service' }) | ||
}) | ||
@@ -69,2 +67,3 @@ | ||
tracer = new Tracer({ service: 'service' }) | ||
const testSpan = tracer.startSpan('name', fields) | ||
@@ -89,2 +88,3 @@ | ||
tracer = new Tracer({ service: 'service' }) | ||
tracer.startSpan('name', fields) | ||
@@ -105,2 +105,3 @@ | ||
tracer = new Tracer({ service: 'service' }) | ||
tracer.startSpan('name', fields) | ||
@@ -122,2 +123,3 @@ | ||
tracer = new Tracer({ service: 'service' }) | ||
tracer.startSpan('name', fields) | ||
@@ -136,2 +138,3 @@ | ||
tracer = new Tracer({ service: 'service' }) | ||
tracer.startSpan('name', fields) | ||
@@ -148,2 +151,3 @@ | ||
tracer = new Tracer({ service: 'service' }) | ||
tracer.on('error', e => { | ||
@@ -160,2 +164,3 @@ expect(e).to.equal(error) | ||
tracer = new Tracer({ service: 'service' }) | ||
tracer.inject(spanContext, opentracing.FORMAT_TEXT_MAP, carrier) | ||
@@ -169,2 +174,3 @@ | ||
tracer = new Tracer({ service: 'service' }) | ||
tracer.inject(spanContext, opentracing.FORMAT_HTTP_HEADERS, carrier) | ||
@@ -178,2 +184,3 @@ | ||
tracer = new Tracer({ service: 'service' }) | ||
tracer.inject(spanContext, opentracing.FORMAT_BINARY, carrier) | ||
@@ -188,2 +195,3 @@ | ||
tracer = new Tracer({ service: 'service' }) | ||
const spanContext = tracer.extract(opentracing.FORMAT_TEXT_MAP, carrier) | ||
@@ -198,2 +206,3 @@ | ||
tracer = new Tracer({ service: 'service' }) | ||
const spanContext = tracer.extract(opentracing.FORMAT_HTTP_HEADERS, carrier) | ||
@@ -208,2 +217,3 @@ | ||
tracer = new Tracer({ service: 'service' }) | ||
const spanContext = tracer.extract(opentracing.FORMAT_BINARY, carrier) | ||
@@ -210,0 +220,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
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
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
1026463
54
6183
97
8
34
2
7
5
+ Addedes6-promise@^4.1.1
+ Addedmersenne-twister@^1.1.0
+ Addedtypedarray@0.0.6
+ Added@protobufjs/codegen@2.0.4(transitive)
+ Added@types/long@4.0.2(transitive)
+ Added@types/node@22.9.1(transitive)
+ Addedes6-promise@4.2.8(transitive)
+ Addedlong@4.0.0(transitive)
+ Addedmersenne-twister@1.1.0(transitive)
+ Addedprotobufjs@6.11.4(transitive)
+ Addedtypedarray@0.0.6(transitive)
+ Addedundici-types@6.19.8(transitive)
- Removedjson-bignum@^0.0.3
- Removedrandombytes@^2.0.3
- Removed@protobufjs/codegen@1.0.8(transitive)
- Removed@types/long@3.0.32(transitive)
- Removed@types/node@7.0.12(transitive)
- Removedjson-bignum@0.0.3(transitive)
- Removedprotobufjs@6.7.3(transitive)
- Removedrandombytes@2.1.0(transitive)
- Removedsafe-buffer@5.2.1(transitive)
Updatedopentracing@^0.14.1
Updatedprotobufjs@^6.8.0