Skip to content

uptrace/uptrace-js

Repository files navigation

Uptrace for Node.js and Web

build workflow Documentation Chat

Introduction

uptrace-js is an OpenTelemery distribution configured to export traces and metrics to Uptrace.

uptrace-js comes in two flavors:

Quickstart

Install uptrace-js:

yarn add @uptrace/node --save

Run the basic example below using the DSN from the Uptrace project settings page.

// The very first import must be Uptrace/OpenTelemetry.
const otel = require('@opentelemetry/api')
const uptrace = require('@uptrace/node')

// Start OpenTelemetry SDK and invoke instrumentations to patch the code.
uptrace.configureOpentelemetry({
  // Set dsn or UPTRACE_DSN env var.
  //dsn: '',
  serviceName: 'myservice',
  serviceVersion: '1.0.0',
})

// Create a tracer. Usually, tracer is a global variable.
const tracer = otel.trace.getTracer('app_or_package_name', '1.0.0')

// Create a root span (a trace) to measure some operation.
tracer.startActiveSpan('main-operation', (main) => {
  tracer.startActiveSpan('child1-of-main', (child1) => {
    child1.setAttribute('key1', 'value1')
    child1.recordException(new Error('error1'))
    child1.end()
  })

  tracer.startActiveSpan('child2-of-main', (child2) => {
    child2.setAttribute('key2', 42)
    child2.end()
  })

  // End the span when the operation we are measuring is done.
  main.end()

  console.log(uptrace.traceUrl(main))
})

setTimeout(async () => {
  // Send buffered spans and free resources.
  await uptrace.shutdown()
})

Links