Skip to content

Commit 5c47b4d

Browse files
authored
Merge pull request #101594 from TimothyMothra/tilee/ai-sdk-connectionstring
Add documentation for Application Insights SDK Connection Strings
2 parents 5ea0e93 + 95f2161 commit 5c47b4d

File tree

3 files changed

+282
-0
lines changed

3 files changed

+282
-0
lines changed
42.8 KB
Loading
Lines changed: 280 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,280 @@
1+
---
2+
title: Connection strings in Azure Application Insights | Microsoft Docs
3+
description: How to use connection strings.
4+
ms.service: azure-monitor
5+
ms.subservice: application-insights
6+
ms.topic: conceptual
7+
author: timothymothra
8+
ms.author: tilee
9+
ms.date: 01/17/2020
10+
11+
ms.reviewer: mbullwin
12+
---
13+
14+
# Connection strings
15+
16+
## Overview
17+
18+
Connection strings provide Application Insight users with a single configuration setting, eliminating the need for multiple proxy settings. Highly useful for intranet web servers, sovereign or hybrid cloud environments looking to send in data to the monitoring service.
19+
20+
The key value pairs provide an easy way for users to define a prefix suffix combination for each Application Insights (AI) service/ product.
21+
22+
> [!IMPORTANT]
23+
> We don't recommend setting both Connection String and Instrumentation key. In the event that a user does set both, whichever was set last will take precedence.
24+
25+
26+
## Scenario overview
27+
28+
Customer scenarios where we visualize this having the most impact:
29+
30+
- Firewall exceptions or proxy redirects
31+
32+
In cases where monitoring for intranet web server is required, our earlier solution asked customers to add individual service endpoints to your configuration. For more information, see [here](https://docs.microsoft.com/azure/azure-monitor/app/troubleshoot-faq#can-i-monitor-an-intranet-web-server).
33+
Connection strings offer a better alternative by reducing this effort to a single setting. A simple prefix, suffix amendment allows automatic population and redirection of all endpoints to the right services.
34+
35+
- Sovereign or Hybrid cloud environments
36+
37+
Users can send data to a defined [Azure Government Region](https://docs.microsoft.com/azure/azure-government/documentation-government-services-monitoringandmanagement#application-insights).
38+
Connection strings allow you to define endpoint settings for your intranet servers or hybrid cloud settings.
39+
40+
## Getting started
41+
42+
### Finding my connection string?
43+
44+
Your connection string is displayed on the Overview blade of your Application Insights resource.
45+
46+
![connection string on overview blade](media/overview-dashboard/overview-connection-string.png)
47+
48+
### Schema
49+
50+
#### Max length
51+
52+
The connection has a maximum supported length of 4096 characters.
53+
54+
#### Key-value pairs
55+
56+
Connection string consists of a list of settings represented as key-value pairs separated by semicolon:
57+
`key1=value1;key2=value2;key3=value3`
58+
59+
#### Syntax
60+
61+
- `InstrumentationKey` (ex: 00000000-0000-0000-0000-000000000000)
62+
The connection string is a **required** field.
63+
- `Authorization` (ex: ikey) (This setting is optional because today we only support ikey authorization.)
64+
- `EndpointSuffix` (ex: applicationinsights.azure.cn)
65+
Setting the endpoint suffix will instruct the SDK which Azure cloud to connect to. The SDK will assemble the rest of the endpoint for individual services.
66+
- Explicit Endpoints.
67+
Any service can be explicitly overridden in the connection string.
68+
- `IngestionEndpoint` (ex: https://dc.applicationinsights.azure.com)
69+
- `LiveEndpoint` (ex: https://live.applicationinsights.azure.com)
70+
- `ProfilerEndpoint` (ex: https://profiler.applicationinsights.azure.com)
71+
- `SnapshotEndpoint` (ex: https://snapshot.applicationinsights.azure.com)
72+
73+
#### Endpoint schema
74+
75+
`<prefix>.<suffix>`
76+
- Prefix: Defines a service.
77+
- Suffix: Defines the common domain name.
78+
79+
##### Valid suffixes
80+
81+
Here's a list of valid suffixes
82+
- applicationinsights.azure.cn
83+
- applicationinsights.us
84+
85+
86+
See also: https://docs.microsoft.com/azure/azure-monitor/app/custom-endpoints#regions-that-require-endpoint-modification
87+
88+
89+
##### Valid prefixes
90+
91+
- [Telemetry Ingestion](./app-insights-overview.md): `dc`
92+
- [Live Metrics](./live-stream.md): `live`
93+
- [Profiler](./profiler-overview.md): `profiler`
94+
- [Snapshot](./snapshot-debugger.md): `snapshot`
95+
96+
97+
98+
## Connection string examples
99+
100+
101+
### Minimal valid connection string
102+
103+
`InstrumentationKey=00000000-0000-0000-0000-000000000000;`
104+
105+
In this example, only the Instrumentation Key has been set.
106+
107+
- Authorization scheme defaults to “ikey”
108+
- Instrumentation Key: 00000000-0000-0000-0000-000000000000
109+
- The regional service URIs are based on the [SDK defaults](https://github.com/microsoft/ApplicationInsights-dotnet/blob/e50d569cebf485e72e98f4a08a0bc0e30cdf42bc/BASE/src/Microsoft.ApplicationInsights/Extensibility/Implementation/Endpoints/Constants.cs#L6) and will connect to the public global Azure:
110+
- Ingestion: https://dc.services.visualstudio.com/
111+
- Live metrics: https://rt.services.visualstudio.com/
112+
- Profiler: https://agent.azureserviceprofiler.net/
113+
- Debugger: https://agent.azureserviceprofiler.net/
114+
115+
116+
117+
### Connection string with endpoint suffix
118+
119+
`InstrumentationKey=00000000-0000-0000-0000-000000000000;EndpointSuffix=ai.contoso.com;`
120+
121+
In this example, this connection string specifies the endpoint suffix and the SDK will construct service endpoints.
122+
123+
- Authorization scheme defaults to “ikey”
124+
- Instrumentation Key: 00000000-0000-0000-0000-000000000000
125+
- The regional service URIs are based on provided endpoint suffix:
126+
- Ingestion: https://dc.ai.contoso.com
127+
- Live metrics: https://live.ai.contoso.com
128+
- Profiler: https://profiler.ai.contoso.com
129+
- Debugger: https://snapshot.ai.contoso.com
130+
131+
132+
133+
### Connection string with explicit endpoint overrides
134+
135+
`InstrumentationKey=00000000-0000-0000-0000-000000000000;IngestionEndpoint=https://custom.com:111/;LiveEndpoint=https://custom.com:222/;ProfilerEndpoint=https://custom.com:333/;SnapshotEndpoint=https://custom.com:444/;`
136+
137+
In this example, this connection string specifies explicit overrides for every service. The SDK will use the exact endpoints provided without modification.
138+
139+
- Authorization scheme defaults to “ikey”
140+
- Instrumentation Key: 00000000-0000-0000-0000-000000000000
141+
- The regional service URIs are based on the explicit override values:
142+
- Ingestion: https://custom.com:111/
143+
- Live metrics: https://custom.com:222/
144+
- Profiler: https://custom.com:333/
145+
- Debugger: https://custom.com:444/
146+
147+
148+
## How to set a connection string
149+
150+
Connection Strings are supported in the following SDK versions:
151+
- .NET and .NET Core v2.12.0
152+
- Java v2.5.1
153+
- Javascript v2.3.0
154+
- NodeJS v1.5.0
155+
- Python v1.0.0
156+
157+
A connection string can be set by either in code, environment variable, or configuration file.
158+
159+
160+
161+
### Environment variable
162+
163+
- Connection String: `APPLICATIONINSIGHTS_CONNECTION_STRING`
164+
165+
### .Net SDK example
166+
167+
TelemetryConfiguration.ConnectionString: https://github.com/microsoft/ApplicationInsights-dotnet/blob/add45ceed35a817dc7202ec07d3df1672d1f610d/BASE/src/Microsoft.ApplicationInsights/Extensibility/TelemetryConfiguration.cs#L271-L274
168+
169+
.Net Explicitly Set:
170+
```csharp
171+
var configuration = new TelemetryConfiguration
172+
{
173+
ConnectionString = "InstrumentationKey=00000000-0000-0000-0000-000000000000;"
174+
};
175+
```
176+
177+
.Net Config File:
178+
179+
```xml
180+
<?xml version="1.0" encoding="utf-8"?>
181+
<ApplicationInsights xmlns="http://schemas.microsoft.com/ApplicationInsights/2013/Settings">
182+
<ConnectionString>InstrumentationKey=00000000-0000-0000-0000-000000000000</ConnectionString>
183+
</ApplicationInsights>
184+
```
185+
186+
187+
NetCore config.json:
188+
189+
```json
190+
{
191+
"ApplicationInsights": {
192+
"ConnectionString" : "InstrumentationKey=00000000-0000-0000-0000-000000000000;"
193+
}
194+
}
195+
```
196+
197+
198+
### Java SDK example
199+
200+
201+
Java Explicitly Set:
202+
```java
203+
TelemetryConfiguration.getActive().setConnectionString("InstrumentationKey=00000000-0000-0000-0000-000000000000");
204+
```
205+
206+
ApplicationInsights.xml
207+
```xml
208+
<?xml version="1.0" encoding="utf-8"?>
209+
<ApplicationInsights xmlns="http://schemas.microsoft.com/ApplicationInsights/2013/Settings">
210+
<ConnectionString>InstrumentationKey=00000000-0000-0000-0000-000000000000;</ConnectionString>
211+
</ApplicationInsights>
212+
```
213+
214+
### Javascript SDK example
215+
216+
Important: Javascript doesn't support the use of Environment Variables.
217+
218+
Using the snippet:
219+
220+
```javascript
221+
<script type="text/javascript">
222+
var sdkInstance="appInsightsSDK";window[sdkInstance]="appInsights";var aiName=window[sdkInstance],aisdk=window[aiName]||function(e){function n(e){t[e]=function(){var n=arguments;t.queue.push(function(){t[e].apply(t,n)})}}var t={config:e};t.initialize=!0;var i=document,a=window;setTimeout(function(){var n=i.createElement("script");n.src=e.url||"https://az416426.vo.msecnd.net/scripts/b/ai.2.min.js",i.getElementsByTagName("script")[0].parentNode.appendChild(n)});try{t.cookie=i.cookie}catch(e){}t.queue=[],t.version=2;for(var r=["Event","PageView","Exception","Trace","DependencyData","Metric","PageViewPerformance"];r.length;)n("track"+r.pop());n("startTrackPage"),n("stopTrackPage");var s="Track"+r[0];if(n("start"+s),n("stop"+s),n("addTelemetryInitializer"),n("setAuthenticatedUserContext"),n("clearAuthenticatedUserContext"),n("flush"),t.SeverityLevel={Verbose:0,Information:1,Warning:2,Error:3,Critical:4},!(!0===e.disableExceptionTracking||e.extensionConfig&&e.extensionConfig.ApplicationInsightsAnalytics&&!0===e.extensionConfig.ApplicationInsightsAnalytics.disableExceptionTracking)){n("_"+(r="onerror"));var o=a[r];a[r]=function(e,n,i,a,s){var c=o&&o(e,n,i,a,s);return!0!==c&&t["_"+r]({message:e,url:n,lineNumber:i,columnNumber:a,error:s}),c},e.autoExceptionInstrumented=!0}return t}(
223+
{
224+
connectionString:"InstrumentationKey=00000000-0000-0000-0000-000000000000;"
225+
}
226+
);window[aiName]=aisdk,aisdk.queue&&0===aisdk.queue.length&&aisdk.trackPageView({});
227+
</script>
228+
```
229+
230+
231+
Manual Setup:
232+
```javascript
233+
import { ApplicationInsights } from '@microsoft/applicationinsights-web'
234+
235+
const appInsights = new ApplicationInsights({ config: {
236+
connectionString: 'InstrumentationKey=00000000-0000-0000-0000-000000000000;'
237+
/* ...Other Configuration Options... */
238+
} });
239+
appInsights.loadAppInsights();
240+
appInsights.trackPageView();
241+
```
242+
243+
### Node SDK example
244+
245+
```javascript
246+
const appInsights = require("applicationinsights");
247+
appInsights.setup("InstrumentationKey=00000000-0000-0000-0000-000000000000;");
248+
appInsights.start();
249+
```
250+
251+
### Python SDK example
252+
253+
We recommend users set the environment variable.
254+
255+
To explicitly set the connection string:
256+
257+
```python
258+
from opencensus.ext.azure.trace_exporter import AzureExporter
259+
from opencensus.trace.samplers import ProbabilitySampler
260+
from opencensus.trace.tracer import Tracer
261+
262+
tracer = Tracer(exporter=AzureExporter(connection_string='InstrumentationKey=00000000-0000-0000-0000-000000000000'), sampler=ProbabilitySampler(1.0))
263+
```
264+
265+
266+
## Next steps
267+
268+
Get started at runtime with:
269+
270+
* [Azure VM and Azure virtual machine scale set IIS-hosted apps](../../azure-monitor/app/azure-vm-vmss-apps.md)
271+
* [IIS server](../../azure-monitor/app/monitor-performance-live-website-now.md)
272+
* [Azure Web Apps](../../azure-monitor/app/azure-web-apps.md)
273+
274+
Get started at development time with:
275+
276+
* [ASP.NET](../../azure-monitor/app/asp-net.md)
277+
* [ASP.NET Core](../../azure-monitor/app/asp-net-core.md)
278+
* [Java](../../azure-monitor/app/java-get-started.md)
279+
* [Node.js](../../azure-monitor/app/nodejs.md)
280+
* [Python (preview)](../../azure-monitor/app/opencensus-python.md)

articles/azure-monitor/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,8 @@
439439
href: app/configuration-with-applicationinsights-config.md
440440
- name: Correlate custom data sources
441441
href: app/custom-data-correlation.md
442+
- name: Connection strings
443+
href: app/sdk-connection-string.md
442444
- name: Special topics
443445
items:
444446
- name: Deep diagnostics for web apps and services

0 commit comments

Comments
 (0)