Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('should have http_request_handler metrics', () => {
const reporter = new PrometheusReporter({
ignoreTags: {
[Tags.HTTP_URL]: /bar/
}
})
const tracer = new Tracer('my-service', [reporter])
const span1 = tracer.startSpan('http_request')
span1.setTag(Tags.HTTP_URL, 'http://127.0.0.1/foo')
span1.setTag(Tags.HTTP_METHOD, 'GET')
span1.setTag(Tags.HTTP_STATUS_CODE, 200)
span1.setTag(Tags.SPAN_KIND, Tags.SPAN_KIND_RPC_SERVER)
clock.tick(100)
span1.finish()
// will be ignored
const span2 = tracer.startSpan('http_request')
span2.setTag(Tags.HTTP_URL, 'http://127.0.0.1/bar')
span2.setTag(Tags.HTTP_METHOD, 'GET')
span2.setTag(Tags.HTTP_STATUS_CODE, 200)
span2.setTag(Tags.SPAN_KIND, Tags.SPAN_KIND_RPC_SERVER)
clock.tick(300)
span2.finish()
const labelStr1 = `parent_service="${PrometheusReporter.LABEL_PARENT_SERVICE_UNKNOWN}",name="http_request"`
const labelStr2 = `parent_service="${PrometheusReporter.LABEL_PARENT_SERVICE_UNKNOWN}",method="GET",code="200"`
expect(reporter.metrics()).to.be.equal(dedent`
if (!parent || followsFromIsParent) {
parent = ref.referencedContext();
break;
}
} else if (ref.type() === opentracing.REFERENCE_FOLLOWS_FROM) {
if (!parent) {
parent = ref.referencedContext();
followsFromIsParent = true;
}
}
}
let ctx: SpanContext;
let internalTags: any = {};
let hasValidParent = false;
const isRpcServer = Boolean(userTags && userTags[otTags.SPAN_KIND] === otTags.SPAN_KIND_RPC_SERVER);
if (!parent || !parent.isValid) {
if (this._traceId128bit) {
let randomId = Utils.getRandom128();
ctx = new SpanContext(randomId, randomId.slice(-8));
} else {
let randomId = Utils.getRandom64();
ctx = new SpanContext(randomId, randomId);
}
if (parent) {
// fake parent, doesn't contain a parent trace-id, but may contain debug-id/baggage
if (parent.isDebugIDContainerOnly() && this._isDebugAllowed(operationName)) {
ctx._setSampled(true);
ctx._setDebug(true);
internalTags[constants.JAEGER_DEBUG_HEADER] = parent.debugId;
}
// baggage that could have been passed via `jaeger-baggage` header
const prometheusReporter = new PrometheusReporter()
const httpRequestDurationSeconds = prometheusReporter._metricshttpRequestDurationSeconds()
const metricsStub = {
observe: this.sandbox.spy()
}
this.sandbox.stub(httpRequestDurationSeconds, 'labels').callsFake(() => metricsStub)
// generate data
const tracer = new Tracer('service')
const span = tracer.startSpan('http_request')
span.setTag(Tags.HTTP_METHOD, 'GET')
span.setTag(Tags.HTTP_STATUS_CODE, 200)
span.setTag(Tags.SPAN_KIND, Tags.SPAN_KIND_RPC_SERVER)
clock.tick(100)
span.finish()
prometheusReporter.reportFinish(span)
// assert
expect(httpRequestDurationSeconds.labels).to.have.callCount(1)
expect(httpRequestDurationSeconds.labels)
.to.be.calledWith(PrometheusReporter.LABEL_PARENT_SERVICE_UNKNOWN, 'GET', 200)
expect(metricsStub.observe).to.have.callCount(1)
expect(metricsStub.observe).to.be.calledWith(0.1)
})
const tracer = new Tracer('my-service', [reporter])
const span1 = tracer.startSpan('http_request')
span1.setTag(Tags.HTTP_URL, 'http://127.0.0.1/foo')
span1.setTag(Tags.HTTP_METHOD, 'GET')
span1.setTag(Tags.HTTP_STATUS_CODE, 200)
span1.setTag(Tags.SPAN_KIND, Tags.SPAN_KIND_RPC_SERVER)
clock.tick(100)
span1.finish()
// will be ignored
const span2 = tracer.startSpan('http_request')
span2.setTag(Tags.HTTP_URL, 'http://127.0.0.1/bar')
span2.setTag(Tags.HTTP_METHOD, 'GET')
span2.setTag(Tags.HTTP_STATUS_CODE, 200)
span2.setTag(Tags.SPAN_KIND, Tags.SPAN_KIND_RPC_SERVER)
clock.tick(300)
span2.finish()
const labelStr1 = `parent_service="${PrometheusReporter.LABEL_PARENT_SERVICE_UNKNOWN}",name="http_request"`
const labelStr2 = `parent_service="${PrometheusReporter.LABEL_PARENT_SERVICE_UNKNOWN}",method="GET",code="200"`
expect(reporter.metrics()).to.be.equal(dedent`
# HELP operation_duration_seconds Duration of operations in second
# TYPE operation_duration_seconds histogram
operation_duration_seconds_bucket{le="0.005",${labelStr1}} 0
operation_duration_seconds_bucket{le="0.01",${labelStr1}} 0
operation_duration_seconds_bucket{le="0.025",${labelStr1}} 0
operation_duration_seconds_bucket{le="0.05",${labelStr1}} 0
operation_duration_seconds_bucket{le="0.1",${labelStr1}} 1
operation_duration_seconds_bucket{le="0.25",${labelStr1}} 1
operation_duration_seconds_bucket{le="0.5",${labelStr1}} 1
const prometheusReporter = new PrometheusReporter()
const httpRequestDurationSeconds = prometheusReporter._metricshttpRequestDurationSeconds()
const metricsStub = {
observe: this.sandbox.spy()
}
this.sandbox.stub(httpRequestDurationSeconds, 'labels').callsFake(() => metricsStub)
// generate data
const tracer = new Tracer('service')
const span = tracer.startSpan('http_request')
span.setTag(Tags.HTTP_METHOD, 'GET')
span.setTag(Tags.HTTP_STATUS_CODE, 200)
span.setTag(Tags.SPAN_KIND_RPC_SERVER, false) // or not set
clock.tick(100)
span.finish()
prometheusReporter.reportFinish(span)
// assert
expect(httpRequestDurationSeconds.labels).to.have.callCount(0)
expect(metricsStub.observe).to.have.callCount(0)
})
})
return () => {
let traceSpan: Span;
// NOTE: OpenTracing type definitions at
//
const parentSpanContext = tracer.extract(FORMAT_HTTP_HEADERS, headers);
if (
parentSpanContext &&
parentSpanContext.toSpanId &&
parentSpanContext.toSpanId().length > 0
) {
traceSpan = tracer.startSpan(operation, {
childOf: parentSpanContext,
tags: {
[Tags.SPAN_KIND]: Tags.SPAN_KIND_RPC_SERVER,
[Tags.COMPONENT]: component
}
});
} else {
traceSpan = tracer.startSpan(operation, {
tags: {
[Tags.SPAN_KIND]: Tags.SPAN_KIND_RPC_SERVER,
[Tags.COMPONENT]: component
}
});
}
return traceSpan;
};
}
module.exports = function processRequest(options, request, response) {
this.emit('start', request);
const parentSpanContext = tracer.extract(
FORMAT_HTTP_HEADERS,
request.headers
);
const spanOptions = parentSpanContext ? { childOf: parentSpanContext } : {};
const span = tracer.startSpan('compose_page', spanOptions);
span.addTags({
[Tags.HTTP_URL]: request.url,
[Tags.SPAN_KIND]: Tags.SPAN_KIND_RPC_SERVER
});
const {
fetchContext,
fetchTemplate,
parseTemplate,
filterResponseHeaders,
maxAssetLinks,
amdLoaderUrl
} = options;
const asyncStream = new AsyncStream();
asyncStream.once('plugged', () => {
asyncStream.end();
});
const parentSpan = tracer.startSpan('http_request')
tracer.inject(parentSpan, headers)
const app = express()
app.get('/', (req, res) => res.send('ok'))
const result = await request(app)
.get('/')
.headers(headers)
.expect(200)
.end()
expect(cls.startRootSpan).to.be.calledWith(tracer, instrumentation.OPERATION_NAME, {
childOf: parentSpan.context(),
tags: {
[Tags.SPAN_KIND]: Tags.SPAN_KIND_RPC_SERVER,
[Tags.HTTP_URL]: `http://127.0.0.1:${result.request.uri.port}/`,
[Tags.HTTP_METHOD]: 'GET'
}
})
})
const spans = parentSpanContexts.map((parentSpanContext, key) =>
cls.startRootSpan(tracers[key], OPERATION_NAME, {
childOf: parentSpanContext,
tags: {
[Tags.SPAN_KIND]: Tags.SPAN_KIND_RPC_SERVER,
[Tags.HTTP_URL]: req.url,
[Tags.HTTP_METHOD]: req.method
}
}))
const spans = parentSpanContexts.map((parentSpanContext, key) =>
cls.startRootSpan(tracers[key], OPERATION_NAME, {
childOf: parentSpanContext,
tags: {
[Tags.SPAN_KIND]: Tags.SPAN_KIND_RPC_SERVER,
[Tags.HTTP_URL]: url,
[Tags.HTTP_METHOD]: req.method
}
}))
debug(`Operation started ${OPERATION_NAME}`, {