-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix Azure Functions instrumentation (#2684)
The fix was just un-inlining `generateCustomDimensions()`. But this pointed out our lack of basic test for this instrumentation, so added some stub classes to test the instrumentation (since azure functions worker classes are not on maven central). And using these stub classes also allows us to remove the reflection.
- Loading branch information
Showing
12 changed files
with
219 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
agent/instrumentation/azure-functions-worker-stub/build.gradle.kts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
plugins { | ||
id("ai.java-conventions") | ||
} | ||
|
||
// this module is needed since the azure functions worker artifact is not available in maven central |
15 changes: 15 additions & 0 deletions
15
...rker-stub/src/main/java/com/microsoft/azure/functions/rpc/messages/InvocationRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package com.microsoft.azure.functions.rpc.messages; | ||
|
||
public class InvocationRequest { | ||
|
||
public RpcTraceContext getTraceContext() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
public String getInvocationId() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...worker-stub/src/main/java/com/microsoft/azure/functions/rpc/messages/RpcTraceContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package com.microsoft.azure.functions.rpc.messages; | ||
|
||
import java.util.Map; | ||
|
||
public class RpcTraceContext { | ||
|
||
public Map<String, String> getAttributesMap() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
public String getTraceParent() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
public String getTraceState() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
.../src/main/java/com/microsoft/azure/functions/worker/handler/InvocationRequestHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package com.microsoft.azure.functions.worker.handler; | ||
|
||
import com.microsoft.azure.functions.rpc.messages.InvocationRequest; | ||
|
||
public class InvocationRequestHandler { | ||
|
||
public void execute(InvocationRequest request) { | ||
verifyCurrentContext(); | ||
} | ||
|
||
// this doesn't exist in the real worker artifact | ||
// it only exists for testing the instrumentation | ||
protected void verifyCurrentContext() {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
agent/instrumentation/azure-functions/src/test/java/AzureFunctionsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
import static java.util.Collections.emptyMap; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.ArgumentMatchers.anyString; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
import com.microsoft.applicationinsights.agent.bootstrap.AzureFunctions; | ||
import com.microsoft.applicationinsights.agent.bootstrap.BytecodeUtil; | ||
import com.microsoft.applicationinsights.agent.bootstrap.BytecodeUtil.BytecodeUtilDelegate; | ||
import com.microsoft.azure.functions.rpc.messages.InvocationRequest; | ||
import com.microsoft.azure.functions.rpc.messages.RpcTraceContext; | ||
import com.microsoft.azure.functions.worker.handler.InvocationRequestHandler; | ||
import io.opentelemetry.api.trace.Span; | ||
import io.opentelemetry.api.trace.SpanContext; | ||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension; | ||
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; | ||
import java.util.Map; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
class AzureFunctionsTest { | ||
|
||
@RegisterExtension | ||
static final InstrumentationExtension testing = AgentInstrumentationExtension.create(); | ||
|
||
static { | ||
// this is needed since currently tests are run against otel javaagent, and not ai javaagent | ||
AzureFunctions.setup(() -> true, () -> {}); | ||
BytecodeUtilDelegate delegate = mock(BytecodeUtilDelegate.class); | ||
when(delegate.shouldSample(anyString())).thenReturn(true); | ||
BytecodeUtil.setDelegate(delegate); | ||
} | ||
|
||
@Test | ||
void setRequestProperty() { | ||
// given | ||
String traceParent = "00-11111111111111111111111111111111-1111111111111111-00"; | ||
String traceState = null; | ||
Map<String, String> attributesMap = emptyMap(); | ||
RpcTraceContext traceContext = new MockRpcTraceContext(traceParent, traceState, attributesMap); | ||
|
||
String invocationId = null; | ||
InvocationRequest request = new MockInvocationRequest(traceContext, invocationId); | ||
|
||
AtomicReference<Context> contextRef = new AtomicReference<>(); | ||
InvocationRequestHandler handler = | ||
new InvocationRequestHandler() { | ||
@Override | ||
protected void verifyCurrentContext() { | ||
contextRef.set(Context.current()); | ||
} | ||
}; | ||
|
||
// when | ||
handler.execute(request); | ||
|
||
// then | ||
Context context = contextRef.get(); | ||
SpanContext spanContext = Span.fromContext(context).getSpanContext(); | ||
assertThat(spanContext.getTraceId()).isEqualTo("11111111111111111111111111111111"); | ||
assertThat(spanContext.getSpanId()).isEqualTo("1111111111111111"); | ||
assertThat(spanContext.getTraceFlags().isSampled()).isTrue(); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
agent/instrumentation/azure-functions/src/test/java/MockInvocationRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
import com.microsoft.azure.functions.rpc.messages.InvocationRequest; | ||
import com.microsoft.azure.functions.rpc.messages.RpcTraceContext; | ||
|
||
class MockInvocationRequest extends InvocationRequest { | ||
|
||
private final RpcTraceContext traceContext; | ||
private final String invocationId; | ||
|
||
MockInvocationRequest(RpcTraceContext traceContext, String invocationId) { | ||
this.traceContext = traceContext; | ||
this.invocationId = invocationId; | ||
} | ||
|
||
@Override | ||
public RpcTraceContext getTraceContext() { | ||
return traceContext; | ||
} | ||
|
||
@Override | ||
public String getInvocationId() { | ||
return invocationId; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
agent/instrumentation/azure-functions/src/test/java/MockRpcTraceContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
import com.microsoft.azure.functions.rpc.messages.RpcTraceContext; | ||
import java.util.Map; | ||
|
||
class MockRpcTraceContext extends RpcTraceContext { | ||
|
||
private final String traceParent; | ||
private final String traceState; | ||
private final Map<String, String> attributesMap; | ||
|
||
MockRpcTraceContext(String traceParent, String traceState, Map<String, String> attributesMap) { | ||
this.traceParent = traceParent; | ||
this.traceState = traceState; | ||
this.attributesMap = attributesMap; | ||
} | ||
|
||
@Override | ||
public String getTraceParent() { | ||
return traceParent; | ||
} | ||
|
||
@Override | ||
public String getTraceState() { | ||
return traceState; | ||
} | ||
|
||
@Override | ||
public Map<String, String> getAttributesMap() { | ||
return attributesMap; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters