Skip to content

Commit cc8b707

Browse files
authored
Set role name lazily for Linux consumption plan (#1537)
1 parent 0f2612c commit cc8b707

File tree

8 files changed

+250
-60
lines changed

8 files changed

+250
-60
lines changed

agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/AiComponentInstaller.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
import com.microsoft.applicationinsights.internal.config.TelemetryModulesXmlElement;
6060
import com.microsoft.applicationinsights.internal.system.SystemInformation;
6161
import com.microsoft.applicationinsights.internal.util.PropertyHelper;
62-
import io.opentelemetry.instrumentation.api.aiconnectionstring.AiConnectionString;
62+
import io.opentelemetry.instrumentation.api.aisdk.AiLazyConfiguration;
6363
import io.opentelemetry.javaagent.spi.ComponentInstaller;
6464
import org.apache.http.HttpHost;
6565
import org.checkerframework.checker.nullness.qual.Nullable;
@@ -158,7 +158,7 @@ private static void start(Instrumentation instrumentation) {
158158

159159
// this is for Azure Function Linux consumption plan support.
160160
if ("java".equals(System.getenv("FUNCTIONS_WORKER_RUNTIME"))) {
161-
AiConnectionString.setAccessor(new ConnectionStringAccessor());
161+
AiLazyConfiguration.setAccessor(new LazyConfigurationAccessor());
162162
}
163163

164164
// this is currently used by Micrometer instrumentation in addition to 2.x SDK

agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/AppIdSupplier.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import com.microsoft.applicationinsights.internal.channel.common.LazyHttpClient;
3131
import com.microsoft.applicationinsights.internal.util.ExceptionStats;
3232
import com.microsoft.applicationinsights.internal.util.ThreadPoolUtils;
33-
import io.opentelemetry.instrumentation.api.aiappid.AiAppId;
33+
import io.opentelemetry.instrumentation.api.aisdk.AiAppId;
3434
import org.apache.http.HttpResponse;
3535
import org.apache.http.client.methods.HttpGet;
3636
import org.apache.http.util.EntityUtils;

agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/ConnectionStringAccessor.java

Lines changed: 0 additions & 54 deletions
This file was deleted.
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* ApplicationInsights-Java
3+
* Copyright (c) Microsoft Corporation
4+
* All rights reserved.
5+
*
6+
* MIT License
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this
8+
* software and associated documentation files (the ""Software""), to deal in the Software
9+
* without restriction, including without limitation the rights to use, copy, modify, merge,
10+
* publish, distribute, sublicense, and/or sell copies of the Software, and to permit
11+
* persons to whom the Software is furnished to do so, subject to the following conditions:
12+
* The above copyright notice and this permission notice shall be included in all copies or
13+
* substantial portions of the Software.
14+
* THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15+
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
16+
* PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
17+
* FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
18+
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19+
* DEALINGS IN THE SOFTWARE.
20+
*/
21+
22+
package com.microsoft.applicationinsights.agent.internal;
23+
24+
import com.google.common.base.Strings;
25+
import com.microsoft.applicationinsights.TelemetryConfiguration;
26+
import com.microsoft.applicationinsights.agent.internal.propagator.DelegatingPropagator;
27+
import com.microsoft.applicationinsights.agent.internal.sampling.DelegatingSampler;
28+
import io.opentelemetry.instrumentation.api.aisdk.AiLazyConfiguration;
29+
import org.slf4j.Logger;
30+
import org.slf4j.LoggerFactory;
31+
32+
public class LazyConfigurationAccessor implements AiLazyConfiguration.Accessor {
33+
34+
private static final Logger logger = LoggerFactory.getLogger(LazyConfigurationAccessor.class);
35+
36+
@Override
37+
public void lazyLoad() {
38+
lazySetEnvVars();
39+
}
40+
41+
private void lazySetEnvVars() {
42+
String instrumentationKey = TelemetryConfiguration.getActive().getInstrumentationKey();
43+
String roleName = TelemetryConfiguration.getActive().getRoleName();
44+
if (instrumentationKey != null && !instrumentationKey.isEmpty() && roleName != null && !roleName.isEmpty()) {
45+
return;
46+
}
47+
48+
boolean lazySetOptIn = Boolean.parseBoolean(System.getProperty("LazySetOptIn"));
49+
String enableAgent = System.getenv("APPLICATIONINSIGHTS_ENABLE_AGENT");
50+
logger.debug("lazySetOptIn: {}", lazySetOptIn);
51+
logger.debug("APPLICATIONINSIGHTS_ENABLE_AGENT: {}", enableAgent);
52+
if (!shouldSetConnectionString(lazySetOptIn, enableAgent)) {
53+
return;
54+
}
55+
56+
setConnectionString(System.getenv("APPLICATIONINSIGHTS_CONNECTION_STRING"), System.getenv("APPINSIGHTS_INSTRUMENTATIONKEY"));
57+
setWebsiteSiteName(System.getenv("WEBSITE_SITE_NAME"));
58+
}
59+
60+
static void setConnectionString(String connectionString, String instrumentationKey) {
61+
if (connectionString != null && !connectionString.isEmpty()) {
62+
setValue(connectionString);
63+
} else {
64+
// if the instrumentation key is neither null nor empty , we will create a default
65+
// connection string based on the instrumentation key.
66+
// this is to support Azure Functions that were created prior to the introduction of
67+
// connection strings
68+
if (instrumentationKey != null && !instrumentationKey.isEmpty()) {
69+
setValue("InstrumentationKey=" + instrumentationKey);
70+
}
71+
}
72+
}
73+
74+
private static void setValue(String value) {
75+
if (!Strings.isNullOrEmpty(value)) {
76+
TelemetryConfiguration.getActive().setConnectionString(value);
77+
// now that we know the user has opted in to tracing, we need to init the propagator and sampler
78+
DelegatingPropagator.getInstance().setUpStandardDelegate();
79+
// TODO handle APPLICATIONINSIGHTS_SAMPLING_PERCENTAGE
80+
DelegatingSampler.getInstance().setAlwaysOnDelegate();
81+
logger.info("Set connection string {} lazily for the Azure Function Consumption Plan.", value);
82+
}
83+
}
84+
85+
static void setWebsiteSiteName(String websiteSiteName) {
86+
if (websiteSiteName != null && !websiteSiteName.isEmpty()) {
87+
TelemetryConfiguration.getActive().setRoleName(websiteSiteName);
88+
logger.info("Set WEBSITE_SITE_NAME: {} lazily for the Azure Function Consumption Plan.", websiteSiteName);
89+
}
90+
}
91+
92+
static boolean shouldSetConnectionString(boolean lazySetOptIn, String enableAgent) {
93+
if (lazySetOptIn) {
94+
// when LazySetOptIn is on, enable agent if APPLICATIONINSIGHTS_ENABLE_AGENT is null or true
95+
if (enableAgent == null || Boolean.parseBoolean(enableAgent)) {
96+
return true;
97+
}
98+
} else {
99+
// when LazySetOptIn is off, enable agent if APPLICATIONINSIGHTS_ENABLE_AGENT is true
100+
if (Boolean.parseBoolean(enableAgent)) {
101+
return true;
102+
}
103+
}
104+
return false;
105+
}
106+
}

agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/propagator/AiLegacyPropagator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
import io.opentelemetry.context.propagation.TextMapGetter;
3838
import io.opentelemetry.context.propagation.TextMapPropagator;
3939
import io.opentelemetry.context.propagation.TextMapSetter;
40-
import io.opentelemetry.instrumentation.api.aiappid.AiAppId;
40+
import io.opentelemetry.instrumentation.api.aisdk.AiAppId;
4141
import org.slf4j.Logger;
4242
import org.slf4j.LoggerFactory;
4343

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/*
2+
* ApplicationInsights-Java
3+
* Copyright (c) Microsoft Corporation
4+
* All rights reserved.
5+
*
6+
* MIT License
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this
8+
* software and associated documentation files (the ""Software""), to deal in the Software
9+
* without restriction, including without limitation the rights to use, copy, modify, merge,
10+
* publish, distribute, sublicense, and/or sell copies of the Software, and to permit
11+
* persons to whom the Software is furnished to do so, subject to the following conditions:
12+
* The above copyright notice and this permission notice shall be included in all copies or
13+
* substantial portions of the Software.
14+
* THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15+
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
16+
* PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
17+
* FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
18+
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19+
* DEALINGS IN THE SOFTWARE.
20+
*/
21+
22+
package com.microsoft.applicationinsights.agent.internal;
23+
24+
import com.microsoft.applicationinsights.TelemetryConfiguration;
25+
import org.junit.*;
26+
27+
import static org.junit.Assert.*;
28+
29+
public class LazyConfigurationAccessorTest {
30+
31+
/*
32+
* Lazily Set Connection String For Linux Consumption Plan:
33+
*
34+
* Term LazySetOptIn ConnectionString EnableAgent LazySet
35+
* Preview FALSE VALID TRUE Enabled
36+
* VALID FALSE Disabled
37+
* VALID NULL Disabled
38+
* NULL TRUE/FALSE/NULL Disabled
39+
* GA TRUE VALID TRUE Enabled
40+
* VALID FALSE Disabled
41+
* VALID NULL Enabled
42+
* NULL TRUE/FALSE/NULL Disabled
43+
*/
44+
private static final String CONNECTION_STRING =
45+
"InstrumentationKey=00000000-0000-0000-0000-0FEEDDADBEEF;IngestionEndpoint=http://fakeingestion:60606/";
46+
47+
private static final String INSTRUMENTATION_KEY = "00000000-0000-0000-0000-0FEEDDADBEEF";
48+
private static final String WEBSITE_SITE_NAME = "fake_site_name";
49+
50+
@Test
51+
//"LazySetOptIn is FALSE, ConnectionString is valid and EnableAgent is TRUE"
52+
public void enableLazySetWithLazySetOptInOffEnableAgentOn() {
53+
assertTrue(LazyConfigurationAccessor.shouldSetConnectionString(false, "true"));
54+
}
55+
56+
@Test
57+
//"LazySetOptIn is FALSE, ConnectionString is valid and EnableAgent is FALSE"
58+
public void disableLazySetWithLazySetOptInOffEnableAgentOff() {
59+
assertFalse(LazyConfigurationAccessor.shouldSetConnectionString(false, "false"));
60+
}
61+
62+
@Test
63+
//"LazySetOptIn is FALSE, ConnectionString is valid and EnableAgent is NULL"
64+
public void disableLazySetWithLazySetOptInOffEnableAgentNull() {
65+
assertFalse(LazyConfigurationAccessor.shouldSetConnectionString(false, null));
66+
}
67+
68+
@Test
69+
//"LazySetOptIn is FALSE, ConnectionString is NULL, InstrumentationKey is NULL, and EnableAgent is TRUE"
70+
public void disableLazySetWithLazySetOptInOffConnectionStringNullInstrumentationKeyNull() {
71+
String oldConnectionString = TelemetryConfiguration.getActive().getConnectionString();
72+
assertTrue(LazyConfigurationAccessor.shouldSetConnectionString(false, "true"));
73+
LazyConfigurationAccessor.setConnectionString(null, null);
74+
assertEquals(TelemetryConfiguration.getActive().getConnectionString(), oldConnectionString);
75+
}
76+
77+
@Test
78+
//"LazySetOptIn is FALSE, ConnectionString is valid, InstrumentationKey is NULL, and EnableAgent is TRUE"
79+
public void disableLazySetWithLazySetOptInOffConnectionStringNotNullInstrumentationKeyNull() {
80+
assertTrue(LazyConfigurationAccessor.shouldSetConnectionString(false, "true"));
81+
LazyConfigurationAccessor.setConnectionString(CONNECTION_STRING, null);
82+
assertEquals(TelemetryConfiguration.getActive().getConnectionString(), CONNECTION_STRING);
83+
84+
LazyConfigurationAccessor.setWebsiteSiteName(WEBSITE_SITE_NAME);
85+
assertEquals(TelemetryConfiguration.getActive().getRoleName(), WEBSITE_SITE_NAME);
86+
}
87+
88+
@Test
89+
//"LazySetOptIn is FALSE, ConnectionString is NULL, InstrumentationKey is valid, and EnableAgent is TRUE")
90+
public void enableLazySetWithLazySetOptInOffConnectionStringNullInstrumentationKeyNotNull() {
91+
assertTrue(LazyConfigurationAccessor.shouldSetConnectionString(false, "true"));
92+
LazyConfigurationAccessor.setConnectionString(null, INSTRUMENTATION_KEY);
93+
assertEquals(TelemetryConfiguration.getActive().getConnectionString(), "InstrumentationKey=" + INSTRUMENTATION_KEY);
94+
}
95+
96+
@Test
97+
//"LazySetOptIn is TRUE, ConnectionString is valid and EnableAgent is TRUE"
98+
public void enableLazySetWithLazySetOptInOnEnableAgentOn() {
99+
assertTrue(LazyConfigurationAccessor.shouldSetConnectionString(true, "true"));
100+
}
101+
102+
@Test
103+
//"LazySetOptIn is TRUE, ConnectionString is valid and EnableAgent is FALSE"
104+
public void disableLazySetWithLazySetOptInOnEnableAgentOff() {
105+
assertFalse(LazyConfigurationAccessor.shouldSetConnectionString(true, "false"));
106+
}
107+
108+
@Test
109+
//"LazySetOptIn is TRUE, ConnectionString is valid and EnableAgent is NULL"
110+
public void enableLazySetWithLazySetOptInOnEnableAgentNull() {
111+
assertTrue(LazyConfigurationAccessor.shouldSetConnectionString(true, null));
112+
}
113+
114+
@Test
115+
//"LazySetOptIn is TRUE, ConnectionString is NULL, InstrumentationKey is NULL, and EnableAgent is TRUE"
116+
public void disableLazySetWithLazySetOptInOnConnectionStringNullAndInstrumentationKeyNull() {
117+
String oldConnectionString = TelemetryConfiguration.getActive().getConnectionString();
118+
assertTrue(LazyConfigurationAccessor.shouldSetConnectionString(true, "true"));
119+
LazyConfigurationAccessor.setConnectionString(null, null);
120+
assertEquals(TelemetryConfiguration.getActive().getConnectionString(), oldConnectionString);
121+
}
122+
123+
@Test
124+
//"LazySetOptIn is TRUE, ConnectionString is valid, InstrumentationKey is NULL, and EnableAgent is TRUE"
125+
public void enableLazySetWithLazySetOptInOnConnectionStringNotNullInstrumentationKeyNull() {
126+
assertTrue(LazyConfigurationAccessor.shouldSetConnectionString(false, "true"));
127+
LazyConfigurationAccessor.setConnectionString(CONNECTION_STRING, null);
128+
assertEquals(TelemetryConfiguration.getActive().getConnectionString(), CONNECTION_STRING);
129+
}
130+
131+
@Test
132+
//"LazySetOptIn is TRUE, ConnectionString is NULL, InstrumentationKey is valid, and EnableAgent is TRUE"
133+
public void enableLazySetWithLazySetOptInOnConnectionStringNullInstrumentationKeyNotNull() {
134+
assertTrue(LazyConfigurationAccessor.shouldSetConnectionString(false, "true"));
135+
LazyConfigurationAccessor.setConnectionString(null, INSTRUMENTATION_KEY);
136+
assertEquals(TelemetryConfiguration.getActive().getConnectionString(), "InstrumentationKey=" + INSTRUMENTATION_KEY);
137+
}
138+
}

agent/exporter/src/main/java/com/microsoft/applicationinsights/agent/Exporter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
import io.opentelemetry.api.trace.SpanKind;
5252
import io.opentelemetry.api.trace.SpanId;
5353
import io.opentelemetry.api.trace.StatusCode;
54-
import io.opentelemetry.instrumentation.api.aiappid.AiAppId;
54+
import io.opentelemetry.instrumentation.api.aisdk.AiAppId;
5555
import io.opentelemetry.sdk.common.CompletableResultCode;
5656
import io.opentelemetry.sdk.trace.data.EventData;
5757
import io.opentelemetry.sdk.trace.data.LinkData;

otel

Submodule otel updated 12 files

0 commit comments

Comments
 (0)