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 start and finish span with https', async () => {
// WARNING: nock doesn't work well with https instrumentation
// create real request
await request('https://risingstack.com')
expect(cls.startChildSpan).to.be.calledWith(tracer, instrumentation.OPERATION_NAME, {
tags: {
[Tags.SPAN_KIND]: Tags.SPAN_KIND_RPC_CLIENT,
[Tags.HTTP_URL]: 'https://risingstack.com:443/',
[Tags.HTTP_METHOD]: 'GET'
}
})
expect(mockChildSpan.setTag).to.have.calledWith(Tags.HTTP_STATUS_CODE, 200)
expect(mockChildSpan.finish).to.have.callCount(1)
})
// generate data
const parentTracer = new Tracer('parent-service')
const tracer = new Tracer('service')
const parentSpan1 = parentTracer.startSpan('parent-operation')
const span1 = tracer.startSpan('http_request', { childOf: parentSpan1 })
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()
const parentSpan2 = parentTracer.startSpan('parent-operation')
const span2 = tracer.startSpan('http_request', { childOf: parentSpan2 })
span2.setTag(Tags.HTTP_METHOD, 'POST')
span2.setTag(Tags.HTTP_STATUS_CODE, 201)
span2.setTag(Tags.SPAN_KIND, Tags.SPAN_KIND_RPC_SERVER)
clock.tick(300)
span2.finish()
prometheusReporter.reportFinish(span1)
prometheusReporter.reportFinish(span2)
// assert
expect(httpRequestDurationSeconds.labels).to.have.callCount(2)
expect(httpRequestDurationSeconds.labels).to.be.calledWith('parent-service', 'POST', 201)
expect(metricsStub.observe).to.have.callCount(2)
expect(metricsStub.observe).to.be.calledWith(0.1)
expect(metricsStub.observe).to.be.calledWith(0.3)
})
// FIXME: should be undefined, but the dummy tracer returns an empty span context
const childOf = new SpanContext()
expect(cls.startRootSpan).to.be.calledWith(tracer, instrumentation.OPERATION_NAME, {
childOf,
tags: {
[Tags.SPAN_KIND]: Tags.SPAN_KIND_RPC_SERVER,
[Tags.HTTP_URL]: '/',
[Tags.HTTP_METHOD]: 'GET'
}
})
expect(mockSpan.log).to.be.calledWith({ peerRemoteAddress: '::ffff:127.0.0.1' })
expect(mockSpan.setTag).to.be.calledWith(instrumentation.TAG_REQUEST_PATH, '/')
expect(mockSpan.setTag).to.be.calledWith(Tags.HTTP_STATUS_CODE, 200)
expect(mockSpan.finish).to.have.callCount(1)
})
const server = http.createServer((req, res) => {
// Instrumentation
const requestSpan = metricsTracer.startSpan('http_request', {
childOf: metricsTracer.extract(FORMAT_HTTP_HEADERS, req.headers)
})
const headers = {}
metricsTracer.inject(requestSpan, FORMAT_HTTP_HEADERS, headers)
requestSpan.setTag(Tags.HTTP_URL, req.url)
requestSpan.setTag(Tags.HTTP_METHOD, req.method || 'GET')
requestSpan.setTag(Tags.HTTP_STATUS_CODE, 200)
requestSpan.setTag(Tags.SPAN_KIND_RPC_CLIENT, true)
// Dummy router: GET /metrics
if (req.url === '/metrics') {
requestSpan.finish()
res.writeHead(200, {
'Content-Type': MetricsTracer.PrometheusReporter.Prometheus.register.contentType
})
res.end(prometheusReporter.metrics())
return
}
// My child operation like DB access
const childOperationSpan = metricsTracer.startSpan('my_operation', {
childOf: requestSpan
it('should set error tag for > 3xx status codes', async () => {
const server = restify.createServer()
server.get('/', (req, res) => {
res.statusCode = 400
res.send('ok')
})
await request(server)
.get('/')
.expect(400)
.end()
expect(mockSpan.setTag).to.be.calledWith(Tags.HTTP_STATUS_CODE, 400)
expect(mockSpan.setTag).to.be.calledWith(Tags.ERROR, true)
expect(mockSpan.finish).to.have.callCount(1)
})
})
_reportHttpRequestFinish (span) {
assert(span instanceof Span, 'span is required')
this._metricshttpRequestDurationSeconds()
.labels(
PrometheusReporter.getParentService(span),
span.getTag(Tags.HTTP_METHOD),
span.getTag(Tags.HTTP_STATUS_CODE)
)
.observe(span.duration() / 1000)
}
.then( data => {
span.setTag(Tags.HTTP_STATUS_CODE, 200)
span.finish();
})
.catch( err => {
spans.forEach((span) => span.setTag(Tags.HTTP_STATUS_CODE, res.statusCode))
endTargetSpan: function(statusCode) {
if (process.env.EDGEMICRO_OPENTRACE) {
try {
responsespan.log({
'event': 'target_end'
});
responsespan.setTag(Tags.HTTP_STATUS_CODE, statusCode);
responsespan.finish();
} catch (err) {}
}
},
startTargetSpan: function(correlation_id, targetHostname) {