Below, we shall see the Cloud Function Node.js Serverless Auto-Instrumentation Monitoring steps in Atatus using OTel.

Instrument your serverless cloud functions with OpenTelemetry Node.js

  1. If you need to add API Key to otelwrapper.js file, navigate to Atatus Dashboard --> Settings --> Account Settings --> API Keys. You can either generate a new API key or copy an existing one with write permission. After obtaining the API key, you'll need to set it in the otelwrapper.js.

  2. Create otelwrapper.js file. Then add the below code:

    /* otelwrapper.js */
    
    const { Resource } = require('@opentelemetry/resources');
    const { SEMRESATTRS_SERVICE_NAME, } = require('@opentelemetry/semantic-conventions');
    const api = require('@opentelemetry/api');
    const { BatchSpanProcessor } = require('@opentelemetry/sdk-trace-base');
    const { OTLPTraceExporter, } = require('@opentelemetry/exporter-trace-otlp-http');
    const { NodeTracerProvider } = require('@opentelemetry/sdk-trace-node');
    const { registerInstrumentations } = require('@opentelemetry/instrumentation');
    const { getNodeAutoInstrumentations, } = require('@opentelemetry/auto-instrumentations-node');
    
    const providerConfig = {
        resource: new Resource({
            [SEMRESATTRS_SERVICE_NAME]: '<your function name>',
        }),
    };
    
    api.diag.setLogger(new api.DiagConsoleLogger(), api.DiagLogLevel.ALL);
    
    const provider = new NodeTracerProvider(providerConfig);
    const collectorOptions = {
        url: 'https://otel-rx.atatus.com/v1/traces',
        headers: {
            "api-key": '<YOUR_API_KEY>',
        }
    };
    
    const spanProcessor = new BatchSpanProcessor(
        new OTLPTraceExporter(collectorOptions),
    );
    
    provider.addSpanProcessor(spanProcessor);
    provider.register();
    
    registerInstrumentations({
        instrumentations: [getNodeAutoInstrumentations()],
    });
    
    
  3. Add package dependencies

    Add the following dependencies to your package.json

    {
    "dependencies": {
        "@opentelemetry/api": "^1.3.0",
        "@opentelemetry/auto-instrumentations-node": "^0.35.0",
        "@opentelemetry/exporter-trace-otlp-http": "^0.34.0",
        "@opentelemetry/instrumentation": "^0.34.0",
        "@opentelemetry/sdk-node": "^0.34.0",
        "@opentelemetry/sdk-trace-base": "^1.8.0",
        "@opentelemetry/sdk-trace-node": "^1.8.0",
        "@opentelemetry/resources": "^1.8.0",
        "@opentelemetry/semantic-conventions": "^1.8.0"
        }
    }
    
  4. Add the below environment into your serverless.yml.

    environment:
        NODE_OPTIONS: --require ./otelwrapper.js
    
  5. Finally, Deploy your GCP cloud function.