aws / aws-xray-sdk-node

The official AWS X-Ray SDK for Node.js.
Apache License 2.0
267 stars 156 forks source link

Linking CloudWatch logs to traces from ECS Fargate #588

Closed hexionas closed 1 year ago

hexionas commented 1 year ago

We would love to be able to see our logs linked to a trace. Following some previous hacked together solutions they appear to be impossible in this library.

Will there be support for this at any point? Or is there a way to achieve this that I might be missing?

atshaw43 commented 1 year ago

Have you tried setting the "awslogs-group"?

https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/deploy_servicelens_CloudWatch_agent_deploy_ECS.html

carolabadeer commented 1 year ago

Closing as stale issue - please reopen if there are any further questions

GD-Dheer commented 11 months ago

@hexionas Did you guys ever figure this out?

markrin commented 3 months ago

This can be done using resource-detectors in collector config: 1) my-config.yaml

...
processors:
  resourcedetection/ecs:
    detectors: [env, ecs]
    timeout: 2s
    override: false
...

2) Build your own collector image

FROM public.ecr.aws/aws-observability/aws-otel-collector:latest
COPY ./my-config.yaml /etc/ecs/myconfig.yaml

3) upload image to your registry 4) update container definition for your sidecar-collector:

{
            "name": "otel-collector",
            "image": "YOUR_REGISTRY_ADDRESS/my-xray-collector:latest",
            "cpu": 0,
            "essential": true,
            "command": [
                "--config=/etc/ecs/my-config.yaml"
            ]
        }
JoseFdri commented 2 months ago

I'm having the same issue, I'm adding the log group as metadata and it's not working! any workaround for this?

JoseFdri commented 2 months ago

I fixed this way:

const segment = AWSXRay.getSegment() as AWSXRay.Segment;
    segment.addPluginData({
      "cloudwatch_logs": [
        {
          "log_group": SERVICE_NAME
        }
      ],
      resource_names: [
        SERVICE_NAME
      ]
    })
I'm using typescript and the AWSXRay client so directly assigning `segment.aws.cloudwatch_logs = ` didn't work for me.
ibnjunaid commented 2 months ago

I was able to get it working with express using the below sample code and it can be possibly be adapted for other use cases as well.


const express = require('express');
const AWSXRay = require('aws-xray-sdk');

const logger = {
    info: function (message, subsegment) {
        if (subsegment != undefined){
            console.info( `AWS-XRAY-TRACE-ID: ${subsegment.parent.trace_id} ${subsegment.id} ${message}`)
        } else {
            const segment = AWSXRay.getSegment()
            console.info(`AWS-XRAY-TRACE-ID: ${segment.trace_id} ${message}`)
        }
    }
}

const setCloudWatchConfig = (cloudwatch_log_groupname) => (req, res, next) => {
    const segment = AWSXRay.getSegment()
    segment.aws.cloudwatch_logs= [{log_group: cloudwatch_log_groupname}]
    next();
}

const app = express();

app.use(AWSXRay.express.openSegment('MyApp'));

app.use(setCloudWatchConfig("xray-test"))

app.get('/user' ,(req, res) => {
    logger.info('rendering user!');
    res.send('user rendered!')
})

app.use(AWSXRay.express.closeSegment());

app.listen('5000', ()=> {
    console.log('App listening on http://localhost:5000');
})
foxxor commented 1 month ago

Using only the Log Group Name is not enough, it shows an error on the XRay UI.

So you also need to specify the Log Group ARN:

import * as AWSXRay from "aws-xray-sdk-core";

export function setCloudWatchLogGroup(cwLogGroupName: string, cwLogGroupArn: string): void {
    const segment = AWSXRay.getSegment();

    if (segment) {
        const isSubsegment = "parent" in segment;

        // This can only be added to the parent segment
        if (!isSubsegment) {
            segment.addPluginData({
                cloudwatch_logs: [
                    {
                        log_group: cwLogGroupName,
                        arn: cwLogGroupArn,
                    },
                ],
            });
        }
    }
}

Then it works and the Cloudwatch Logs are correctly correlated on XRay.