How to use @opentelemetry/tracing - 10 common examples

To help you get started, we’ve selected a few @opentelemetry/tracing 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 GoogleCloudPlatform / esp-v2 / tests / endpoints / bookstore / http_bookstore.js View on Github external
express: {
      enabled: true,
      path: '@opentelemetry/plugin-express',
      ignoreLayersType: [
          "middleware",
          "request_handler",
      ],
    },
    http: {
      enabled: true,
      path: '@opentelemetry/plugin-http',
    }
  }
});
const exporter = new TraceExporter();
provider.addSpanProcessor(new BatchSpanProcessor(exporter));
provider.register();

// Registration. Use W3C trace context propagation with `traceparent` header.
opentelemetry.trace.setGlobalTracerProvider(provider);
opentelemetry.propagation.setGlobalPropagator(new HttpTraceContext());

// Load express afterwords.
var express = require('express');
var bodyParser = require('body-parser');
var swaggerTools = require('swagger-tools');


/**
 * @typedef {Object} InitializationOptions
 * @property {Boolean} log Log incoming requests.
 * @property {Object} swagger Swagger document object.
github open-telemetry / opentelemetry-js / examples / tracer-web / examples / document-load / index.js View on Github external
const webTracer = new WebTracer({
  plugins: [
    new DocumentLoad()
  ]
});
webTracer.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter()));

const webTracerWithZone = new WebTracer({
  scopeManager: new ZoneScopeManager(),
  plugins: [
    new DocumentLoad()
  ]
});
webTracerWithZone.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter()));
webTracerWithZone.addSpanProcessor(new SimpleSpanProcessor(new CollectorExporter()));

console.log('Current span is window', webTracerWithZone.getCurrentSpan() === window);

// example of keeping track of scope between async operations
const prepareClickEvent = () => {
  const url1 = 'https://raw.githubusercontent.com/open-telemetry/opentelemetry-js/master/package.json';
  const url2 = 'https://raw.githubusercontent.com/open-telemetry/opentelemetry-js/master/packages/opentelemetry-web/package.json';

  const element = document.getElementById('button1');
  let mainSpan = webTracerWithZone.startSpan('main-span');
  webTracerWithZone.bind(element, mainSpan);

  const onClick = () => {
    const span1 = webTracerWithZone.startSpan(`files-series-info-1`, {
      parent: webTracerWithZone.getCurrentSpan()
    });
github open-telemetry / opentelemetry-js / examples / tracer-web / examples / document-load / index.js View on Github external
import { CollectorExporter } from '@opentelemetry/exporter-collector'

const webTracer = new WebTracer({
  plugins: [
    new DocumentLoad()
  ]
});
webTracer.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter()));

const webTracerWithZone = new WebTracer({
  scopeManager: new ZoneScopeManager(),
  plugins: [
    new DocumentLoad()
  ]
});
webTracerWithZone.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter()));
webTracerWithZone.addSpanProcessor(new SimpleSpanProcessor(new CollectorExporter()));

console.log('Current span is window', webTracerWithZone.getCurrentSpan() === window);

// example of keeping track of scope between async operations
const prepareClickEvent = () => {
  const url1 = 'https://raw.githubusercontent.com/open-telemetry/opentelemetry-js/master/package.json';
  const url2 = 'https://raw.githubusercontent.com/open-telemetry/opentelemetry-js/master/packages/opentelemetry-web/package.json';

  const element = document.getElementById('button1');
  let mainSpan = webTracerWithZone.startSpan('main-span');
  webTracerWithZone.bind(element, mainSpan);

  const onClick = () => {
    const span1 = webTracerWithZone.startSpan(`files-series-info-1`, {
      parent: webTracerWithZone.getCurrentSpan()
github open-telemetry / opentelemetry-js / examples / tracer-web / examples / document-load / index.js View on Github external
import { ConsoleSpanExporter, SimpleSpanProcessor } from '@opentelemetry/tracing';
import { WebTracer } from '@opentelemetry/web';
import { DocumentLoad } from '@opentelemetry/plugin-document-load';
import { ZoneScopeManager } from '@opentelemetry/scope-zone';
import { CollectorExporter } from '@opentelemetry/exporter-collector'

const webTracer = new WebTracer({
  plugins: [
    new DocumentLoad()
  ]
});
webTracer.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter()));

const webTracerWithZone = new WebTracer({
  scopeManager: new ZoneScopeManager(),
  plugins: [
    new DocumentLoad()
  ]
});
webTracerWithZone.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter()));
webTracerWithZone.addSpanProcessor(new SimpleSpanProcessor(new CollectorExporter()));

console.log('Current span is window', webTracerWithZone.getCurrentSpan() === window);

// example of keeping track of scope between async operations
const prepareClickEvent = () => {
  const url1 = 'https://raw.githubusercontent.com/open-telemetry/opentelemetry-js/master/package.json';
  const url2 = 'https://raw.githubusercontent.com/open-telemetry/opentelemetry-js/master/packages/opentelemetry-web/package.json';
github open-telemetry / opentelemetry-js / examples / basic-tracer-node / index.js View on Github external
// Initialize an exporter depending on how we were started
let exporter;

const EXPORTER = process.env.EXPORTER || '';
if (EXPORTER.toLowerCase().startsWith('z')) {
  exporter = new ZipkinExporter(options);
} else if (EXPORTER.toLowerCase().startsWith('j')) {
  exporter = new JaegerExporter(options);
} else {
  exporter = new CollectorExporter(options);
}

const tracer = new BasicTracer();

// Configure span processor to send spans to the provided exporter
tracer.addSpanProcessor(new SimpleSpanProcessor(exporter));

// Initialize the OpenTelemetry APIs to use the BasicTracer bindings
opentelemetry.initGlobalTracer(tracer);

// Create a span. A span must be closed.
const span = opentelemetry.getTracer().startSpan('main');
for (let i = 0; i < 10; i++) {
  doWork(span);
}
// Be sure to end the span.
span.end();

// flush and close the connection.
exporter.shutdown();

function doWork(parent) {
github open-telemetry / opentelemetry-js / examples / mysql / setup.js View on Github external
function setupTracerAndExporters(service) {
  const tracer = new NodeTracer({
    plugins: {
      mysql: {
        enabled: true,
        path: "@opentelemetry/plugin-mysql"
      },
      http: {
        enabled: true,
        path: "@opentelemetry/plugin-http"
      }
    }
  });

  tracer.addSpanProcessor(new SimpleSpanProcessor(new ZipkinExporter({
    serviceName: service,
  })));
  tracer.addSpanProcessor(new SimpleSpanProcessor(new JaegerExporter({
    serviceName: service,
  })));

  // Initialize the OpenTelemetry APIs to use the BasicTracer bindings
  opentelemetry.initGlobalTracer(tracer);
}
github open-telemetry / opentelemetry-js / examples / mysql / setup.js View on Github external
plugins: {
      mysql: {
        enabled: true,
        path: "@opentelemetry/plugin-mysql"
      },
      http: {
        enabled: true,
        path: "@opentelemetry/plugin-http"
      }
    }
  });

  tracer.addSpanProcessor(new SimpleSpanProcessor(new ZipkinExporter({
    serviceName: service,
  })));
  tracer.addSpanProcessor(new SimpleSpanProcessor(new JaegerExporter({
    serviceName: service,
  })));

  // Initialize the OpenTelemetry APIs to use the BasicTracer bindings
  opentelemetry.initGlobalTracer(tracer);
}
github open-telemetry / opentelemetry-js / examples / basic-tracer-node / multi_exporter.js View on Github external
const jaegerExporter = new JaegerExporter({
  serviceName: 'basic-service',
});
const collectorExporter = new CollectorExporter({serviceName: 'basic-service'});

// It is recommended to use this BatchSpanProcessor for better performance
// and optimization, especially in production.
tracer.addSpanProcessor(new BatchSpanProcessor(zipkinExporter, {
  bufferSize: 10 // This is added for example, default size is 100.
}));

// It is recommended to use SimpleSpanProcessor in case of Jaeger exporter as
// it's internal client already handles the spans with batching logic.
tracer.addSpanProcessor(new SimpleSpanProcessor(jaegerExporter));

tracer.addSpanProcessor(new SimpleSpanProcessor(collectorExporter));

// Initialize the OpenTelemetry APIs to use the BasicTracer bindings
opentelemetry.initGlobalTracer(tracer);

// Create a span. A span must be closed.
const span = opentelemetry.getTracer().startSpan('main');
for (let i = 0; i < 10; i++) {
  doWork(span);
}
// Be sure to end the span.
span.end();

// flush and close the connection.
zipkinExporter.shutdown();
jaegerExporter.shutdown();
collectorExporter.shutdown();
github open-telemetry / opentelemetry-js / examples / basic-tracer-node / multi_exporter.js View on Github external
const zipkinExporter = new ZipkinExporter({serviceName: 'basic-service'});
const jaegerExporter = new JaegerExporter({
  serviceName: 'basic-service',
});
const collectorExporter = new CollectorExporter({serviceName: 'basic-service'});

// It is recommended to use this BatchSpanProcessor for better performance
// and optimization, especially in production.
tracer.addSpanProcessor(new BatchSpanProcessor(zipkinExporter, {
  bufferSize: 10 // This is added for example, default size is 100.
}));

// It is recommended to use SimpleSpanProcessor in case of Jaeger exporter as
// it's internal client already handles the spans with batching logic.
tracer.addSpanProcessor(new SimpleSpanProcessor(jaegerExporter));

tracer.addSpanProcessor(new SimpleSpanProcessor(collectorExporter));

// Initialize the OpenTelemetry APIs to use the BasicTracer bindings
opentelemetry.initGlobalTracer(tracer);

// Create a span. A span must be closed.
const span = opentelemetry.getTracer().startSpan('main');
for (let i = 0; i < 10; i++) {
  doWork(span);
}
// Be sure to end the span.
span.end();

// flush and close the connection.
zipkinExporter.shutdown();
github open-telemetry / opentelemetry-js / examples / dns / setup.js View on Github external
}
      }
  });

  let exporter;
  if (EXPORTER.toLowerCase().startsWith('z')) {
    exporter = new ZipkinExporter({
      serviceName: service,
    });
  } else {
    exporter = new JaegerExporter({
      serviceName: service,
    });
  }

  tracer.addSpanProcessor(new SimpleSpanProcessor(exporter));

  // Initialize the OpenTelemetry APIs to use the BasicTracer bindings
  opentelemetry.initGlobalTracer(tracer);
}

@opentelemetry/tracing

OpenTelemetry Tracing

Apache-2.0
Latest version published 3 years ago

Package Health Score

67 / 100
Full package analysis