Skip to main content

2 posts tagged with "datadog"

View All Tags

AWS Lambda β†’ Datadog: Show active alerts & logs

See active alerts for this Lambda at a glance
πŸ”’
https://eu-central-1.console.aws.amazon.com/lambda/home?region=eu-central-1#/functions/shop-order-processing?tab=code
preload

How does this help you?​

  • You will notice any active alerts on your Lambda function, which can help you fix issues faster
  • You will be able to navigate directly to the logs for the Lambda function

Preview​

How it looks in the extension

ToolJumpβ€’datadog4 active alertsβ€’datadogLogsβ€’ ... Other items ...

High level approach​

We read the DD_SERVICE tag from the Lambda function using the AWS Lambda API, then query Datadog for active alerts using that service tag. The logs link is constructed using the same service tag.

Prerequisites​

For the code below to work, please follow the guide on Connecting to Datadog and ensure you have:

  1. Datadog API credentials configured in your secrets
  2. AWS credentials configured in your secrets
  3. Lambda functions tagged with DD_SERVICE tag containing your Datadog service name

Code​

aws-lambda-datadog-logs-alerts.integration.example.js
module.exports = {
metadata: {
name: 'aws-lambda-datadog-logs-alerts',
description: 'Show any active alerts from Datadog for AWS Lambda functions, and jump from AWS Lambda to Logs and Alerts for this service',
match: {
contextType: 'aws',
context: {
'service.name': { equals: 'lambda' },
'service.arn': { exists: true },
'scope.region': { exists: true },
'service.resourceName': { exists: true }
}
},
requiredSecrets: ['DATADOG_API_KEY', 'DATADOG_APP_KEY', 'AWS_ACCESS_KEY_ID', 'AWS_SECRET_ACCESS_KEY'],
cache: 300
},
run: async function (context, secrets = {}) {
// adjust this to your Datadog instance
const DATADOG_HOST = 'https://api.datadoghq.com';

// Get AWS region from context
const awsRegion = context.scope.region;

const functionName = context.service.resourceName;

let ddServiceName = null;

try {
// Query AWS Lambda to get function tags
const { LambdaClient, ListTagsCommand } = require('@aws-sdk/client-lambda');

const lambdaClient = new LambdaClient({
credentials: {
accessKeyId: secrets.AWS_ACCESS_KEY_ID,
secretAccessKey: secrets.AWS_SECRET_ACCESS_KEY,
},
region: awsRegion
});

const command = new ListTagsCommand({ Resource: context.service.arn });
const tagsResponse = await lambdaClient.send(command);

// Look for DD_SERVICE tag
ddServiceName = tagsResponse.Tags?.DD_SERVICE || tagsResponse.Tags?.['dd-service'];

if (!ddServiceName) {
logger.warn({
operation: 'aws-lambda-datadog-logs-alerts',
step: 'no-dd-service-tag',
functionName: functionName
}, 'No DD_SERVICE tag found on Lambda function');
return [];
}
} catch (error) {
logger.error({
operation: 'aws-lambda-datadog-logs-alerts',
step: 'aws-lambda-error',
functionName: functionName,
error: error.message
}, 'Error querying AWS Lambda tags');
return [];
}

// Query Datadog for active alerts using the service tag
const serviceTag = `service:${ddServiceName}`;
const url = `${DATADOG_HOST}/api/v1/monitor?group_states=alert&monitor_tags=${encodeURIComponent(serviceTag)}`;

try {
const response = await fetch(url, {
method: 'GET',
headers: {
'DD-API-KEY': secrets.DATADOG_API_KEY,
'DD-APPLICATION-KEY': secrets.DATADOG_APP_KEY,
'Content-Type': 'application/json'
}
});

if (!response.ok) {
throw new Error(`Datadog API error: ${response.status} ${response.statusText}`);
}

let monitorList = await response.json();
// Filter monitorList to only include monitors with overall_state === "Alert"
monitorList = Array.isArray(monitorList)
? monitorList.filter(monitor => monitor.overall_state === "Alert")
: [];
const activeAlertsCount = monitorList.length || 0;

// Build Datadog logs and alerts URLs for this service
const logsUrl = `${DATADOG_HOST}/logs?query=${encodeURIComponent(serviceTag)}`;
const alertsUrl = `${DATADOG_HOST}/monitors/manage?q=tag%3A"${encodeURIComponent(serviceTag)}`;

const results = [
{
type: 'link',
content: `${activeAlertsCount} alert${activeAlertsCount === 1 ? '' : 's'}`,
href: alertsUrl,
status: activeAlertsCount > 0 ? 'important' : 'success',
icon: 'datadog'
},
{
type: 'link',
content: 'Logs',
href: logsUrl,
icon: 'datadog'
}
];

return results;

} catch (error) {
logger.error({
operation: 'aws-lambda-datadog-logs-alerts',
step: 'datadog-api-error',
ddServiceName: ddServiceName,
error: error.message
}, 'Error querying Datadog API');
return [];
}
}
};

GitHub β†’ Datadog: Show active alerts & logs

See active alerts for this repo at a glance
πŸ”’
https://github.com/mycompany/webshop
preload

How does this help you?​

  • You will notice any active alerts on the repo, which can help you fix issues faster
  • You will be able to navigate directly to the logs for the repo

Preview​

How it looks in the extension

ToolJumpβ€’datadog3 active alertsβ€’datadogLogsβ€’ ... Other items ...

High level approach​

We assume the alerts and logs are tagged with the repository name. We then query alerts in Datadog by the tag with the repo name, and filter the active alerts.

Prerequisites​

For the code below to work, please follow the guide on Connecting to Datadog.

Code​

github.datadog.logs-alerts.integration.example.js
module.exports = {
metadata: {
name: 'github-datadog-logs-alerts',
description: 'Show any active alerts from Datadog in Github, and jump from Github to Logs and Alerts for this repository',
match: {
contextType: 'github',
context: {
'page.repository': { startsWith: 'my-org/' }
}
},
requiredSecrets: ['DATADOG_API_KEY', 'DATADOG_APP_KEY'],
cache: 300
},
run: async function (context, secrets = {}) {

// adjust this to your Datadog instance
const DATADOG_HOST = 'https://api.datadoghq.com';

// assume the alerts for services belonging to this repository are tagged using the repository tag
const repoTag = `repository:${context.page.repository}`;

// if using Datadog EU instance, please change the url accordingly
const url = `${DATADOG_HOST}/api/v1/monitor?group_states=alert&monitor_tags=${encodeURIComponent(repoTag)}`;
const response = await fetch(url, {
method: 'GET',
headers: {
'DD-API-KEY': secrets.DATADOG_API_KEY,
'DD-APPLICATION-KEY': secrets.DATADOG_APP_KEY,
'Content-Type': 'application/json'
}
});

if (!response.ok) {
throw new Error(`Datadog API error: ${response.status} ${response.statusText}`);
}

monitorList = await response.json();
// Filter monitorList to only include monitors with overall_state === "Alert"
monitorList = Array.isArray(monitorList)
? monitorList.filter(monitor => monitor.overall_state === "Alert")
: [];
const activeAlertsCount = monitorList.length || 0;

// Build Datadog logs and alerts URLs for this repository
const logsUrl = `${DATADOG_HOST}/logs?query=${encodeURIComponent(repoTag)}`;
const alertsUrl = `${DATADOG_HOST}/monitors/manage?q=tag%3A"${encodeURIComponent(repoTag)}`;

const results = [
{
type: 'link',
content: `${activeAlertsCount} alert${activeAlertsCount === 1 ? '' : 's'}`,
href: alertsUrl,
status: activeAlertsCount > 0 ? 'important' : 'success',
icon: 'datadog'
},
{ type: 'link', content: 'Logs', href: logsUrl, icon: 'datadog' }
];

return results;
}
};