Giter VIP home page Giter VIP logo

datadog-apm's Introduction

Datadog APM Helpers

A lightweight wrapper over Datadog's dd-trace library, adding utility functions to make it easier to trace, tag, and search for your functions.

Motivation

dd-trace offers power and flexibility, but with that comes complexity. This wrapper adds helpers to simplify with:

  • Tracing functions (including async)
  • Tracing class methods
  • Adding tags to the active span
  • Adding tags to the root span (Required to enable filtering and searching in Trace Search and Analytics)
  • Marking a span as an error without throwing

Install

npm install --save @gamechanger/datadog-apm

or

yarn add @gamechanger/datadog-apm

Decorators

Your observability code should stay out of the way of your business logic, and should be easy to add and remove. Decorators are a great way to accomplish that.

// Trace a function
@APM.trace()
async function foo() {}

// Trace *all* methods of a class
@APM.trace()
class GameChanger {
    public foo() {}
    private bar() {}
}

// Trace *individual* methods of a class
class GameChanger {
    @APM.trace()
    public foo() {}
    private bar() {}
}

// The decorator can be configured to override the defaults
class EmailQueue {
    @APM.trace({ serviceName: 'queue', spanName: 'queue.message' })
    public async pop() {}
}

Tags

Adding tags happens throughout the code, and so ideally adds as few lines as possible.

With vanilla dd-trace, you must always check if the active span exists:

const span = tracer.scope().active();

if (span !== null) {
    span.addTags({
        'http.method': req.method
    });
}

This wrapper helps clean things up for you:

APM.addTags({ 'http.method': req.method })

Root Span Access

If you're using koa or express, you can use APM.getRootSpanFromContext to get the root span. This can be used to add tags to the root span, which are then accessible in the Trace Search & Analytics screens in datadog.

APM.addTags({ teamId: context.params.teamId }, APM.getRootSpanFromContext(context));

NOTE: This uses undocumented properties and is not guaranteed to work. However, if the underlying dd-trace code were to change, it will safely fall back to adding the tags to the current span rather than the root span.

Marking Spans as Errors

APM.markAsError(new Error('I am not thrown'))

Deploying

All publishing is done through the CI pipeline.

To trigger a new deploy, simply update the version number using npm version <version> and then push the package.json change and related tag to the repo with git push --follow-tags.

datadog-apm's People

Contributors

dependabot[bot] avatar odielleongc avatar miki725 avatar eliran avatar dmg3 avatar vector919 avatar eduarenas avatar brianskibinskigc avatar ivy-moore avatar matt-casey avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.