0.1.0 • Published 14 days ago

@gyizerhq/loopback4_tracing v0.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
14 days ago

loopback4-tracing

Actions Status Coverage Status

Latest version License Downloads Total Downloads

LoopBack 4 Tracing Component

Contents

Prerequisites

Some modules need to be installed as peer dependencies with at least a certain version.

@loopback/core  >=2.14.0
@loopback/rest  >=9.1.2

Installation

npm install loopback4-tracing

Initialize tracing

Before loading any application code it is required to initialize tracing. This is usually done at the top of the index.js or index.ts file.

require("loopback4-tracing").init({/* config */});

or in TypeScript it is also possible to do

import { initializeTracing } from "loopback4-tracing";

initializeTracing({/* config */});

Bind the component

This will add the tracing interceptor and observer to the application. The interceptor will create method invocation spans and the observer is required to gracefully shutdown the tracer provider and exporters when the application is stopped.

import { TracingBindings, TracingComponent } from "loopback4-tracing";

export class MyApplication extends BootMixin(
    ServiceMixin(RepositoryMixin(RestApplication))
) {
    constructor(options?: ApplicationConfig) {
        super(options);

        this.component(TracingComponent);
    }
}

Usage

The module provides a lot of auto instrumentations by default but is also possible to create custom spans in your code.

Trace decorator

The easiest way create custom spans is by using the decorator which can be added to any method.

import { trace } from "loopback4-tracing";

class ExampleService {
    constructor() {}

    @trace()
    exampleMethod() {
        // do some work
    }
}

The decorator will wrap the method into a span which will use the method name as span name by default. It is also possible to use a custom span name by setting the operation name in the decorator options.

@trace({ operationName: "customName" })

Create custom span

The first step is get the tracer of the service either by using dependency injection

import { Tracer, TracingBindings } from "loopback4-tracing";

class ExampleService {
    constructor(
        @inject(TracingBindings.TRACER)
        private tracer: Tracer
    ) {}

    exampleMethod() {
        const span = this.tracer.startSpan("exampleMethod");

        // do some work

        span.end();
    }
}

or by directly importing the tracer

import { tracer } from "loopback4-tracing";

function exampleFunction() {
    const span = tracer.startSpan("exampleFunction");

    // do some work

    span.end();
}

Get active span

In some cases it might not be desired to create a new span but instead get the active span to add additional events and attributes to it.

import { getActiveSpan } from "loopback4-tracing";

function exampleFunction() {
    const span = getActiveSpan();

    span.addEvent("some event");

    span.setAttribute("custom.attribute", "some value");
}

Configuration

Most of the time it is not recommended to change the default configuration but there are some cases where it makes sense, for example to enable / disable default instrumentations provided by the module such as http.

The module can be configured by providing custom values in the init function or by using environment variables which will have highest priority.

Note: By default tracing is not enabled. The recommended approach is to enable tracing by setting the environment variable TRACING_ENABLED=true and to only enable it if the collected traces are analyzed.

Configuration parameters

ParameterEnvironment VariableDescriptionDefaultType
enabledTRACING_ENABLEDEnable tracingfalseboolean
serviceNameTRACING_SERVICE_NAMEName of servicepkg.namestring
serviceVersionTRACING_SERVICE_VERSIONVersion of servicepkg.versionstring
propagationFormatTRACING_PROPAGATION_FORMATPropagation format"jaeger""jaeger" \| "w3c"
setRequestIdTRACING_SET_REQUEST_IDSet request id in error and responsetrueboolean
jaeger.enabledTRACING_JAEGER_ENABLEDEnable jaeger exportertrueboolean
jaeger.hostTRACING_JAEGER_HOSTJaeger host"localhost"string
jaeger.portTRACING_JAEGER_PORTJaeger port6832number
jaeger.endpointTRACING_JAEGER_ENDPOINTJaeger traces endpointundefinedstring
jaeger.spanProcessor.typeTRACING_JAEGER_SPAN_PROCESSORJaeger span processor type"batch""simple" \| "batch"
console.enabledTRACING_CONSOLE_ENABLEDEnable console exporterfalseboolean
diagnostics.enabledTRACING_DIAGNOSTICS_ENABLEDEnable diagnostics loggerfalseboolean
diagnostics.logLevelTRACING_DIAGNOSTICS_LOG_LEVELLog level of diag logger9999DiagLogLevel
methodInvocations.enabledTRACING_METHOD_INVOCATIONS_ENABLEDEnable method invocation spanstrueboolean
http.enabledTRACING_HTTP_ENABLEDEnable http instrumentationtrueboolean

For further details about possible configuration options, see tracing options.

Note: Some values can not be configured by using environment variables but instead need to be provided to the init function.

Example

For an example on how to create custom spans see tracing interceptor and for more information, please read the opentelemetry tracing documentation.

Debug

To enable debug logs set the DEBUG environment variable to loopback:tracing:*, see Setting debug strings for further details.

Related resources

Contributing

contributions welcome

License

This project is licensed under the MIT license. See the LICENSE file for more info.