npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

b3trace

v1.0.2

Published

Object that represents a B3 Trace based on the OpenZipkin specifications: https://github.com/openzipkin/b3-propagation

Downloads

8

Readme

B3Trace

Description

This module provides an Object representation of a B3 TraceContext based on the OpenZipkin specifications. B3Trace lets you construct new Traces or a TraceContext from a parent TraceContext.

Installation

npm i b3trace

Usage

New Trace

To construct a TraceContext of a brand new Trace:

const {initializeTrace} from 'b3trace';

const traceCtx = initializeTrace();

TraceContext from an Existing Trace

To construct a TraceContext from an existing Trace given an incoming TraceContext:

// ...other imports...
const {initializeTraceContext} from 'b3trace';

async function handler(({headers}), context) {
    const traceId = headers['X-B3-TraceId'];
    const parentSpanId = headers['X-B3-ParentSpanId'];
    const spanId = headers['X-B3-SpanId'];
    const sampled = headers['X-B3-Sampled'];

    const traceCtx = initializeTraceContext({ traceId, spanId, parentSpanId, sampled });

    logger.info('Some logger message...', traceCtx.toJson());
    const user = await dynamoDb.getUser(/*...parameters...*/);

    //...some business logic...

    return {
        // ... return Https Response to API Gateway
    }
}

export {
    handler
}

propagation

When propagation=true:

   Client Tracer                                                  Server Tracer
┌───────────────────────┐                                       ┌───────────────────────┐
│                       │                                       │                       │
│   TraceContext        │          Http Request Headers         │   TraceContext        │
│ ┌───────────────────┐ │         ┌───────────────────┐         │ ┌───────────────────┐ │
│ │ TraceId           │ │         │ X-B3-TraceId      │         │ │ TraceId           │ │
│ │                   │ │         │                   │         │ │                   │ │
│ │ ParentSpanId      │ │ Inject  │ X-B3-ParentSpanId │ Extract │ │ ParentSpanId      │ │
│ │                   ├─┼────────>│                   ├─────────┼>│                   │ │
│ │ SpanId            │ │         │ X-B3-SpanId       │         │ │ SpanId            │ │
│ │                   │ │         │                   │         │ │                   │ │
│ │ Sampling decision │ │         │ X-B3-Sampled      │         │ │ Sampling decision │ │
│ └───────────────────┘ │         └───────────────────┘         │ └───────────────────┘ │
│                       │                                       │                       │
└───────────────────────┘                                       └───────────────────────┘

When the propagation flag is true, initializeTraceContext copies the incoming TraceContext. This means that the incoming TraceContext and the current TraceContext will share the same node on a TraceContext tree.

When propagation=false:

                           ┌───────────────────┐
 Incoming Headers          │   TraceContext    │
┌───────────────────┐      │ ┌───────────────┐ │
│ XXXX-TraceId      │──────┼─┼> TraceId      │ │
│                   │      │ │               │ │
│ XXXX-SpanId       │──────┼─┼> ParentSpanId │ │
└───────────────────┘      │ │               │ │      ┌──────────────┐
                           │ │  SpanId      <┼─┼──────│ ID Generator │
                           │ └───────────────┘ │      └──────────────┘
                           └───────────────────┘

When the propagation flag is false, initializeTraceContext will construct the new TraceContext similar to how a child span is constructed. This means that the current TraceContext will:

  • share the trace identifier with the incoming TraceContext
  • have a parent span identifier that matches the incoming TraceContext's span identifier
  • have a new, 16 hexadecimal length span identifier

Upcoming Features

TraceContext - getTraceContext(spanId: string): TraceContext

This method will retrieve a reference to a TraceContext with a given span identifier. The intent will be to restructure the TraceContext as a recursive tree of TraceContext(s) with references to the parent and child TraceContext(s).

const {initializeTrace} from 'b3trace';

// root TraceContext:
const traceCtx = initializeTrace();
// descendents of root TraceContext:
const childCtx1 = traceCtx.createChildContext();
const childCtx2 = traceCtx.createChildContext();
const childCtx3 = traceCtx.createChildContext();
// descendents of childCtx1 TraceContext:
const childCtx1_1 = childCtx1.createChildContext();
const childCtx1_2 = childCtx1.createChildContext();

// Should return direct descendent (childCtx3):
traceCtx.getTraceContext(childCtx3.getSpanId());
// Should return deep descendent (childCtx1_2):
traceCtx.getTraceContext(childCtx1_2.getSpanId());

// Should NOT return because (root TraceContext) is NOT a descendent of childCtx1_2:
childCtx1_2.getTraceContext(childCtx1_2.getSpanId());

TraceContext - constructor(traceContext: TraceContext): TraceContext

This constructor will deep copy an existing TraceContext Object.

const {TraceContext} from 'b3trace';

// assume this TraceContext was constructed from incoming headers:
const traceCtx = initializeTraceContext(/*...assume headers were propagated...*/);
const traceCtxDeepCopy = new TraceContext(traceCtx);

index - initializeTraceContext(b3Header: string, isPropagated = true): TraceContext

This constructor will construct a TraceContext from an existing TraceContext given a single b3 header string.

For example:

const {initializeTraceContext} from 'b3trace';

const b3 = '80f198ee56343ba864fe8b2a57d3eff7-e457b5a2e4d86bd1-1-05e3ac9a4f6e3b90';
const traceCtx = initializeTraceContext(b3);

License

Copyright 2021. Licensed MIT.