Skip to content

Commit d8583a6

Browse files
committed
Custom metrics refresh
1 parent fbef31f commit d8583a6

File tree

1 file changed

+89
-41
lines changed
  • content/en/serverless/custom_metrics

1 file changed

+89
-41
lines changed

content/en/serverless/custom_metrics/_index.md

Lines changed: 89 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -58,28 +58,94 @@ Datadog recommends using the [Datadog Lambda Extension][1] to submit custom metr
5858
1. Follow the general [serverless installation instructions][8] appropriate for your Lambda runtime.
5959
1. If you are not interested in collecting traces from your Lambda function, set the environment variable `DD_TRACE_ENABLED` to `false`.
6060
1. If you are not interested in collecting logs from your Lambda function, set the environment variable `DD_SERVERLESS_LOGS_ENABLED` to `false`.
61-
1. Import and use the helper function from the Datadog Lambda Library, such as `lambda_metric` or `sendDistributionMetric`, to submit your custom metrics following the [sample code](#custom-metrics-sample-code).
61+
1. Follow the sample code or instructions belows to submit your custom metric.
6262

63-
If your Lambda function is running in a VPC, ensure that your function can reach Datadog API endpoints either through the public internet, [PrivateLink][9] or a [proxy][10].
63+
{{< programming-lang-wrapper langs="python,nodeJS,go,ruby,other" >}}
64+
{{< programming-lang lang="python" >}}
6465

65-
## With the Datadog Forwarder
66+
```python
67+
from datadog_lambda.metric import lambda_metric
6668

67-
Datadog recommends using the [Datadog Forwarder Lambda][11] to submit custom metrics from Lambda runtimes that are not yet supported by the Datadog Lambda Extension.
69+
def lambda_handler(event, context):
70+
lambda_metric(
71+
"coffee_house.order_value", # Metric name
72+
12.45, # Metric value
73+
tags=['product:latte', 'order:online'] # Associated tags
74+
)
75+
```
76+
{{< /programming-lang >}}
77+
{{< programming-lang lang="nodeJS" >}}
6878

69-
1. Follow the general [serverless installation instructions][8] to configure your Lambda function, install the Datadog Lambda Library and the Datadog Forwarder Lambda function, and subscribe the Forwarder to your function's log group.
70-
1. If you are not interested in collecting traces from your Lambda function, set the environment variable `DD_TRACE_ENABLED` to `false` on your own Lambda function.
71-
1. If you are not interested in collecting logs from your Lambda function, set the Forwarder's CloudFormation stack parameter `DdForwardLog` to `false`.
72-
1. Import and use the helper function from the Datadog Lambda Library, such as `lambda_metric` or `sendDistributionMetric`, to submit your custom metrics following the [sample code](#custom-metrics-sample-code).
79+
```javascript
80+
const { sendDistributionMetric } = require('datadog-lambda-js');
7381

74-
If the Datadog Lambda Library is not available for your runtime, you can print metrics to CloudWatch logs in the expected JSON format on your own. Select the "Other" tab from the [sample code](#custom-metrics-sample-code) section.
82+
async function myHandler(event, context) {
83+
sendDistributionMetric(
84+
'coffee_house.order_value', // Metric name
85+
12.45, // Metric value
86+
'product:latte', // First tag
87+
'order:online' // Second tag
88+
);
89+
}
90+
```
91+
{{< /programming-lang >}}
92+
{{< programming-lang lang="go" >}}
7593

76-
## Custom metrics sample code
94+
```go
95+
package main
7796

78-
**Note:** The arguments to the custom metrics reporting methods have the following requirements:
97+
import (
98+
"github.com/aws/aws-lambda-go/lambda"
99+
"github.com/DataDog/datadog-lambda-go"
100+
)
79101

80-
- `<METRIC_NAME>` uniquely identifies your metric and follows the [metric naming policy][12].
81-
- `<METRIC_VALUE>` MUST be a number (that is, integer or float).
82-
- `<TAG_LIST>` is optional and formatted, for example: `['owner:Datadog', 'env:demo', 'cooltag']`.
102+
func main() {
103+
lambda.Start(ddlambda.WrapFunction(myHandler, nil))
104+
}
105+
106+
func myHandler(ctx context.Context, event MyEvent) (string, error) {
107+
ddlambda.Distribution(
108+
"coffee_house.order_value", // Metric name
109+
12.45, // Metric value
110+
"product:latte", "order:online" // Associated tags
111+
)
112+
}
113+
```
114+
115+
{{< /programming-lang >}}
116+
{{< programming-lang lang="ruby" >}}
117+
118+
```ruby
119+
require 'datadog/lambda'
120+
121+
def handler(event:, context:)
122+
# You only need to wrap your function handler (Not helper functions).
123+
Datadog::Lambda.wrap(event, context) do
124+
Datadog::Lambda.metric(
125+
'coffee_house.order_value', # Metric name
126+
12.45, # Metric value
127+
"product":"latte", "order":"online" # Associated tags
128+
)
129+
end
130+
end
131+
```
132+
133+
{{< /programming-lang >}}
134+
{{< programming-lang lang="other" >}}
135+
136+
[Install][14] the DogStatsD client for your runtime and follow the [sample code][15] to submit your custom metrics. Note: For accurate results, you must use [**distribution**](#understanding-distribution-metrics).
137+
138+
{{< /programming-lang >}}
139+
{{< /programming-lang-wrapper >}}
140+
141+
## With the Datadog Forwarder
142+
143+
Datadog recommends using the [Datadog Forwarder Lambda][9] to submit custom metrics from Lambda runtimes that are not yet supported by the Datadog Lambda Extension.
144+
145+
1. Follow the general [serverless installation instructions][8] to instrument your Lambda function using the Datadog Forwarder Lambda function.
146+
1. If you are not interested in collecting traces from your Lambda function, set the environment variable `DD_TRACE_ENABLED` to `false` on your own Lambda function.
147+
1. If you are not interested in collecting logs from your Lambda function, set the Forwarder's CloudFormation stack parameter `DdForwardLog` to `false`.
148+
1. Import and use the helper function from the Datadog Lambda Library, such as `lambda_metric` or `sendDistributionMetric`, to submit your custom metrics following the sample code below.
83149

84150
{{< programming-lang-wrapper langs="python,nodeJS,go,ruby,java,other" >}}
85151
{{< programming-lang lang="python" >}}
@@ -94,8 +160,7 @@ def lambda_handler(event, context):
94160
tags=['product:latte', 'order:online'] # Associated tags
95161
)
96162

97-
# Submit a metric with a timestamp that is within the last 20 minutes,
98-
# only supported when using the Forwarder Lambda
163+
# Submit a metric with a timestamp that is within the last 20 minutes
99164
lambda_metric(
100165
"coffee_house.order_value", # Metric name
101166
12.45, # Metric value
@@ -117,19 +182,14 @@ async function myHandler(event, context) {
117182
'order:online' // Second tag
118183
);
119184

120-
// Submit a metric with a timestamp that is within the last 20 minutes,
121-
// only supported when using the Forwarder Lambda
185+
// Submit a metric with a timestamp that is within the last 20 minutes
122186
sendDistributionMetricWithDate(
123187
'coffee_house.order_value', // Metric name
124188
12.45, // Metric value
125189
'product:latte', // First tag
126190
'order:online' // Second tag
127191
new Date(Date.now()), // date
128192
);
129-
return {
130-
statusCode: 200,
131-
body: 'hello, dog!'
132-
};
133193
}
134194
```
135195
{{< /programming-lang >}}
@@ -144,14 +204,7 @@ import (
144204
)
145205

146206
func main() {
147-
// You only need to wrap your function handler (Not helper functions).
148207
lambda.Start(ddlambda.WrapFunction(myHandler, nil))
149-
/* OR with manual configuration options
150-
lambda.Start(ddlambda.WrapFunction(myHandler, &ddlambda.Config{
151-
BatchInterval: time.Second * 15
152-
APIKey: "my-api-key",
153-
}))
154-
*/
155208
}
156209

157210
func myHandler(ctx context.Context, event MyEvent) (string, error) {
@@ -161,8 +214,7 @@ func myHandler(ctx context.Context, event MyEvent) (string, error) {
161214
"product:latte", "order:online" // Associated tags
162215
)
163216

164-
// Submit a metric with a timestamp that is within the last 20 minutes,
165-
// only supported when using the Forwarder Lambda
217+
// Submit a metric with a timestamp that is within the last 20 minutes
166218
ddlambda.MetricWithTimestamp(
167219
"coffee_house.order_value", // Metric name
168220
12.45, // Metric value
@@ -187,15 +239,13 @@ def handler(event:, context:)
187239
"product":"latte", "order":"online" # Associated tags
188240
)
189241

190-
# Submit a metric with a timestamp that is within the last 20 minutes,
191-
# only supported when using the Forwarder Lambda
242+
# Submit a metric with a timestamp that is within the last 20 minutes
192243
Datadog::Lambda.metric(
193244
'coffee_house.order_value', # Metric name
194245
12.45, # Metric value
195246
time: Time.now.utc, # Timestamp
196247
"product":"latte", "order":"online" # Associated tags
197248
)
198-
return { statusCode: 200, body: 'Hello World' }
199249
end
200250
end
201251
```
@@ -254,7 +304,7 @@ For example:
254304

255305
**Note**: If you are migrating to one of the recommended solutions, you'll need to start instrumenting your custom metrics under **new metric names** when submitting them to Datadog. The same metric name cannot simultaneously exist as both distribution and non-distribution metric types.
256306

257-
This requires the following AWS permissions in your [Datadog IAM policy][13].
307+
This requires the following AWS permissions in your [Datadog IAM policy][10].
258308

259309
| AWS Permission | Description |
260310
| ------------------------- | ----------------------------------------------------------- |
@@ -274,7 +324,7 @@ Where:
274324
- `<UNIX_EPOCH_TIMESTAMP>` is in seconds, not milliseconds.
275325
- `<METRIC_VALUE>` MUST be a number (that is, integer or float).
276326
- `<METRIC_TYPE>` is `count`, `gauge`, `histogram`, or `check`.
277-
- `<METRIC_NAME>` uniquely identifies your metric and follows the [metric naming policy][12].
327+
- `<METRIC_NAME>` uniquely identifies your metric and follows the [metric naming policy][11].
278328
- `<TAG_LIST>` is optional, comma separated, and must be preceded by `#`. The tag `function_name:<name_of_the_function>` is automatically applied to custom metrics.
279329

280330
**Note**: The sum for each timestamp is used for counts and the last value for a given timestamp is used for gauges. It is not recommended to print a log statement every time you increment a metric, as this increases the time it takes to parse your logs. Continually update the value of the metric in your code, and print one log statement for that metric before the function finishes.
@@ -287,8 +337,6 @@ Where:
287337
[6]: /logs/logs_to_metrics/
288338
[7]: /tracing/generate_metrics/
289339
[8]: /serverless/installation/
290-
[9]: /agent/guide/private-link/
291-
[10]: /agent/proxy/
292-
[11]: /serverless/forwarder/
293-
[12]: /metrics/
294-
[13]: /integrations/amazon_web_services/?tab=roledelegation#datadog-aws-iam-policy
340+
[9]: /serverless/forwarder/
341+
[10]: /integrations/amazon_web_services/?tab=roledelegation#datadog-aws-iam-policy
342+
[11]: /metrics/

0 commit comments

Comments
 (0)