Description

Custom basic tracer that mildly emulates OpenTelemetry semantics and behavior. Built as a ligher-weight way to handle spans in technical contexts (like AWS Lambda) where OTel tooling seems brittle at best.

Make sure to reuse the same instance across your application to get it working as intended.

MikroTrace simplifies the OTel model a bit:

  • Only supports a single tracing context
  • Removes the need to pass in complete instances into the span functions. Use strings to refer to spans.

Hierarchy

  • MikroTrace

Constructors

Properties

context: any
correlationId?: string
event: any
instance: MikroTrace
isTraceSampled: boolean
metadataConfig: StaticMetadataConfigInput | Record<string, any> = {}
parentContext: string = ''
samplingLevel: number
serviceName: string
traceId: string

Methods

  • Description

    Store local representation so we can make lookups for relations.

    Parameters

    Returns void

  • Description

    Request to create a valid Span.

    Parameters

    • spanName: string
    • dynamicMetadata: any
    • Optional parentSpanName: string
    • Optional parentSpan: any

    Returns Span

  • Description

    Closes all spans.

    Only use this sparingly and in relevant cases, such as when you need to close all spans in case of an error.

    Returns void

  • Description

    Output the tracer configuration, for example for debugging needs.

    Returns {
        correlationId: undefined | string;
        parentContext: string;
        serviceName: string;
        spans: SpanRepresentation[];
        traceId: string;
    }

    • correlationId: undefined | string
    • parentContext: string
    • serviceName: string
    • spans: SpanRepresentation[]
    • traceId: string
  • Description

    Initialize the sample rate level. Only accepts numbers or strings that can convert to numbers. The default is to use all traces (i.e. 100 percent).

    Returns number

  • Description

    Check if MicroTrace has sampled the last trace. Will only return true value after having output an actual trace.

    Returns boolean

  • Description

    Remove an individual span.

    Avoid calling this manually as the Span class will make the necessary call when having ended a span.

    Parameters

    • spanName: string

    Returns void

  • Description

    Set the parent context. Use this if you want to automatically assign a span as the parent for any future spans.

    Call it with an empty string to reset it.

    Example

    tracer.setParentContext('FullSpan')
    

    Example

    tracer.setParentContext('')

    This value will be propagated to all future spans.

    Parameters

    • parentContext: string

    Returns void

  • Description

    Set sampling rate of traces as a number between 0 and 100.

    Parameters

    • samplingPercent: number

    Returns number

  • Description

    Utility to check if a log should be sampled (written) based on the currently set samplingLevel. This uses a 0-100 scale.

    If the random number is lower than (or equal to) the sampling level, then we may sample the log.

    Returns boolean

  • Description

    Start a new trace. This will typically be automatically assigned to the parent trace if one exists. Optionally you can pass in the name of a parent span to link it to its trace ID.

    See

    https://docs.honeycomb.io/getting-data-in/tracing/send-trace-data/

    A root span, the first span in a trace, does not have a parent. As you
    instrument your code, make sure every span propagates its `trace.trace_id`
    and` trace.span_id` to any child spans it calls, so that the child span can
    use those values as its `trace.trace_id` and `trace.parent_id`. Honeycomb uses
    these relationships to determine the order spans execute and construct the
    waterfall diagram.

    Parameters

    • spanName: string
    • Optional parentSpanName: string

      If provided, this will override any existing parent context for this particular trace.

    Returns Span

  • Description

    An emergency mechanism if you absolutely need to reset the instance to its empty default state.

    Returns void

  • Description

    Set correlation ID. Make use of this if you were not able to set the correlation ID at the point of instantiating MikroTrace.

    This value will be propagated to all future spans.

    Parameters

    • correlationId: string

    Returns void

  • Description

    This instantiates MikroTrace. In order to be able to "remember" event and context we use a singleton pattern to reuse the same logical instance.

    If the start method receives any input, that input will overwrite any existing metadata.

    If you want to "add" to these, you should instead call enrich() and pass in your additional data there.

    Running this without input will also force a new traceId.

    Parameters

    Returns MikroTrace

Generated using TypeDoc