aws-observability / aws-otel-lambda

AWS Distro for OpenTelemetry - AWS Lambda
https://aws-otel.github.io/
Apache License 2.0
147 stars 56 forks source link

Unable to get tracecontext or trace-id using SDK API | NodeJs #942

Closed jarpz closed 2 weeks ago

jarpz commented 4 months ago

Describe the bug

We are unable to get OTEL tracecontext or trace-id using OTEL libraries on NodeJs 20 Runtime

Steps to reproduce

What did you expect to see? Trace-id on logs

What did you see instead? Trace context or trace-id is and empty object

What version of collector/language SDK version did you use?

"arn:aws:lambda:${AWS::Region}:901920570463:layer:aws-otel-nodejs-amd64-ver-1-18-1:1" "@opentelemetry/api": "^1.9.0", NodeJs 20

What language layer did you use? typescript / javascript

Additional context

We are able to see in xray, call make by the lambda to S3, but unable to access to the trace-id throught the OTEL SDK API.

import {Context} from 'aws-lambda';
import {Logger} from '@aws-lambda-powertools/logger';
import {LambdaInterface} from '@aws-lambda-powertools/commons/types';
import middy from '@middy/core';
import {ListBucketsCommand, S3Client} from '@aws-sdk/client-s3';
import api, {context, propagation} from '@opentelemetry/api';
import * as otel from '../../utils/opentelemetry';

const logger = new Logger();

const s3 = new S3Client({});

class Handler implements LambdaInterface {
    public async handler(_event: any, _context: Context): Promise<any> {
        logger.info(`currentTrace: ${otel.currentTrace()}`);

        const result = await s3.send(new ListBucketsCommand({}));

        logger.info(`context => ${JSON.stringify(api.context.active())} || ${JSON.stringify(currentContext())}`);
        if (api.context.active()) {
            const span = api.trace.getSpan(api.context.active());
            logger.info(`span => ${span}`);
            if (span) {
                logger.info(`QUE PASO??? ==> ${JSON.stringify(span)}`);
            }
        }

        // for (const bucket of result.Buckets){
        //     logger.info('showing bucket: ' + bucket.Name);
        // }

        return {
            result: 'hello world => ' + result.Buckets.length,
        };
    }
}

const main = middy(new Handler().handler);

module.exports = { main };

export function currentContext(): Record<string, any> {
    const output = {};

    propagation.inject(context.active(), output);

    return output;
}
github-actions[bot] commented 1 month ago

This issue is stale because it has been open 90 days with no activity. If you want to keep this issue open, please just leave a comment below and auto-close will be canceled

github-actions[bot] commented 2 weeks ago

This issue was closed because it has been marked as stale for 30 days with no activity.