How to use @opencensus/nodejs - 10 common examples

To help you get started, we’ve selected a few @opencensus/nodejs 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 census-ecosystem / opencensus-experiments / interoptest / src / nodejsservice / server.js View on Github external
function main () {
  // Setup Tracer
  const tracer = tracing.start({
    samplingRate: 1,
    propagation: new propagation.TraceContextFormat()
  }).tracer;

  // Setup Exporter, Enable GRPC and HTTP plugin
  enableJaegerTraceExporter(tracer);
  enableHttpPlugin(tracer);
  //enableGrpcPlugin(tracer);

  // Start GRPC Server
  grpcServer.start(interop.ServicePort.NODEJS_GRPC_BINARY_PROPAGATION_PORT, '0.0.0.0');

  // Start HTTP Server
  httpServer.start(interop.ServicePort.NODEJS_HTTP_TRACECONTEXT_PROPAGATION_PORT, '0.0.0.0');
}
github census-ecosystem / opencensus-experiments / interoptest / src / nodejsservice / src / testservice / service-hopper.js View on Github external
return new Promise((resolve, reject) => {
    const id = request.getId();
    const name = request.getName();
    const hops = request.getServiceHopsList();

    // Adds reqId attribute
    const span = tracing.tracer.currentRootSpan;
    if (span) {
      span.addAttribute('reqId', id);
    }

    // Creates a test response
    const response = new interop.TestResponse();
    response.setId(id);
    if (hops.length === 0) {
      resolve(setSuccessStatus(response));
    } else {
      // Extracts data from first service hop.
      const firstHop = hops[0];
      const host = firstHop.getService().getHost() || constants.DEFAULT_HOST;
      const port = firstHop.getService().getPort();
      const restHops = hops.slice(1);
      const transport = firstHop
github census-instrumentation / opencensus-node / examples / grpc / capitalize_server.js View on Github external
// A Stackdriver workspace is required and provided through the environment as ${GOOGLE_PROJECT_ID}
  const projectId = process.env.GOOGLE_PROJECT_ID;

  // GOOGLE_APPLICATION_CREDENTIALS are expected by a dependency of this code
  // Not this code itself. Checking for existence here but not retaining (as not needed)
  if (!projectId || !process.env.GOOGLE_APPLICATION_CREDENTIALS) {
    throw Error('Unable to proceed without a Project ID');
  }
  // Creates Stackdriver exporter
  const exporter = new StackdriverTraceExporter({ projectId: projectId });

  // Starts Stackdriver exporter
  tracing.registerExporter(exporter).start();

  // Starts tracing and set sampling rate
  const tracer = tracing.start({
    samplingRate: 1 // For demo purposes, always sample
  }).tracer;

  // Defines basedir and version
  const basedir = path.dirname(require.resolve('grpc'));
  const version = require(path.join(basedir, 'package.json')).version;

  // Enables GRPC plugin: Method that enables the instrumentation patch.
  plugin.enable(grpc, tracer, version, /** plugin options */{}, basedir);

  return tracer;
}
github census-instrumentation / opencensus-node / examples / http / client.js View on Github external
function setupTracerAndExporters () {
  const zipkinOptions = {
    url: 'http://localhost:9411/api/v2/spans',
    serviceName: 'opencensus_tutorial'
  };

  // Creates Zipkin exporter
  const exporter = new ZipkinTraceExporter(zipkinOptions);

  // Starts tracing and set sampling rate, exporter and propagation
  const tracer = tracing.start({
    exporter,
    samplingRate: 1, // For demo purposes, always sample
    propagation: new TraceContextFormat(),
    logLevel: 1 // show errors, if any
  }).tracer;

  return tracer;
}
github census-instrumentation / opencensus-node / examples / grpc / capitalize_client.js View on Github external
const projectId = process.env.GOOGLE_PROJECT_ID;

  // GOOGLE_APPLICATION_CREDENTIALS are expected by a dependency of this code
  // Not this code itself. Checking for existence here but not retaining (as not needed)
  if (!projectId || !process.env.GOOGLE_APPLICATION_CREDENTIALS) {
    throw Error('Unable to proceed without a Project ID');
  }

  // Creates Stackdriver exporter
  const exporter = new StackdriverTraceExporter({ projectId: projectId });

  // Starts Stackdriver exporter
  tracing.registerExporter(exporter).start();

  // Starts tracing and set sampling rate
  const tracer = tracing.start({
    samplingRate: 1 // For demo purposes, always sample
  }).tracer;

  // Defines basedir and version
  const basedir = path.dirname(require.resolve('grpc'));
  const version = require(path.join(basedir, 'package.json')).version;

  // Enables GRPC plugin: Method that enables the instrumentation patch.
  plugin.enable(grpc, tracer, version, /** plugin options */{}, basedir);

  return tracer;
}
github census-instrumentation / opencensus-web / examples / user_interaction / server / server.js View on Github external
function setupTracerAndExporters() {
  const zipkinOptions = {
    url: 'http://localhost:9411/api/v2/spans',
    serviceName: 'opencensus_web_server'
  };

  // Creates Zipkin exporter
  const exporter = new ZipkinTraceExporter(zipkinOptions);

  // Starts tracing and set sampling rate, exporter and propagation
  const tracer = tracing.start({
    exporter,
    samplingRate: 1, // For demo purposes, always sample
    propagation: new TraceContextFormat(),
    logLevel: 1 // show errors, if any
  }).tracer;

  return tracer;
}
github census-instrumentation / opencensus-node / examples / trace / multi-span-tracing.js View on Github external
*      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/** Example showing how to directly create a child span and add annotations. */
const tracing = require('@opencensus/nodejs');
const { ZipkinTraceExporter } = require('@opencensus/exporter-zipkin');

// 1. Get the global singleton Tracer object
// 2. Configure 100% sample rate, otherwise, few traces will be sampled.
const tracer = tracing.start({ samplingRate: 1 }).tracer;

// 3. Configure exporter to export traces to Zipkin.
tracer.registerSpanEventListener(new ZipkinTraceExporter({
  url: 'http://localhost:9411/api/v2/spans',
  serviceName: 'node.js-quickstart'
}));

function main () {
  // 4. Create a span. A span must be closed.
  // For any of the web frameworks for which we provide built-in plugins (http,
  // grpc, mongodb etc), a root span is automatically started whenever an
  // incoming request is received (in other words, all middleware already runs
  // within a root span).
  tracer.startRootSpan({ name: 'main' }, rootSpan => {
    for (let i = 0; i < 10; i++) {
      doWork();
github thesandlord / Istio101 / code / code-opencensus-full / index.js View on Github external
return new Promise(async (res,rej)=>{
    const fibSpan = tracing.tracer.startChildSpan('Fibonacci Calculation')
    fibSpan.parentSpanId = span.id
    const num = fibonacci(Math.floor(Math.random() * 30));
    fibSpan.end()

    // Random sleep! Because everyone needs their sleep!
    const sleepSpan = tracing.tracer.startChildSpan('Sleep')
    sleepSpan.parentSpanId = span.id
    await sleep()
    sleepSpan.end()
    
    res({
        isOdd: Boolean(num%2),
        num
    })
    
  })
}
github thesandlord / Istio101 / code / code-opencensus-full / index.js View on Github external
app.get('/', async(req, res) => {

  const begin = Date.now()

  // Calculate a Fibbonacci Number and check if it is Odd or Even
  const childSpan = tracing.tracer.startChildSpan('Fibonacci Odd Or Even')
  const isOddOrEven = await oddOrEven(childSpan)
  childSpan.end()

  let up
  try {
    up = await request({url: upstream_uri})
  } catch (error) {
        up = error
  }
  const timeSpent = (Date.now() - begin) / 1000 + "secs (opencensus full)"

  res.end(`${service_name} - ${timeSpent} - num: ${isOddOrEven.num} - isOdd: ${isOddOrEven.isOdd}\n${upstream_uri} -> ${up}`)
})
github census-instrumentation / opencensus-node / examples / grpc / capitalize_client.js View on Github external
// for more details.
  // Expects ADCs to be provided through the environment as ${GOOGLE_APPLICATION_CREDENTIALS}
  // A Stackdriver workspace is required and provided through the environment as ${GOOGLE_PROJECT_ID}
  const projectId = process.env.GOOGLE_PROJECT_ID;

  // GOOGLE_APPLICATION_CREDENTIALS are expected by a dependency of this code
  // Not this code itself. Checking for existence here but not retaining (as not needed)
  if (!projectId || !process.env.GOOGLE_APPLICATION_CREDENTIALS) {
    throw Error('Unable to proceed without a Project ID');
  }

  // Creates Stackdriver exporter
  const exporter = new StackdriverTraceExporter({ projectId: projectId });

  // Starts Stackdriver exporter
  tracing.registerExporter(exporter).start();

  // Starts tracing and set sampling rate
  const tracer = tracing.start({
    samplingRate: 1 // For demo purposes, always sample
  }).tracer;

  // Defines basedir and version
  const basedir = path.dirname(require.resolve('grpc'));
  const version = require(path.join(basedir, 'package.json')).version;

  // Enables GRPC plugin: Method that enables the instrumentation patch.
  plugin.enable(grpc, tracer, version, /** plugin options */{}, basedir);

  return tracer;
}

@opencensus/nodejs

OpenCensus is a toolkit for collecting application performance and behavior data.

Apache-2.0
Latest version published 3 years ago

Package Health Score

50 / 100
Full package analysis