Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"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.
*/
/**
* Creates an Express.js application which implements a Bookstore
* API defined in `swagger.json`.
'use strict';
const opentelemetry = require('@opentelemetry/api');
const { NodeTracerProvider } = require('@opentelemetry/node');
const { BatchSpanProcessor } = require('@opentelemetry/tracing');
const { TraceExporter } = require('@google-cloud/opentelemetry-cloud-trace-exporter');
const { AlwaysOnSampler, AlwaysOffSampler, ParentOrElseSampler } = require("@opentelemetry/core");
const { HttpTraceContext } = require("@opentelemetry/core");
// Initialize the OpenTelemetry APIs to use the NodeTracerProvider bindings
const provider = new NodeTracerProvider({
// TODO(nareddyt): Only sample when the incoming request creates a new span.
// Otherwise don't make new spans, but propagate the context.
// sampler: new ParentOrElseSampler(new AlwaysOffSampler()),
sampler: new AlwaysOnSampler(),
plugins: {
express: {
enabled: true,
path: '@opentelemetry/plugin-express',
ignoreLayersType: [
"middleware",
"request_handler",
],
},
http: {
enabled: true,
path: '@opentelemetry/plugin-http',
}
}
});
const exporter = new TraceExporter();
const opentelemetry = require('@opentelemetry/core');
const config = require('./setup');
/**
* The trace instance needs to be initialized first, if you want to enable
* automatic tracing for built-in plugins (gRPC in this case).
*/
config.setupTracerAndExporters('grpc-client-service');
const grpc = require('grpc');
const messages = require('./helloworld_pb');
const services = require('./helloworld_grpc_pb');
const PORT = 50051;
const tracer = opentelemetry.getTracer();
/** A function which makes requests and handles response. */
function main() {
// span corresponds to outgoing requests. Here, we have manually created
// the span, which is created to track work that happens outside of the
// request lifecycle entirely.
const span = tracer.startSpan('client.js:main()');
tracer.withSpan(span, () => {
console.log('Client traceId ', span.context().traceId);
const client = new services.GreeterClient(
`localhost:${PORT}`,
grpc.credentials.createInsecure()
);
const request = new messages.HelloRequest();
let user;
if (process.argv.length >= 3) {
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);
}
const tracer = new NodeTracer();
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);
}
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) {
// Start another span. In this example, the main method already started a
// span, so that'll be the parent span, and this will be a child span.
const span = opentelemetry.getTracer().startSpan('doWork', {
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);
}
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);
}
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);
}
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();
function doWork(parent) {
// Start another span. In this example, the main method already started a