|
| 1 | +--- |
| 2 | +title: Install Serverless Monitoring for Azure Functions |
| 3 | +--- |
| 4 | + |
| 5 | +## Overview |
| 6 | +This page explains how to collect traces, trace metrics, runtime metrics, and custom metrics from your Azure Functions. To collect additional metrics, install the [Datadog Azure integration][6]. |
| 7 | + |
| 8 | +## Setup |
| 9 | + |
| 10 | +{{< programming-lang-wrapper langs="nodejs,python" >}} |
| 11 | +{{< programming-lang lang="nodejs" >}} |
| 12 | +1. **Install dependencies**. Run the following commands: |
| 13 | + ```shell |
| 14 | + npm install @datadog/serverless-compat |
| 15 | + npm install dd-trace |
| 16 | + ``` |
| 17 | + |
| 18 | + To use [automatic instrumentation][1], you must use `dd-trace` v5.25+. |
| 19 | + |
| 20 | + Datadog recommends pinning the package versions and regularly upgrading to the latest versions of both `@datadog/serverless-compat` and `dd-trace` to ensure you have access to enhancements and bug fixes. |
| 21 | + |
| 22 | +2. **Start the Datadog serverless compatibility layer and initialize the Node.js tracer**. Add the following lines to your main application entry point file (for example, `app.js`): |
| 23 | + |
| 24 | + ```js |
| 25 | + require('@datadog/serverless-compat').start(); |
| 26 | + |
| 27 | + // This line must come before importing any instrumented module. |
| 28 | + const tracer = require('dd-trace').init() |
| 29 | + ``` |
| 30 | + |
| 31 | +3. (Optional) **Enable runtime metrics**. See [Node.js Runtime Metrics][2]. |
| 32 | + |
| 33 | +4. (Optional) **Enable custom metrics**. See [Metric Submission: DogStatsD][3]. |
| 34 | + |
| 35 | +[1]: /tracing/trace_collection/automatic_instrumentation/?tab=singlestepinstrumentation |
| 36 | +[2]: /tracing/metrics/runtime_metrics/nodejs/?tab=environmentvariables |
| 37 | +[3]: /metrics/custom_metrics/dogstatsd_metrics_submission/?code-lang=nodejs |
| 38 | +{{< /programming-lang >}} |
| 39 | +{{< programming-lang lang="python" >}} |
| 40 | +1. **Install dependencies**. Run the following commands: |
| 41 | + ```shell |
| 42 | + pip install datadog-serverless-compat |
| 43 | + pip install ddtrace |
| 44 | + ``` |
| 45 | + |
| 46 | + To use [automatic instrumentation][1], you must use `dd-trace` v2.19+. |
| 47 | + |
| 48 | + Datadog recommends using the latest versions of both `datadog-serverless-compat` and `ddtrace` to ensure you have access to enhancements and bug fixes. |
| 49 | + |
| 50 | +2. **Initialize the Datadog Python tracer and serverless compatibility layer**. Add the following lines to your main application entry point file: |
| 51 | + |
| 52 | + ```python |
| 53 | + from datadog_serverless_compat import start |
| 54 | + from ddtrace import tracer, patch_all |
| 55 | + |
| 56 | + start() |
| 57 | + patch_all() |
| 58 | + ``` |
| 59 | + |
| 60 | +3. (Optional) **Enable runtime metrics**. See [Python Runtime Metrics][2]. |
| 61 | + |
| 62 | +4. (Optional) **Enable custom metrics**. See [Metric Submission: DogStatsD][3]. |
| 63 | + |
| 64 | +[1]: /tracing/trace_collection/automatic_instrumentation/?tab=singlestepinstrumentation |
| 65 | +[2]: /tracing/metrics/runtime_metrics/python/ |
| 66 | +[3]: /metrics/custom_metrics/dogstatsd_metrics_submission/?code-lang=python |
| 67 | +{{< /programming-lang >}} |
| 68 | +{{< /programming-lang-wrapper >}} |
| 69 | + |
| 70 | +5. **Deploy your function**. |
| 71 | + |
| 72 | +6. **Configure Datadog intake**. Add the following environment variables to your function's application settings: |
| 73 | + |
| 74 | + | Name | Value | |
| 75 | + | ---- | ----- | |
| 76 | + | `DD_API_KEY` | Your [Datadog API key][1]. | |
| 77 | + | `DD_SITE` | Your [Datadog site][2]. For example, {{< region-param key=dd_site code="true" >}}. | |
| 78 | + |
| 79 | +7. **Configure Unified Service Tagging**. You can collect metrics from your Azure Functions by installing the [Datadog Azure integration][6]. To correlate these metrics with your traces, first set the `env`, `service`, and `version` tags on your resource in Azure. Then, configure the following environment variables. You can add custom tags as `DD_TAGS`. |
| 80 | + |
| 81 | + | Name | Value | |
| 82 | + | ---- | ----- | |
| 83 | + | `DD_ENV` | How you want to tag your env for [Unified Service Tagging][9]. For example, `prod`. | |
| 84 | + | `DD_SERVICE` | How you want to tag your service for [Unified Service Tagging][9]. | |
| 85 | + | `DD_VERSION` | How you want to tag your version for [Unified Service Tagging][9]. | |
| 86 | + | `DD_TAGS` | Your comma-separated custom tags. For example, `key1:value1,key2:value2`. | |
| 87 | + |
| 88 | +## What's next? |
| 89 | + |
| 90 | +- You can view your Azure Functions traces in [Trace Explorer][4]. Search for the service name you set in the `DD_SERVICE` environment variable to see your traces. |
| 91 | +- You can use the [Serverless > Azure Functions][5] page to see your traces enriched with telemetry collected by the [Datadog Azure integration][6]. |
| 92 | + |
| 93 | +### Enable/disable trace metrics |
| 94 | + |
| 95 | +[Trace metrics][3] are enabled by default. To configure trace metrics, use the following environment variable: |
| 96 | + |
| 97 | +`DD_TRACE_STATS_COMPUTATION_ENABLED` |
| 98 | +: Enables (`true`) or disables (`false`) trace metrics. Defaults to `true`. |
| 99 | + |
| 100 | + **Values**: `true`, `false` |
| 101 | + |
| 102 | +## Troubleshooting |
| 103 | + |
| 104 | +### Enable debug logs |
| 105 | + |
| 106 | +You can collect [debug logs][7] for troubleshooting. To configure debug logs, use the following environment variables: |
| 107 | + |
| 108 | +`DD_TRACE_DEBUG` |
| 109 | +: Enables (`true`) or disables (`false`) debug logging for the Datadog Tracing Library. Defaults to `false`. |
| 110 | + |
| 111 | + **Values**: `true`, `false` |
| 112 | + |
| 113 | +`DD_LOG_LEVEL` |
| 114 | +: Sets logging level for the Datadog Serverless Compatibility Layer. Defaults to `info`. |
| 115 | + |
| 116 | + **Values**: `trace`, `debug`, `info`, `warn`, `error`, `critical`, `off` |
| 117 | + |
| 118 | +### Linux Consumption plans and GitHub Actions |
| 119 | + |
| 120 | +To use a GitHub Action to deploy to a Linux Consumption function, you must configure your workflow to use an Azure Service Principal for RBAC. See [Using Azure Service Principal for RBAC as Deployment Credential][8]. |
| 121 | + |
| 122 | + |
| 123 | +[1]: /account_management/api-app-keys/#add-an-api-key-or-client-token |
| 124 | +[2]: /getting_started/site |
| 125 | +[3]: /tracing/metrics/metrics_namespace/ |
| 126 | +[4]: https://app.datadoghq.com/apm/traces |
| 127 | +[5]: https://app.datadoghq.com/functions?cloud=azure&entity_view=function |
| 128 | +[6]: /integrations/azure/ |
| 129 | +[7]: /tracing/troubleshooting/tracer_debug_logs/#enable-debug-mode |
| 130 | +[8]: https://github.com/Azure/functions-action?tab=readme-ov-file#using-azure-service-principal-for-rbac-as-deployment-credential |
| 131 | +[9]: /getting_started/tagging/unified_service_tagging/ |
0 commit comments