0.0.134 • Published 2 years ago

@mzahor-test-org/opentelemetry v0.0.134

Weekly downloads
23
License
Apache-2.0
Repository
github
Last release
2 years ago

Aspecto

Aspecto enables developers to find, fix and prevent issues before your customers even notice.

Install

npm install @aspecto/opentelemetry

Usage

In the root folder create an aspecto.json file with the content {"aspectoAuth" : "-- token goes here --"}.

You can get your token from here

Add this call at the top of your app entry point:

require('@aspecto/opentelemetry')();

// the rest of your main file requires

See below for more configuration options

Configuration

You can configure the package via one or more of the following:

  • options variable. for example: require('@aspecto/opentelemetry')({optionName: optionValue});
    This enables setting config options dynamically.
  • Environment variables
  • Add aspecto.json configuration file to the root directory, next to service's package.json file

Values are evaluated in the following priority: 1) options object 2) environment variables 3) config file 4) default values

Option NameEnvironment VariableTypeDefaultDescription
disableAspectoDISABLE_ASPECTObooleanfalsedisable aspecto
envNODE_ENVstring-environment name
aspectoAuthASPECTO_AUTHUUID-Aspecto's API key for authentication
packageNameASPECTO_PACKAGE_NAMEstringname key in package.jsonset packageName manually instead of reading it from package.json. For example: a service that runs in multiple "modes"
packageVersionASPECTO_PACKAGE_VERSIONstringversion key in package.jsonset packageVersion manually instead of reading it from package.json
local-booleanfalsewhen set to true, enable live flows
ciReportASPECTO_CI_REPORTbooleanfalseset to true to indicate running the service from CI environment for testing
logger-logger interface-logger to be used in this tracing library. common use for debugging logger: console
writeSystemLogs-booleanfalseIf true, emit all log messages from Opentelemetry SDK to supplied logger if present, or to console if missing
samplingRatioASPECTO_SAMPLING_RATIOnumber1.0How many of the traces starting in this service should be sampled. set to number in range 0.0, 1.0 where 0.0 is no sampling, and 1.0 is sample all. Specific rules set via aspecto app takes precedence
waitForSamplingRulesASPECTO_WAIT_FOR_SAMPLING_RULESbooleanfalseWhen true, the SDK will not trace anything until remote sampling configuration arrives (few hundreds ms). Can be used to enforce sampling configuration is always applied, with the cost of losing traces generated during service startup.
collectPayloadsASPECTO_COLLECT_PAYLOADSbooleantrueShould aspecto SDK collect payloads of operations
exportBatchSizeASPECTO_EXPORT_BATCH_SIZEnumber100How many spans to batch in a single export to the collector
exportBatchTimeoutMsASPECTO_EXPORT_BATCH_TIMEOUT_MSnumber1000 (1s)Maximum time in ms for batching spans before sending to collector
sqsExtractContextPropagationFromPayloadASPECTO_SQS_EXTRACT_CONTEXT_PROPAGATION_FROM_PAYLOADbooleantruefor aws-sdk instrumentation. should be true when the service receiveMessages from SQS which is subscribed to SNS and subscription configured with "Raw message delivery": Disabled. setting to false is a bit more performant as it turns off JSON parse on message payload

Send Spans Manually

Background

"Span" is the name of the data structure representing an interesting operation in your app.
Aspecto will automatically collect spans for operations created by popular packages that perform IO (such as http, messaging systems, databases, etc).
Manual spans are used if you need to trace an operation in a code you wrote, or when using a package that does not provide an automatic tracing.

Example

To create a Manual Span for a function run, you need to wrap it in a trace call like this:

import { trace } from '@aspecto/opentelemetry'; // ES import
const { trace } = require('@aspecto/opentelemetry'); // CommonJS require

trace(
    // All options are optional
    {
        name: '** optional name for the operation **',
        metadata: {
            'metadata.key.for.the.operation': 'you can attach custom metadata to the operation',
        },
        type: 'Type of Operation',
    },
    () => {
        // your code which you want to trace
    }
);

Add span attributes manually

You can add attributes to your spans for more visibility.
Attributes can be added to a span at any time before the span is finished:

import { setAttribute, setAttributes } from '@aspecto/opentelemetry';

// add a single attribute
const result = setAttribute('foo', 'bar');

// add multiple attributes
const result = setAttributes({ foo: 'bar' });

// result will be true in case of success

(*) All keys will get a prefix of 'aspecto.extra'.

Correlate Logs with Traces

A common use case for the Trace Search tool is to see the related trace while inspecting a log event.
To do this, you must attach an active traceId to your logs.

Example

Use the getContext method, exposed from our package, to attach traceId to your logs:

const { getContext } = require('@aspecto/opentelemetry');

console.log('Something happened!', { traceId: getContext().traceId })});

Live Traces

Live Traces captures all payloads and traces in your local environment and automatically extract the topology & dependencies between endpoints. You can activate it using local: true, like so:

require('@aspecto/opentelemetry')({
    local: true,
});

This allows you to capture traces from all the microservices that you're running locally (both on the host env and docker) with local mode enabled. Once the process starts it will output the following link:

=====================================================================================================================================
|                                                                                                                                   |
| 🕵️‍♀️See the live tracing stream at https://app.aspecto.io/app/live-traces/sessions?instanceId=14243e72-14dc-4255-87af-ef846b247578   |
|                                                                                                                                   |
=====================================================================================================================================

You only need to click the link once to see traces from all the microservices, that are running on your environment and have local mode enabled. Also this link is valid for a limited period of time (couple of days, but it may change in the future). If you don't see trace from some microservice (or none of them), please click the newly-generated link.

FaaS

AWS Lambda

Aspecto supports instrumenting AWS lambdas.
To do so, set up Aspecto as you'd usually do, and extract the returned lambda utility:

const { lambda } = require('@aspecto/opentelemetry')();

Next, wrap your function handler definition with the returned utility.

Example:

// Before
module.exports.myCallbackHandler = (event, context, callback) => { ... };
module.exports.myAsyncHandler = async (event, context) => { ... };

// After
module.exports.myCallbackHandler = lambda((event, context, callback) => { ... });
module.exports.myAsyncHandler = lambda(async (event, context) => { ... });

Notice: if your lambda is not deployed with a package.json file, make sure to provide the packageName option when initializing Aspecto.

Google Cloud Function

Aspecto supports instrumenting GCF with http trigger.
To do so, set up Aspecto as you'd usually do, and extract the gcf utility:

const { gcf } = require('@aspecto/opentelemetry')();

Next, wrap your function handler definition with the returned utility. Example:

// Before
exports.myEndpoint = (req, res) => { ... };

// After
exports.myEndpoint = gcf((req, res) => { ... });

Test Frameworks

Mocha

To instrument your test with aspecto using mocha version 8.0.0 and above, register mocha plugin as instructed below. In this mode, token (and other configuration) can be set only via aspecto.json config file or environment variables.

With CLI
mocha --require @aspecto/opentelemetry/mocha
With Mocha Config in package.json
  "mocha": {
    "require": [
      "@aspecto/opentelemetry/mocha"
    ]
  }
Config File
{
    "require": [
        "@aspecto/opentelemetry/mocha"
    ]
}
0.0.134

2 years ago

0.0.133

2 years ago

0.0.131

2 years ago

0.0.132

2 years ago

0.0.89-dev.4

3 years ago

0.0.89-dev.3

3 years ago

0.0.89-dev.2

3 years ago

0.0.89-dev.1

3 years ago

0.0.83

4 years ago

0.0.82

4 years ago

0.0.79

4 years ago

0.0.80

4 years ago

0.0.81

4 years ago

0.0.77

4 years ago

0.0.78

4 years ago

0.0.76

4 years ago

0.0.75

4 years ago

0.0.74

4 years ago

0.0.73

4 years ago

0.0.72

4 years ago

0.0.69

4 years ago

0.0.70

4 years ago

0.0.71

4 years ago

0.0.67

4 years ago

0.0.68

4 years ago

0.0.66

4 years ago

0.0.65

4 years ago