How to use the opentracing.FORMAT_TEXT_MAP function in opentracing

To help you get started, we’ve selected a few opentracing examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github opentracing-contrib / nginx-opentracing / example / zoo / node / opentracing-express.js View on Github external
const wireCtx =
        tracer.extract(opentracing.FORMAT_HTTP_HEADERS, req.headers);
    const pathname = url.parse(req.url).pathname;
    const span = tracer.startSpan(pathname, { childOf: wireCtx });
    span.logEvent('request_received');

    // include some useful tags on the trace
    span.setTag('http.method', req.method);
    span.setTag('span.kind', 'server');
    span.setTag('http.url', req.url);
    span.setTag('component', 'express');

    // include trace ID in headers so that we can debug slow requests we see in
    // the browser by looking up the trace ID found in response headers
    const responseHeaders = {};
    tracer.inject(span, opentracing.FORMAT_TEXT_MAP, responseHeaders);
    Object.keys(responseHeaders)
        .forEach(key => res.setHeader(key, responseHeaders[key]));

    // add the span to the request object for handlers to use
    Object.assign(req, { span });

    // finalize the span when the response is completed
    const endOld = res.end;
    res.end = function() {
      span.logEvent('request_finished');
      // Route matching often happens after the middleware is run. Try changing
      // the operation name
      // to the route matcher.
      const opName = (req.route && req.route.path) || pathname;
      span.setOperationName(opName);
      span.setTag('http.status_code', res.statusCode);
github moorara / microservices-demo / services / graphql / src / services / asset.js View on Github external
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, 'requester')
    span.setTag(opentracing.Tags.PEER_SERVICE, 'asset-service')
    span.setTag(opentracing.Tags.PEER_ADDRESS, this.conn.currentServer.url.host)

    const carrier = {}
    this.tracer.inject(span, opentracing.FORMAT_TEXT_MAP, carrier)
    const spanContext = JSON.stringify(carrier)

    // Core functionality
    try {
      startTime = Date.now()
      result = await func(spanContext)
    } catch (e) {
      err = e
      this.logger.error(err)
    } finally {
      latency = (Date.now() - startTime) / 1000
    }

    // Metrics
    const labelValues = { op: name, success: err ? 'false' : 'true' }
    this.histogram.observe(labelValues, latency)
github opentracing-contrib / javascript-express / src / middleware.js View on Github external
return (req, res, next) => {
    const wireCtx = tracer.extract(opentracing.FORMAT_HTTP_HEADERS, req.headers);
    const pathname = url.parse(req.url).pathname;
    const span = tracer.startSpan(pathname, {childOf: wireCtx});
    span.logEvent("request_received");

    // include some useful tags on the trace
    span.setTag("http.method", req.method);
    span.setTag("span.kind", "server");
    span.setTag("http.url", req.url);

    // include trace ID in headers so that we can debug slow requests we see in
    // the browser by looking up the trace ID found in response headers
    const responseHeaders = {};
    tracer.inject(span, opentracing.FORMAT_TEXT_MAP, responseHeaders);
    Object.keys(responseHeaders).forEach(key => res.setHeader(key, responseHeaders[key]));

    // add the span to the request object for handlers to use
    Object.assign(req, {span});

    // finalize the span when the response is completed
    const finishSpan = () => {
      span.logEvent("request_finished");
      // Route matching often happens after the middleware is run. Try changing the operation name
      // to the route matcher.
      const opName = (req.route && req.route.path) || pathname;
      span.setOperationName(opName);
      span.setTag("http.status_code", res.statusCode);
      if (res.statusCode >= 500) {
        span.setTag("error", true);
        span.setTag("sampling.priority", 1);
github jaegertracing / jaeger-client-node / test / samplers / exp_priority_sampler.js View on Github external
it('should not sample or finalize span after serializing context', () => {
    let span = tracer.startSpan('opName');
    let carrier = {};
    tracer.inject(span.context(), opentracing.FORMAT_TEXT_MAP, carrier);
    assert.isDefined(carrier['uber-trace-id']);
    assert.isFalse(span._spanContext.isSampled(), 'sampled');
    assert.isFalse(span._spanContext.samplingFinalized, 'finalized');
  });
github hawkular / hawkular-apm / tests / app / polyglot-opentracing / js / index.js View on Github external
function extractSpanContext(tracer, httpHeaders) {
    return tracer.extract(opentracing.FORMAT_TEXT_MAP, httpHeaders);
}
github DefinitelyTyped / DefinitelyTyped / types / jaeger-client / jaeger-client-tests.ts View on Github external
import * as opentracing from "opentracing";

const tracingConfig: jaegerClient.TracingConfig = {};
const tracingOptions: jaegerClient.TracingOptions = {};

const tracer = jaegerClient.initTracer(tracingConfig, tracingOptions);
jaegerClient.initTracerFromEnv(tracingConfig, tracingOptions);

const metrics = new jaegerClient.PrometheusMetricsFactory(promClient, "foo");

const textCodec = new jaegerClient.TextMapCodec({
    contextKey: 'trace-id',
    baggagePrefix: 'baggage-',
    urlEncoding: false,
});
tracer.registerInjector(opentracing.FORMAT_TEXT_MAP, textCodec);
tracer.registerExtractor(opentracing.FORMAT_TEXT_MAP, textCodec);

const zipkinB3TextMapCodec = new jaegerClient.ZipkinB3TextMapCodec({
    baggagePrefix: 'baggage-',
    urlEncoding: false,
});
tracer.registerInjector(opentracing.FORMAT_HTTP_HEADERS, zipkinB3TextMapCodec);
tracer.registerExtractor(opentracing.FORMAT_HTTP_HEADERS, zipkinB3TextMapCodec);
github elastic / apm-agent-js-core / test / opentracing / api_compatibility.js View on Github external
expect(function () {
              tracer.extract(ot.FORMAT_TEXT_MAP, textCarrier)
            }).not.toThrow(Error)
            expect(function () {
github hawkular / hawkular-apm / tests / app / polyglot-opentracing / js / index.js View on Github external
function createCarrier(tracer, span) {
    const carrier = {};
    tracer.inject(span.context(), opentracing.FORMAT_TEXT_MAP, carrier);
    return carrier;
}
github jaegertracing / jaeger-client-node / src / tracer.js View on Github external
this._metrics
    );
    this._debugThrottler = options.debugThrottler || new DefaultThrottler(false);
    this._injectors = {};
    this._extractors = {};

    let codecOptions = {
      contextKey: options.contextKey || null,
      baggagePrefix: options.baggagePrefix || null,
      urlEncoding: false,
      metrics: this._metrics,
    };

    let textCodec = new TextMapCodec(codecOptions);
    this.registerInjector(opentracing.FORMAT_TEXT_MAP, textCodec);
    this.registerExtractor(opentracing.FORMAT_TEXT_MAP, textCodec);

    codecOptions.urlEncoding = true;

    let httpCodec = new TextMapCodec(codecOptions);
    this.registerInjector(opentracing.FORMAT_HTTP_HEADERS, httpCodec);
    this.registerExtractor(opentracing.FORMAT_HTTP_HEADERS, httpCodec);

    let binaryCodec = new BinaryCodec();
    this.registerInjector(opentracing.FORMAT_BINARY, binaryCodec);
    this.registerExtractor(opentracing.FORMAT_BINARY, binaryCodec);

    const uuid = uuidv4();
    this._tags[constants.TRACER_CLIENT_ID_TAG_KEY] = uuid;
    this._process = {
      serviceName: serviceName,
      tags: Utils.convertObjectToTags(this._tags),
github instana / nodejs-sensor / packages / core / src / tracing / opentracing / Tracer.js View on Github external
'use strict';

var opentracing = require('opentracing');
var constants = require('../constants');
var Span = require('./Span');

var baggageKeyPrefix = 'x-instana-b-';

var valueEncoders = {};
valueEncoders[opentracing.FORMAT_TEXT_MAP] = identity;
valueEncoders[opentracing.FORMAT_HTTP_HEADERS] = encodeURIComponent;

var valueDecoders = {};
valueDecoders[opentracing.FORMAT_TEXT_MAP] = identity;
valueDecoders[opentracing.FORMAT_HTTP_HEADERS] = decodeURIComponent;

function Tracer(isActive) {
  opentracing.Tracer.apply(this, arguments);
  this._isActive = isActive;
}
module.exports = Tracer;

Tracer.prototype = Object.create(opentracing.Tracer.prototype);

Tracer.prototype._startSpan = function _startSpan(name, fields) {
  return new Span(this, name, fields);