Skip to content

Commit b3cca90

Browse files
trentmbmorelli25
andauthored
feat: Azure Functions (#3071)
Support for tracing/monitoring Azure Functions. Supported triggers/bindings: HTTP (spec'd), Timer (not spec'd). Spec: elastic/apm#716 Closes: #3015 Co-authored-by: Brandon Morelli <[email protected]>
1 parent d52e180 commit b3cca90

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+3156
-87
lines changed

.eslintrc.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"/examples/esbuild/dist",
1919
"/examples/typescript/dist",
2020
"/examples/nextjs",
21+
"/examples/an-azure-function-app",
2122
"/lib/opentelemetry-bridge/opentelemetry-core-mini",
2223
"/test/babel/out.js",
2324
"/test/lambda/fixtures/esbuild-bundled-handler/hello.js",

CHANGELOG.asciidoc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,25 @@ Notes:
3232
=== Node.js Agent version 3.x
3333
3434
35+
==== Unreleased
36+
37+
[float]
38+
===== Breaking changes
39+
40+
[float]
41+
===== Features
42+
43+
* Support for tracing/monitoring https://learn.microsoft.com/en-us/azure/azure-functions/[Azure Functions].
44+
See the <<azure-functions>> document.
45+
({pull}3071[#3071], https://github.com/elastic/apm/blob/main/specs/agents/tracing-instrumentation-azure-functions.md[spec])
46+
47+
[float]
48+
===== Bug fixes
49+
50+
[float]
51+
===== Chores
52+
53+
3554
[[release-notes-3.41.1]]
3655
==== 3.41.1 2022/12/21
3756

NOTICE.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,3 +424,37 @@ The above copyright notice and this permission notice shall be included in all c
424424
425425
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
426426
```
427+
428+
429+
## require-in-the-middle
430+
431+
- **path:** [lib/ritm.js](lib/ritm.js)
432+
- **author:** Thomas Watson Steen
433+
- **project url:** https://github.com/elastic/require-in-the-middle
434+
- **original file:** https://github.com/elastic/require-in-the-middle/blob/v5.2.0/index.js
435+
- **license:** MIT License (MIT), http://opensource.org/licenses/MIT
436+
437+
```
438+
The MIT License (MIT)
439+
440+
Copyright (c) 2016-2019 Thomas Watson Steen
441+
442+
Permission is hereby granted, free of charge, to any person obtaining a copy
443+
of this software and associated documentation files (the "Software"), to deal
444+
in the Software without restriction, including without limitation the rights
445+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
446+
copies of the Software, and to permit persons to whom the Software is
447+
furnished to do so, subject to the following conditions:
448+
449+
The above copyright notice and this permission notice shall be included in all
450+
copies or substantial portions of the Software.
451+
452+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
453+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
454+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
455+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
456+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
457+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
458+
SOFTWARE.
459+
```
460+

docs/azure-functions.asciidoc

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
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]
104 KB
Loading

docs/set-up.asciidoc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ To get you off the ground, we've prepared guides for setting up the Agent with a
77
// Updates made here will be applied elsewhere as well.
88
// tag::web-frameworks-list[]
99
* <<lambda>>
10+
* <<azure-functions>>
1011
* <<express>>
1112
* <<fastify>>
1213
* <<hapi>>
1314
* <<koa>>
1415
* <<nextjs>>
1516
* <<restify>>
1617
* <<typescript>>
17-
* <<nextjs>>
1818
// end::web-frameworks-list[]
1919

2020
Alternatively, you can <<custom-stack>>.
@@ -30,6 +30,8 @@ Other useful documentation includes:
3030

3131
include::./lambda.asciidoc[]
3232

33+
include::./azure-functions.asciidoc[]
34+
3335
include::./express.asciidoc[]
3436

3537
include::./fastify.asciidoc[]

docs/supported-technologies.asciidoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ These are the frameworks that we officially support:
8888
|=======================================================================
8989
| Framework | Version | Note
9090
| <<lambda,AWS Lambda>> | N/A |
91+
| <<azure-functions,Azure Functions>> | ~4 | See https://learn.microsoft.com/en-ca/azure/azure-functions/set-runtime-version[the guide on Azure Functions runtime versions].
9192
| <<express,Express>> | ^4.0.0 |
9293
| <<fastify,Fastify>> | >=1.0.0 | See also https://www.fastify.io/docs/latest/Reference/LTS/[Fastify's own LTS documentation]
9394
| <<hapi,@hapi/hapi>> | >=17.9.0 <22.0.0 |
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
.vscode/
2+
3+
bin
4+
obj
5+
csx
6+
.vs
7+
edge
8+
Publish
9+
10+
*.user
11+
*.suo
12+
*.cscfg
13+
*.Cache
14+
project.lock.json
15+
16+
/packages
17+
/TestResults
18+
19+
/tools/NuGet.exe
20+
/App_Data
21+
/secrets
22+
/data
23+
.secrets
24+
appsettings.json
25+
26+
node_modules
27+
dist
28+
29+
# Local python packages
30+
.python_packages/
31+
32+
# Python Environments
33+
.env
34+
.venv
35+
env/
36+
venv/
37+
ENV/
38+
env.bak/
39+
venv.bak/
40+
41+
# Byte-compiled / optimized / DLL files
42+
__pycache__/
43+
*.py[cod]
44+
*$py.class
45+
46+
# Azurite artifacts
47+
__blobstorage__
48+
__queuestorage__
49+
__azurite_db*__.json
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock=false
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"bindings": [
3+
{
4+
"authLevel": "Anonymous",
5+
"type": "httpTrigger",
6+
"direction": "in",
7+
"name": "req",
8+
"methods": [
9+
"get",
10+
"post"
11+
]
12+
},
13+
{
14+
"type": "http",
15+
"direction": "out",
16+
"name": "res"
17+
}
18+
]
19+
}

0 commit comments

Comments
 (0)