|
| 1 | +:framework: Azure Functions |
| 2 | + |
| 3 | +[[azure-functions]] |
| 4 | + |
| 5 | +ifdef::env-github[] |
| 6 | +NOTE: For the best reading experience, |
| 7 | +please view this documentation at https://www.elastic.co/guide/en/apm/agent/nodejs/current/azure-functions.html[elastic.co] |
| 8 | +endif::[] |
| 9 | + |
| 10 | +=== Monitoring Node.js Azure Functions |
| 11 | + |
| 12 | +The Node.js APM Agent can trace function invocations in an https://learn.microsoft.com/en-us/azure/azure-functions/[Azure Functions] app. |
| 13 | + |
| 14 | + |
| 15 | +[float] |
| 16 | +[[azure-functions-prerequisites]] |
| 17 | +==== Prerequisites |
| 18 | + |
| 19 | +You need an APM Server to send APM data to. Follow the |
| 20 | +{apm-guide-ref}/apm-quick-start.html[APM Quick start] if you have not set one up |
| 21 | +yet. You will need your *APM server URL* and an APM server *secret token* (or |
| 22 | +*API key*) for configuring the APM agent below. |
| 23 | + |
| 24 | +You will also need an Azure Function app to monitor. If you do not have an |
| 25 | +existing one, you can follow https://learn.microsoft.com/en-us/azure/azure-functions/create-first-function-cli-node#create-supporting-azure-resources-for-your-function[this Azure guide] |
| 26 | +to create one. |
| 27 | + |
| 28 | +[IMPORTANT] |
| 29 | +==== |
| 30 | +If you use `func init --javascript ...` as suggested in this Azure guide, |
| 31 | +then it is recommended that you *uninstall* the `azure-functions-core-tools` |
| 32 | +dependency by running `npm uninstall azure-functions-core-tools` and |
| 33 | +https://github.com/Azure/azure-functions-core-tools#installing[install it separately]. |
| 34 | +Having `azure-functions-core-tools` as a "devDependency" in your package.json |
| 35 | +will result in unreasonably large deployments that will be very slow to publish |
| 36 | +and will run your Azure Function app VM out of disk space. |
| 37 | +==== |
| 38 | + |
| 39 | +You can also take a look at and use this https://github.com/elastic/apm-agent-nodejs/tree/main/examples/an-azure-function-app/[Azure Functions example app with Elastic APM already integrated]. |
| 40 | + |
| 41 | +[float] |
| 42 | +[[azure-functions-setup]] |
| 43 | +==== Step 1: Add the APM agent dependency |
| 44 | + |
| 45 | +Add the `elastic-apm-node` module as a dependency of your application: |
| 46 | + |
| 47 | +[source,bash] |
| 48 | +---- |
| 49 | +npm install elastic-apm-node --save # or 'yarn add elastic-apm-node' |
| 50 | +---- |
| 51 | + |
| 52 | + |
| 53 | +[float] |
| 54 | +==== Step 2: Start the APM agent |
| 55 | + |
| 56 | +For the APM agent to instrument Azure Functions, it needs to be started when the |
| 57 | +Azure host starts its Node.js worker processes. The best way to do so is by |
| 58 | +using an app-level entry point (support for this was added for Node.js Azure |
| 59 | +Functions https://github.com/Azure/azure-functions-nodejs-worker/issues/537[here]). |
| 60 | + |
| 61 | +1. Create a module to start the APM agent. For example, a file at the root of your repository named "initapm.js": |
| 62 | ++ |
| 63 | +[source,javascript] |
| 64 | +---- |
| 65 | +// initapm.js |
| 66 | +require('elastic-apm-node').start({ |
| 67 | + <1> |
| 68 | +}) |
| 69 | +---- |
| 70 | +<1> Optional <<configuration,configuration options>> can be added here. |
| 71 | + |
| 72 | +2. Add a "main" entry to your package.json pointing to the app init file. |
| 73 | ++ |
| 74 | +[source,json] |
| 75 | +---- |
| 76 | +... |
| 77 | + "main": "initapm.js", |
| 78 | +... |
| 79 | +---- |
| 80 | ++ |
| 81 | +If your application already has a "main" init file, you can instead add the |
| 82 | +`require('elastic-apm-node').start()` to top of that file. |
| 83 | + |
| 84 | + |
| 85 | +[float] |
| 86 | +==== Step 3: Configure the APM agent |
| 87 | + |
| 88 | +The APM agent can be <<configuring-the-agent,configured>> with options to the |
| 89 | +`.start()` method or with environment variables. Using environment variables |
| 90 | +allows one to use https://learn.microsoft.com/en-us/azure/azure-functions/functions-how-to-use-azure-function-app-settings?tabs=portal#settings[application settings in the Azure Portal] which allows hiding values and updating settings |
| 91 | +without needing to re-deploy code. |
| 92 | + |
| 93 | +Open _Configuration > Application settings_ for your Function App in the Azure Portal |
| 94 | +and set: |
| 95 | + |
| 96 | +[source,yaml] |
| 97 | +---- |
| 98 | +ELASTIC_APM_SERVER_URL: <your APM server URL from the prerequisites step> |
| 99 | +ELASTIC_APM_SECRET_TOKEN: <your APM secret token from the prerequisites step> |
| 100 | +---- |
| 101 | + |
| 102 | +For example: |
| 103 | + |
| 104 | +image::./images/azure-functions-configuration.png[Configuring the APM Agent in the Azure Portal] |
| 105 | + |
| 106 | +For local testing via `func start` you can set these environment variables in |
| 107 | +your terminal, or in the "local.settings.json" file. See the |
| 108 | +<<configuration,agent configuration guide>> for full details on supported |
| 109 | +configuration variables. |
| 110 | + |
| 111 | + |
| 112 | +[float] |
| 113 | +==== Step 4: (Re-)deploy your Azure Function app |
| 114 | + |
| 115 | +[source,bash] |
| 116 | +---- |
| 117 | +func azure functionapp publish <APP_NAME> |
| 118 | +---- |
| 119 | + |
| 120 | +Now, when you invoke your Azure Functions, you should see your application |
| 121 | +show up as a Service in the APM app in Kibana and see APM transactions for |
| 122 | +function invocations. Tracing data is forwarded to APM server after a period |
| 123 | +of time, so allow a minute or so for data to appear. |
| 124 | + |
| 125 | + |
| 126 | +[float] |
| 127 | +[[azure-functions-limitations]] |
| 128 | +==== Limitations |
| 129 | + |
| 130 | +This instrumentation does not send an APM transaction or error to APM server when |
| 131 | +a handler has an `uncaughtException` or `unhandledRejection`. |
| 132 | +The Azure Functions Node.js reference https://learn.microsoft.com/en-ca/azure/azure-functions/functions-reference-node#use-async-and-await[has a section] with best practices for avoiding these cases. |
| 133 | + |
| 134 | +Azure Functions instrumentation currently does _not_ collect system metrics in |
| 135 | +the background because of a concern with unintentionally increasing Azure |
| 136 | +Functions costs (for Consumption plans). |
| 137 | + |
| 138 | + |
| 139 | +[float] |
| 140 | +[[azure-functions-filter-sensitive-information]] |
| 141 | +==== Filter sensitive information |
| 142 | + |
| 143 | +include::./shared-set-up.asciidoc[tag=filter-sensitive-info] |
| 144 | + |
| 145 | +[float] |
| 146 | +[[azure-functions-compatibility]] |
| 147 | +==== Compatibility |
| 148 | + |
| 149 | +include::./shared-set-up.asciidoc[tag=compatibility-link] |
| 150 | + |
| 151 | +[float] |
| 152 | +[[azure-functions-troubleshooting]] |
| 153 | +==== Troubleshooting |
| 154 | + |
| 155 | +include::./shared-set-up.asciidoc[tag=troubleshooting-link] |
0 commit comments