Skip to content

Commit eec9468

Browse files
Merge pull request #275707 from ggailey777/open-telemetry
[Functions][OTel][Build 2024] OpenTelemetry article for Azure Functions (Flex-only)
2 parents 5158dd6 + 4828321 commit eec9468

File tree

4 files changed

+320
-5
lines changed

4 files changed

+320
-5
lines changed

articles/azure-functions/TOC.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@
574574
displayName: upgrade, migrate, language stack
575575
- name: Monitor
576576
items:
577-
- name: Monitor
577+
- name: Monitoring options
578578
href: monitor-functions.md
579579
displayName: monitor metrics
580580
- name: Monitor function executions
@@ -589,6 +589,8 @@
589589
- name: Stream execution logs
590590
href: streaming-logs.md
591591
displayName: monitoring
592+
- name: Work with OpenTelemetry
593+
href: opentelemetry-howto.md
592594
- name: Authenticate
593595
items:
594596
- name: Configure auth providers

articles/azure-functions/functions-host-json.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
---
22
title: host.json reference for Azure Functions 2.x
3-
description: Reference documentation for the Azure Functions host.json file with the v2 runtime.
4-
ms.topic: conceptual
5-
ms.date: 07/10/2023
3+
description: Describes the various available settings for the Azure Functions host that can be set in the host.json file for the Functions v4 runtime.
4+
ms.topic: reference
5+
ms.date: 05/16/2024
66
---
77

88
# host.json reference for Azure Functions 2.x and later
@@ -130,6 +130,7 @@ The following sample *host.json* file for version 2.x+ has all possible options
130130
"lockAcquisitionTimeout": "00:01:00",
131131
"lockAcquisitionPollingInterval": "00:00:03"
132132
},
133+
"telemetryMode": "OpenTelemetry",
133134
"watchDirectories": [ "Shared", "Test" ],
134135
"watchFiles": [ "myFile.txt" ]
135136
}
@@ -345,7 +346,7 @@ Indicates the timeout duration for all function executions. It follows the times
345346

346347
Configuration settings for [Host health monitor](https://github.com/Azure/azure-webjobs-sdk-script/wiki/Host-Health-Monitor).
347348

348-
```
349+
```json
349350
{
350351
"healthMonitor": {
351352
"enabled": true,
@@ -444,6 +445,12 @@ Configuration settings for Singleton lock behavior. For more information, see [G
444445
|lockAcquisitionTimeout|00:01:00|The maximum amount of time the runtime will try to acquire a lock.|
445446
|lockAcquisitionPollingInterval|n/a|The interval between lock acquisition attempts.|
446447

448+
## telemetryMode
449+
450+
_This feature is currently in preview._
451+
452+
Used to enable output of logs and traces in an OpenTelemetry output format to one or more endpoints that support OpenTelemetry. When this setting is set to `OpenTelemetry`, OpenTelemetry output is used. By default without this setting, all logs, traces, and events are sent to Application Insights using the standard outputs. For more information, see [Use OpenTelemetry with Azure Functions](opentelemetry-howto.md).
453+
447454
## version
448455

449456
This value indicates the schema version of host.json. The version string `"version": "2.0"` is required for a function app that targets the v2 runtime, or a later version. There are no host.json schema changes between v2 and v3.
Lines changed: 297 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,297 @@
1+
---
2+
title: Use OpenTelemetry with Azure Functions
3+
description: This article shows you how to enable export of application logs and traces from your function app using OpenTelemetry.
4+
ms.service: azure-functions
5+
ms.topic: how-to
6+
ms.date: 05/16/2024
7+
zone_pivot_groups: programming-languages-set-functions
8+
9+
#CustomerIntent: As a developer, I want to learn how to enable the export of logs and metrics from my function apps by using OpenTelemetry so I can consume and analyze my application telemetry data either in Application Insights or to any OTLP-compliant tools.
10+
---
11+
12+
# Use OpenTelemetry with Azure Functions
13+
14+
[!INCLUDE [functions-opentelemetry-preview-note](../../includes/functions-opentelemetry-preview-note.md)]
15+
16+
This article shows you how to configure your function app to export log and trace data in an OpenTelemetry format. Azure Functions generates telemetry data on your function executions from both the Functions host process and the language-specific worker process in which your function code runs. By default, this telemetry data is sent to Application Insights using the Application Insights SDK. However, you can choose to export this data using OpenTelemetry semantics. While you can still use an OpenTelemetry format to send your data to Application Insights, you can now also export the same data to any other OpenTelemetry-compliant endpoint.
17+
18+
> [!TIP]
19+
> Because this article is targeted at your development language of choice, remember to choose the correct language at the top of the article.
20+
::: zone pivot="programming-language-java"
21+
> Currently, there's no client optimized OpenTelemetry support for Java apps.
22+
:::zone-end
23+
::: zone pivot="programming-language-csharp"
24+
> OpenTelemetry currently isn't supported for C# in-process apps.
25+
:::zone-end
26+
27+
You can obtain these benefits by enabling OpenTelemetry in your function app:
28+
29+
+ Correlation across traces and logs being generated both at the host and in your application code.
30+
+ Consistent, standards-based generation of exportable telemetry data.
31+
+ Integrates with other providers that can consume OpenTeleletry-compliant data.
32+
33+
OpenTelemetry is enabled at the function app level, both in host configuration (`host.json`) and in your code project. Functions also provides a client optimized experience for exporting OpenTelemetry data from your function code that's running in a language-specific worker process.
34+
35+
## 1. Enable OpenTelemetry in the Functions host
36+
37+
When you enable OpenTelemetry output in the function app's host.json file, your host exports OpenTelemetry output regardless of the language stack used by your app.
38+
39+
To enable OpenTelemetry output from the Functions host, update the [host.json file](./functions-host-json.md) in your code project to add a `"telemetryMode": "openTelemetry"` element to the root collection. With OpenTelemetry enabled, your host.json file might look like this:
40+
41+
```json
42+
{
43+
"version": "2.0",
44+
"logging": {
45+
"applicationInsights": {
46+
"samplingSettings": {
47+
"isEnabled": true,
48+
"excludedTypes": "Request"
49+
},
50+
"enableLiveMetricsFilters": true
51+
}
52+
},
53+
"telemetryMode": "openTelemetry"
54+
}
55+
```
56+
57+
## 2. Configure application settings
58+
59+
When OpenTelemetry is enabled in the host.json file, the endpoints to which data is sent is determined based on which OpenTelemetry-supported application settings are available in your app's environment variables.
60+
61+
Create specific application settings in your function app based on the OpenTelemetry output destination. When connection settings are provided for both Application Insights and an OpenTelemetry protocol (OTLP) exporter, OpenTelemetry data is sent to both endpoints.
62+
63+
### [Application Insights](#tab/app-insights)
64+
65+
**`APPLICATIONINSIGHTS_CONNECTION_STRING`**: the connection string for an Application Insights workspace. When this setting exists, OpenTelemetry data is sent to that workspace. This setting is the same one used to connect to Application Insights without OpenTelemetry enabled. If your app doesn't already have this setting, you might need to [Enable Application Insights integration](configure-monitoring.md#enable-application-insights-integration).
66+
67+
### [OTLP Exporter](#tab/otlp-export)
68+
69+
**`OTEL_EXPORTER_OTLP_ENDPOINT`**: an OTLP exporter endpoint URL.
70+
71+
**`OTEL_EXPORTER_OTLP_HEADERS`**: (Optional) list of headers to apply to all outgoing data. This is used by many endpoints to pass an API key.
72+
73+
If your endpoint requires you to set other environment variables, you need to also add them to your application settings. For more information, see the [OTLP Exporter Configuration documentation](https://opentelemetry.io/docs/languages/sdk-configuration/otlp-exporter/).
74+
75+
You should remove the `APPLICATIONINSIGHTS_CONNECTION_STRING` setting, unless you also want OpenTelemetry output from the host sent to Application Insights.
76+
77+
---
78+
79+
## 3. Enable OpenTelemetry in your app
80+
81+
With the Functions host configured to use OpenTelemetry, you should also update your application code to output OpenTelemetry data. Enabling OpenTelemetry in both the host and your application code enables better correlation between traces and logs emitted both by the Functions host process and from your language worker process.
82+
83+
The way that you instrument your application to use OpenTelemetry depends on your target OpenTelemetry endpoint:
84+
::: zone pivot="programming-language-csharp"
85+
1. Run these commands to install the required assemblies in your app:
86+
87+
### [Application Insights](#tab/app-insights)
88+
89+
```cmd
90+
dotnet add package Microsoft.Azure.Functions.Worker.OpenTelemetry --version 1.0.0-preview1
91+
dotnet add package OpenTelemetry.Extensions.Hosting
92+
dotnet add package Azure.Monitor.OpenTelemetry.AspNetCore
93+
```
94+
95+
### [OTLP Exporter](#tab/otlp-export)
96+
97+
```cmd
98+
dotnet add package Microsoft.Azure.Functions.Worker.OpenTelemetry --version 1.0.0-preview1
99+
dotnet add package OpenTelemetry.Extensions.Hosting
100+
dotnet add package OpenTelemetry.Exporter.OpenTelemetryProtocol
101+
```
102+
---
103+
104+
1. In your Program.cs project file, add this `using` statement:
105+
106+
### [Application Insights](#tab/app-insights)
107+
108+
```csharp
109+
using Azure.Monitor.OpenTelemetry.AspNetCore;
110+
```
111+
### [OTLP Exporter](#tab/otlp-export)
112+
113+
```csharp
114+
using OpenTelemetry;
115+
```
116+
---
117+
118+
1. In the `ConfigureServices` delegate, add this service configuration:
119+
120+
### [Application Insights](#tab/app-insights)
121+
122+
```csharp
123+
services.AddOpenTelemetry()
124+
.UseFunctionsWorkerDefaults()
125+
.UseAzureMonitor();
126+
```
127+
### [OTLP Exporter](#tab/otlp-export)
128+
129+
```csharp
130+
services.AddOpenTelemetry()
131+
.UseFunctionsWorkerDefaults()
132+
.UseOtlpExporter();
133+
```
134+
---
135+
136+
To export to both OpenTelemetry endpoints, call both `UseAzureMonitor` and `UseOtlpExporter`.
137+
::: zone-end
138+
::: zone pivot="programming-language-java"
139+
Java worker optimizations aren't yet available for OpenTelemetry, so there's nothing to configure in your Java code.
140+
::: zone-end
141+
::: zone pivot="programming-language-javascript,programming-language-typescript"
142+
1. Install these npm packages in your project:
143+
144+
### [Application Insights](#tab/app-insights)
145+
146+
```bash
147+
npm install @opentelemetry/api
148+
npm install @opentelemetry/auto-instrumentations-node
149+
npm install @azure/monitor-opentelemetry-exporter
150+
```
151+
### [OTLP Exporter](#tab/otlp-export)
152+
153+
```bash
154+
npm install @opentelemetry/api
155+
npm install @opentelemetry/auto-instrumentations-node
156+
npm install @opentelemetry/exporter-logs-otlp-http
157+
```
158+
---
159+
160+
::: zone-end
161+
::: zone pivot="programming-language-javascript"
162+
1. Create a code file in your project, copy and paste the following code in this new file, and save the file as `src/index.js`:
163+
164+
### [Application Insights](#tab/app-insights)
165+
166+
:::code language="javascript" source="~/azure-functions-nodejs-v4/js/src/otelAppInsights.js":::
167+
168+
### [OTLP Exporter](#tab/otlp-export)
169+
170+
:::code language="javascript" source="~/azure-functions-nodejs-v4/js/src/otelOtlp.js":::
171+
172+
---
173+
174+
1. Update the `main` field in your package.json file to include this new `src/index.js` file, which might look like this:
175+
176+
```json
177+
"main": "src/{index.js,functions/*.js}"
178+
```
179+
::: zone-end
180+
::: zone pivot="programming-language-typescript"
181+
1. Create a code file in your project, copy and paste the following code in this new file, and save the file as `src/index.ts`:
182+
183+
### [Application Insights](#tab/app-insights)
184+
185+
:::code language="typescript" source="~/azure-functions-nodejs-v4/ts/src/otelAppInsights.ts":::
186+
187+
### [OTLP Exporter](#tab/otlp-export)
188+
189+
:::code language="typescript" source="~/azure-functions-nodejs-v4/ts/src/otelOtlp.ts":::
190+
191+
---
192+
193+
1. Update the `main` field in your package.json file to include the output of this new `src/index.ts` file, which might look like this:
194+
195+
```json
196+
"main": "dist/src/{index.js,functions/*.js}"
197+
```
198+
::: zone-end
199+
::: zone pivot="programming-language-powershell"
200+
> [!IMPORTANT]
201+
> OpenTelemetry output to Application Insights from the language worker isn't currently supported for PowerShell apps. You might instead want to use an OTLP exporter endpoint. When your host is configured for OpenTelemetry output to Application Insights, the logs generated by the PowerShell worker process are still be forwarded, but distributed tracing isn't supported at this time.
202+
203+
These instructions only apply for an OTLP exporter:
204+
205+
1. Add an application setting named `OTEL_FUNCTIONS_WORKER_ENABLED` with value of `True`.
206+
207+
1. Create an [app-level `Modules` folder](functions-reference-powershell.md#function-app-level-modules-folder) in the root of your app and run the following command:
208+
209+
```powershell
210+
Save-Module -Name AzureFunctions.PowerShell.OpenTelemetry.SDK
211+
```
212+
213+
This installs the required `AzureFunctions.PowerShell.OpenTelemetry.SDK` module directly in your app. You can't use the `requirements.psd1` file to automatically install this dependency because [managed dependencies](functions-reference-powershell.md#dependency-management) isn't currently supported in the [Flex Consumption plan](./flex-consumption-plan.md) preview.
214+
215+
1. Add this code to your profile.ps1 file:
216+
217+
```powershell
218+
Import-Module AzureFunctions.PowerShell.OpenTelemetry.SDK -Force -ErrorAction Stop
219+
Initialize-FunctionsOpenTelemetry
220+
```
221+
222+
::: zone-end
223+
::: zone pivot="programming-language-python"
224+
1. Add this entry in your `requirements.txt` file:
225+
226+
### [Application Insights](#tab/app-insights)
227+
228+
```text
229+
azure.monitor.opentelemetry
230+
```
231+
### [OTLP Exporter](#tab/otlp-export)
232+
233+
```text
234+
opentelemetry-api
235+
opentelemetry-sdk
236+
opentelemetry-exporter-otlp
237+
opentelemetry-instrumentation-logging
238+
```
239+
---
240+
241+
1. Add this code to your `function_app.py` main entry point file:
242+
243+
### [Application Insights](#tab/app-insights)
244+
245+
```python
246+
from azure.monitor.opentelemetry import configure_azure_monitor
247+
configure_azure_monitor()
248+
```
249+
### [OTLP Exporter](#tab/otlp-export)
250+
251+
Traces, metrics, and logs being exported using OpenTelemetry must be configured manually. For more information, see [Instrumentation](https://opentelemetry.io/docs/languages/python/instrumentation/) for Python.
252+
253+
This is a simple implementation for exporting logs:
254+
255+
```python
256+
logging.basicConfig(level=logging.DEBUG)
257+
258+
from opentelemetry import _logs
259+
from opentelemetry.sdk._logs import LoggerProvider, LoggingHandler
260+
from opentelemetry.sdk._logs.export import BatchLogRecordProcessor
261+
from opentelemetry.exporter.otlp.proto.http._log_exporter import OTLPLogExporter
262+
263+
# Initialize logging and an exporter that can send data to an OTLP endpoint by attaching OTLP handler to root logger
264+
# SELECT * FROM Log WHERE instrumentation.provider='opentelemetry'
265+
_logs.set_logger_provider(
266+
LoggerProvider(resource=Resource.create(OTEL_RESOURCE_ATTRIBUTES))
267+
)
268+
logging.getLogger().addHandler(
269+
LoggingHandler(
270+
logger_provider=_logs.get_logger_provider().add_log_record_processor(
271+
BatchLogRecordProcessor(OTLPLogExporter())
272+
)
273+
)
274+
)
275+
276+
```
277+
278+
Review the [OpenTelemetry Logging SDK](https://opentelemetry-python.readthedocs.io/en/stable/sdk/_logs.html) to learn how to use OpenTelemetry components to collect logs.
279+
280+
---
281+
282+
::: zone-end
283+
## Considerations for OpenTelemetry
284+
285+
When you export your data using OpenTelemetry, keep these current considerations in mind.
286+
287+
+ When the host is configured to use OpenTelemetry, only logs and traces are exported. Host metrics aren't currently exported.
288+
289+
+ You can't currently run your app project locally using Core Tools when you have OpenTelemetry enabled in the host. You currently need to deploy your code to Azure to validate your OpenTelemetry-related updates.
290+
291+
+ At this time, only HTTP trigger and Azure SDK-based triggers are supported with OpenTelemetry outputs.
292+
293+
## Related content
294+
295+
[Monitor Azure Functions](monitor-functions.md)
296+
[Flex Consumption plan](./flex-consumption-plan.md)
297+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
author: ggailey777
3+
ms.service: azure-functions
4+
ms.topic: include
5+
ms.date: 05/17/2024
6+
ms.author: glenga
7+
---
8+
> [!IMPORTANT]
9+
> OpenTelemery support for Azure Functions is currently in preview, and your app must be hosted in a [Flex Consumption plan](../articles/azure-functions/flex-consumption-plan.md) to use OpenTelemetry.

0 commit comments

Comments
 (0)