Skip to content

AWS Lambda function to relay fingerprint-ed / normalized MySQL Slow Query logs to Datadog.

License

Notifications You must be signed in to change notification settings

samitani/mysql-slow-query-datadog-lambda

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mysql-slow-query-datadog-lambda

AWS Lambda function to relay fingerprint-ed / normalized MySQL Slow Query logs to Datadog.

This function normalizes SQL like below to aggregate metrics on Datadog.

SELECT id, name FROM tbl WHERE id = "1000"` => `SELECT id, name FROM tbl WHERE id = ?
SELECT id, name FROM tbl WHERE id IN (10, 20, 30)` => `SELECT id, name FROM tbl WHERE id IN (?+)

How to use

Export Slow query logs to CloudWatch Logs

  1. Enable slow_query_log parameter of your RDS database instance

  2. Modify your database instance to export slow query

Create Datadog API Key secrets on Secrets Manager

  1. Create Datadog API Key secrets with PLAIN TEXT format

You can find your API Key here: https://app.datadoghq.com/account/settings#api

Create Lambda function

  1. Download function.zip from github

https://github.com/samitani/mysql-slow-query-datadog-lambda/releases

  1. Create Lambda function with downloaded function.zip

Specify Python3 as Runtime, main.lambda_handler as Handler

  1. Configure Lambda Environments below
Key Value
DD_API_KEY_SECRET_ARN AWS Secret Manager ARN of Datadog API KEY.
eg) arn:aws:secretsmanager:ap-northeast-1:XXXXXXXXX:secret:DdApiKeySecret-XXXXXXXX
DD_ENHANCED_METRICS false
DD_SITE datadoghq.com
  1. Edit IAM role to allow this lambda function to get secrets

  2. Create Lambda Subscription filter against your Slow Query log CloudWatch Log groups

Datadog

Generate Metrics with below Grok parser.

SlowLogRule ^(\# Time: (%{date("yyMMdd  H:mm:ss"):date}|%{date("yyMMdd HH:mm:ss"):date})\n+)?\# User@Host: %{notSpace: user1}\[%{notSpace: user2}\] @ (%{notSpace: host}| ) *\[%{regex("[0-9.]*"): ip}\]  Id:[\x20\t]+%{number: id}\n+\# Query_time: %{number: query_time} *Lock_time: %{number: lock_time} *Rows_sent: %{number: rows_sent} *Rows_examined: %{number: rows_examined}\n(SET timestamp=%{number: timestamp};\n+)?%{regex("[a-zA-Z].*"):query}.

Example

image image

Note

enhanced_lambda_metrics.py and lambda_function.py were borrowed from below Datadog repository.

https://github.com/DataDog/datadog-serverless-functions

About

AWS Lambda function to relay fingerprint-ed / normalized MySQL Slow Query logs to Datadog.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published