Last active
September 14, 2020 08:50
-
-
Save simonespa/74c5c51ebb7800b7553bfa92fb172f3e to your computer and use it in GitHub Desktop.
AWS Embedded Metrics usage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const aws = require("aws-sdk"); | |
| const cloudwatchlogs = new aws.CloudWatchLogs({ | |
| region: "eu-west-1", | |
| }); | |
| const m = { | |
| requestMethod: 'get', | |
| timestamp: 123432039483, | |
| status: '200' | |
| }; | |
| const logGroupName = '/nginx/logGroup'; | |
| const logStreamName = '/nginx/logStream'; | |
| (async () => { | |
| cloudwatchlogs.createLogGroup({ logGroupName }, (err, data) => { | |
| if (err) { | |
| return Promise.reject(err); | |
| } | |
| console.log(data); | |
| cloudwatchlogs.createLogStream({ logGroupName, logStreamName }, (err, data) => { | |
| if (err) { | |
| return Promise.reject(err); | |
| } | |
| console.log(data); | |
| cloudwatchlogs.putLogEvents({ | |
| logEvents: [ | |
| { | |
| message: JSON.stringify(m), | |
| timestamp: Date.now(), | |
| }, | |
| ], | |
| logGroupName, | |
| logStreamName | |
| }) | |
| .on("build", (req) => { | |
| req.httpRequest.headers["x-amzn-logs-format"] = "json/emf"; | |
| }) | |
| .send((err, data) => { | |
| if (err) { | |
| return Promise.reject(err); | |
| } else { | |
| console.log(data); | |
| return Promise.resolve(data); | |
| } | |
| }); | |
| }); | |
| }); | |
| })(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const aws = require("aws-sdk"); | |
| const cloudwatchlogs = new aws.CloudWatchLogs({ | |
| region: "eu-west-1", | |
| }); | |
| const logGroupName = "/awslabs/emf"; | |
| const namespace1 = "/awslabs/emf/default"; | |
| const namespace2 = "/awslabs/emf/default-multi-value"; | |
| const namespace3 = "/awslabs/emf/new"; | |
| const namespace4 = "/awslabs/emf/new-multi-value"; | |
| const logStreamNames = [namespace1, namespace2, namespace3, namespace4]; | |
| const message1 = { | |
| RequestCount: 1, | |
| ResponseTime: 59, | |
| UpstreamRequestCount: 1, | |
| PageType: "player", | |
| Client: "rms", | |
| _aws: { | |
| Timestamp: Date.now(), | |
| CloudWatchMetrics: [ | |
| { | |
| Namespace: namespace1, | |
| Dimensions: [["PageType"], ["Client"]], | |
| Metrics: [ | |
| { | |
| Name: "RequestCount", | |
| Unit: "Count", | |
| }, | |
| { | |
| Name: "ResponseTime", | |
| Unit: "Milliseconds", | |
| }, | |
| { | |
| Name: "UpstreamRequestCount", | |
| Unit: "Milliseconds", | |
| }, | |
| ], | |
| }, | |
| ], | |
| }, | |
| }; | |
| const message2 = { | |
| RequestCount: [1, 1, 1], | |
| ResponseTime: [150, 60, 49], | |
| UpstreamRequestCount: [1, 1, 1], | |
| PageType: "player", | |
| Client: "rms", | |
| _aws: { | |
| Timestamp: Date.now(), | |
| CloudWatchMetrics: [ | |
| { | |
| Namespace: namespace2, | |
| Dimensions: [["PageType"], ["Client"]], | |
| Metrics: [ | |
| { | |
| Name: "RequestCount", | |
| Unit: "Count", | |
| }, | |
| { | |
| Name: "ResponseTime", | |
| Unit: "Milliseconds", | |
| }, | |
| { | |
| Name: "UpstreamRequestCount", | |
| Unit: "Milliseconds", | |
| }, | |
| ], | |
| }, | |
| ], | |
| }, | |
| }; | |
| const message3 = { | |
| RequestCount: 1, | |
| ResponseTime: 59, | |
| UpstreamRequestCount: 1, | |
| PageType: "player", | |
| Client: "rms", | |
| _aws: { | |
| Timestamp: Date.now(), | |
| CloudWatchMetrics: [ | |
| { | |
| Namespace: namespace3, | |
| Dimensions: [["PageType"]], | |
| Metrics: [ | |
| { | |
| Name: "RequestCount", | |
| Unit: "Count", | |
| }, | |
| { | |
| Name: "ResponseTime", | |
| Unit: "Milliseconds", | |
| }, | |
| ], | |
| }, | |
| { | |
| Namespace: namespace3, | |
| Dimensions: [["Client"]], | |
| Metrics: [ | |
| { | |
| Name: "UpstreamRequestCount", | |
| Unit: "Milliseconds", | |
| }, | |
| ], | |
| }, | |
| ], | |
| }, | |
| }; | |
| const message4 = { | |
| RequestCount: [1, 1, 1], | |
| ResponseTime: [150, 60, 49], | |
| UpstreamRequestCount: [1, 1, 1], | |
| PageType: "player", | |
| Client: "rms", | |
| _aws: { | |
| Timestamp: Date.now(), | |
| CloudWatchMetrics: [ | |
| { | |
| Namespace: namespace4, | |
| Dimensions: [["PageType"]], | |
| Metrics: [ | |
| { | |
| Name: "RequestCount", | |
| Unit: "Count", | |
| }, | |
| { | |
| Name: "ResponseTime", | |
| Unit: "Milliseconds", | |
| }, | |
| ], | |
| }, | |
| { | |
| Namespace: namespace4, | |
| Dimensions: [["Client"]], | |
| Metrics: [ | |
| { | |
| Name: "UpstreamRequestCount", | |
| Unit: "Milliseconds", | |
| }, | |
| ], | |
| }, | |
| ], | |
| }, | |
| }; | |
| /** | |
| * Creates the log group and its log streams | |
| */ | |
| async function initLogs() { | |
| cloudwatchlogs.createLogGroup({ logGroupName }, (err, data) => { | |
| if (err) { | |
| return Promise.reject(err); | |
| } | |
| console.log(data); | |
| logStreamNames.forEach((logStreamName) => { | |
| cloudwatchlogs.createLogStream({ logGroupName, logStreamName }, (err, data) => { | |
| if (err) { | |
| return Promise.reject(err); | |
| } | |
| console.log(data); | |
| return Promise.resolve(data); | |
| }); | |
| }); | |
| }); | |
| } | |
| /** | |
| * Publishes the EMF logs | |
| */ | |
| async function publishEmf() { | |
| cloudwatchlogs | |
| .putLogEvents({ | |
| logEvents: [ | |
| { | |
| message: JSON.stringify(message4), | |
| timestamp: Date.now(), | |
| }, | |
| ], | |
| logGroupName: logGroupName, | |
| logStreamName: namespace4 | |
| }) | |
| .on("build", (req) => { | |
| req.httpRequest.headers["x-amzn-logs-format"] = "json/emf"; | |
| }) | |
| .send((err, data) => { | |
| if (err) { | |
| console.log(err, err.stack); | |
| } else { | |
| console.log(data); | |
| } | |
| }); | |
| } | |
| (async () => { | |
| // await initLogs(); | |
| publishEmf(); | |
| // cloudwatchlogs.describeLogStreams({ logGroupName }, (err, data) => { | |
| // if (err) { | |
| // console.log(err, err.stack); | |
| // } else { | |
| // console.log(data); | |
| // } | |
| // }); | |
| })(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const message = { | |
| "Metric1": "metric_1", | |
| "DimensionX": "dimension_x", | |
| "DimensionY": "dimension_y", | |
| "Metric2": "metric_2", | |
| "DimensionA": "dimension_a", | |
| "DimensionB": "dimension_b", | |
| "Metric3": "metric_3", | |
| "_aws": { | |
| "Timestamp": Date.now(), | |
| "CloudWatchMetrics": [ | |
| { | |
| "Namespace": "/awslabs/aws-embedded-metrics-node/test1", | |
| "Dimensions": [["DimensionX", "DimensionY"], ["DimensionA", "DimensionB"], ["DimensionX"]], | |
| "Metrics": [ | |
| { | |
| "Name": "Metric1", | |
| "Unit": "Count" | |
| }, | |
| { | |
| "Name": "Metric2", | |
| "Unit": "Count" | |
| }, | |
| { | |
| "Name": "Metric3", | |
| "Unit": "Milliseconds" | |
| } | |
| ] | |
| } | |
| ] | |
| } | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const message = { | |
| "Metric1": "metric_1", | |
| "DimensionX": "dimension_x", | |
| "DimensionY": "dimension_y", | |
| "Metric2": "metric_2", | |
| "DimensionA": "dimension_a", | |
| "DimensionB": "dimension_b", | |
| "Metric3": "metric_3", | |
| "_aws": { | |
| "Timestamp": Date.now(), | |
| "CloudWatchMetrics": [ | |
| { | |
| "Namespace": "/awslabs/aws-embedded-metrics-node/test2", | |
| "Dimensions": [["DimensionX", "DimensionY"]], | |
| "Metrics": [ | |
| { | |
| "Name": "Metric1", | |
| "Unit": "Count" | |
| } | |
| ] | |
| }, | |
| { | |
| "Namespace": "/awslabs/aws-embedded-metrics-node/test2", | |
| "Dimensions": [["DimensionA", "DimensionB"]], | |
| "Metrics": [ | |
| { | |
| "Name": "Metric2", | |
| "Unit": "Count" | |
| } | |
| ] | |
| }, | |
| { | |
| "Namespace": "/awslabs/aws-embedded-metrics-node/test2", | |
| "Dimensions": [["DimensionX"]], | |
| "Metrics": [ | |
| { | |
| "Name": "Metric3", | |
| "Unit": "Count" | |
| } | |
| ] | |
| } | |
| ] | |
| } | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const aws = require('aws-sdk'); | |
| const cloudwatchlogs = new aws.CloudWatchLogs({ | |
| region: 'eu-west-1' | |
| }); | |
| const message = { | |
| // https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch_Embedded_Metric_Format_Specification.html#CloudWatch_Embedded_Metric_Format_Specification_structure_metricdefinition | |
| }; | |
| const params = { | |
| logEvents: [ | |
| { | |
| message: JSON.stringify(message), | |
| timestamp: Date.now() | |
| } | |
| ], | |
| logGroupName: '/<NAMESPACE>/<SERVICE_NAME>', | |
| logStreamName: '<EC2_INSTANCE_ID>_<IP>', | |
| sequenceToken: '' // this token can be retrieved in advance, or grab it from the error you get if you run this script without it | |
| }; | |
| cloudwatchlogs.putLogEvents(params).on('build', (req) => { | |
| req.httpRequest.headers['x-amzn-logs-format'] = 'json/emf'; | |
| }).send((err, data) => { | |
| if (err) { | |
| console.log(err, err.stack); | |
| } else { | |
| console.log(data); | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment