Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
let clientCallback = (err, res, headers, body) => {
assert.isNull(err);
assert.equal(reporter.spans.length, 2);
// the first span to be reported is the server span
let serverSpan = reporter.spans[0];
// the second span to be reported is the client span
let clientSpan = reporter.spans[1];
let serverSpanTags = {};
serverSpanTags[opentracing.Tags.PEER_SERVICE] = 'echo';
serverSpanTags[opentracing.Tags.SPAN_KIND] = opentracing.Tags.SPAN_KIND_RPC_SERVER;
serverSpanTags['as'] = o.as;
// TODO(oibe) the port for the client request ephemeral, and I don't know how to get it, or if I can.
let clientSpanTags = {};
clientSpanTags[opentracing.Tags.PEER_SERVICE] = 'server';
clientSpanTags[opentracing.Tags.SPAN_KIND] = opentracing.Tags.SPAN_KIND_RPC_CLIENT;
assert.isTrue(TestUtils.hasTags(serverSpan, serverSpanTags));
assert.isTrue(TestUtils.hasTags(clientSpan, clientSpanTags));
assert.equal(serverSpan.context().parentIdStr, clientSpan.context().spanIdStr);
// If context exists then the following conditions are true
// else the following conditons are false
assert.equal(serverSpan.context().traceIdStr === originalSpan.context().traceIdStr, !!o.context);
assert.equal(clientSpan.context().traceIdStr === originalSpan.context().traceIdStr, !!o.context);
handleRequest(isStartRequest: boolean, traceRequest: any, serverSpan: Span): void {
if (isStartRequest) {
serverSpan.setBaggageItem(constants.BAGGAGE_KEY, traceRequest.baggage);
if (traceRequest.sampled) {
serverSpan.setTag(opentracing.Tags.SAMPLING_PRIORITY, 1);
}
}
// do async call to prepareResponse
return this.prepareResponse(traceRequest.serverRole, traceRequest.downstream, serverSpan);
}
async exec (context, name, func) {
let err, result
let startTime, latency
// https://opentracing-javascript.surge.sh/interfaces/spanoptions.html
// https://github.com/opentracing/specification/blob/master/semantic_conventions.md
const span = this.tracer.startSpan(name, { childOf: context.span }) // { childOf: context.span.context() }
span.setTag(opentracing.Tags.SPAN_KIND, 'client')
span.setTag(opentracing.Tags.PEER_SERVICE, 'switch-service')
span.setTag(opentracing.Tags.PEER_ADDRESS, this.serviceAddr)
const carrier = {}
this.tracer.inject(span, opentracing.FORMAT_TEXT_MAP, carrier)
const metadata = new grpc.Metadata()
metadata.add('span.context', JSON.stringify(carrier))
// Core functionality
try {
startTime = Date.now()
result = await func(metadata)
} catch (e) {
err = e
this.logger.error(err)
} finally {
latency = (Date.now() - startTime) / 1000
// Metrics
const labelValues = {
httpVersion: req.httpVersion,
method: req.method,
url: url,
statusCode: res.statusCode
}
this.histogram.observe(labelValues, duration)
this.summary.observe(labelValues, duration)
// Traces
// https://github.com/opentracing/specification/blob/master/semantic_conventions.md
span.setTag('http.version', req.httpVersion)
span.setTag(opentracing.Tags.HTTP_METHOD, req.method)
span.setTag(opentracing.Tags.HTTP_URL, url)
span.setTag(opentracing.Tags.HTTP_STATUS_CODE, res.statusCode)
span.finish()
}
const express = require("express");
const fetch = require("node-fetch");
const ZipkinJavascriptOpentracing = require("../../lib/index");
const { recorder } = require("../recorder");
const availableTags = require("opentracing").Tags;
const app = express();
const tracer = new ZipkinJavascriptOpentracing({
serviceName: "My Client",
recorder,
kind: "client"
});
app.use(function zipkinExpressMiddleware(req, res, next) {
console.log("client middleware start");
setTimeout(() => {
const headers = {};
const span = tracer.startSpan("Client Span");
span.setTag(availableTags.PEER_ADDRESS, "127.0.0.1:8081");
AUTH0_CHANNEL: constants.TAG_AUTH0_CHANNEL,
AUTH0_HOSTNAME: constants.TAG_AUTH0_HOSTNAME,
AUTH0_PURPOSE: constants.TAG_AUTH0_PURPOSE,
AUTH0_REQUEST_ID: constants.TAG_AUTH0_REQUEST_ID,
AUTH0_WORKER_QUEUE: constants.TAG_AUTH0_WORKER_QUEUE,
AUTH0_WORKER_STRATEGY: constants.TAG_AUTH0_WORKER_STRATEGY,
AUTH0_WORKER_REQUEUE: constants.TAG_AUTH0_WORKER_REQUEUE,
AUTH0_JOB_ID: constants.TAG_AUTH0_JOB_ID,
AUTH0_JOB_TYPE: constants.TAG_AUTH0_JOB_TYPE,
AUTH0_CLIENT_ID: constants.TAG_AUTH0_CLIENT_ID,
AUTH0_CONNECTION_NAME: constants.TAG_AUTH0_CONNECTION_NAME,
AUTH0_IS_RETRY: constants.TAG_AUTH0_IS_RETRY,
AUTH0_RETRIES: constants.TAG_AUTH0_RETRIES,
HTTP_PATH: 'http.path',
}, tracing.Tags);
class SpanWrapper {
constructor(span, tracer) {
this._span = span;
this._tracer = tracer;
}
getSpan() {
return this._span;
}
finish(finishTime) {
this._span.finish(finishTime);
}
setTag(key, value) {
trackError(error: Error, msg: string) {
this.rootSpan.setTag(opentracing.Tags.ERROR, true);
this.rootSpan.setTag("message", error.message);
this.rootSpan.setTag("stack", error.stack);
this.rootSpan.setTag("event", "error");
this.log(msg || error.message);
}
app.get('/', (req, res) => {
const span = tracer.startSpan('http_server')
span.setTag(opentracing.Tags.HTTP_URL, `${req.protocol}://${req.hostname}${req.originalUrl}`)
span.setTag(opentracing.Tags.HTTP_METHOD, req.method)
span.setTag('request_path', req.route.path)
span.setTag('request_id', req.headers['x-request-id'])
const requestOptions = {
headers: { 'trace-span-context': span.context().toString() },
json: true
}
Promise.all([
request(Object.assign({ uri: 'http://localhost:3003/site/somkiat' }, requestOptions)),
request(Object.assign({ uri: 'http://localhost:3003/site/skooldio' }, requestOptions))
])
.then((sites) => {
span.setTag(opentracing.Tags.HTTP_STATUS_CODE, 200)
res.json({ sites })
hemera.ext('onError', (hemera, payload, error, next) => {
const span = hemera[jaegerContextKey]
span.setTag(Opentracing.Tags.ERROR, true)
span.log({
event: 'error',
'error.object': error,
message: error.message,
stack: error.stack
})
next()
})
const {
Annotation,
BatchRecorder,
ExplicitContext,
TraceId,
option: { Some, None },
Tracer,
InetAddress,
sampler,
jsonEncoder
} = require("zipkin");
const { HttpLogger } = require("zipkin-transport-http");
const availableTags = require("opentracing").Tags;
const {
FORMAT_BINARY,
FORMAT_TEXT_MAP,
FORMAT_HTTP_HEADERS
} = require("opentracing");
const { JSON_V2 } = jsonEncoder;
const HttpHeaders = {
TraceId: "x-b3-traceid",
ParentSpanId: "x-b3-parentspanid",
SpanId: "x-b3-spanid",
Sampled: "x-b3-sampled"
};
const startSpanAnnotation = {
client: Annotation.ClientSend,