Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linking CloudWatch logs to traces from ECS Fargate #588

Closed
hexionas opened this issue Apr 28, 2023 · 8 comments
Closed

Linking CloudWatch logs to traces from ECS Fargate #588

hexionas opened this issue Apr 28, 2023 · 8 comments
Labels

Comments

@hexionas
Copy link

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
Copy link
Contributor

atshaw43 commented May 8, 2023

@carolabadeer
Copy link
Contributor

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

@GD-Dheer
Copy link

@hexionas Did you guys ever figure this out?

@markrin
Copy link

markrin commented May 30, 2024

This can be done using resource-detectors in collector config:

  1. my-config.yaml
...
processors:
  resourcedetection/ecs:
    detectors: [env, ecs]
    timeout: 2s
    override: false
...
  1. Build your own collector image
FROM public.ecr.aws/aws-observability/aws-otel-collector:latest
COPY ./my-config.yaml /etc/ecs/myconfig.yaml
  1. upload image to your registry
  2. 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
Copy link

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

@JoseFdri
Copy link

JoseFdri commented Jun 27, 2024

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
Copy link

ibnjunaid commented Jun 28, 2024

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
Copy link

foxxor commented Jul 31, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

8 participants