-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixing issue with status in auto generated request in Azure Fn (#1046)
- Loading branch information
1 parent
9909cbd
commit a4b6f71
Showing
5 changed files
with
315 additions
and
188 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,98 @@ | ||
import assert = require("assert"); | ||
import sinon = require("sinon"); | ||
import { TelemetryClient } from "../../applicationinsights"; | ||
import { AzureFunctionsHook } from "../../AutoCollection/AzureFunctionsHook"; | ||
import { CorrelationContextManager } from "../../AutoCollection/CorrelationContextManager"; | ||
import { HttpRequest } from "../../Library/Functions"; | ||
import Logging = require("../../Library/Logging"); | ||
|
||
import { AutoCollectAzureFunctions } from "../../AutoCollection/AzureFunctionsHook"; | ||
|
||
const testModule = { | ||
registerHook(type: string, hook: any) { | ||
class TestFunctionCore { | ||
public registerCalled: boolean = false; | ||
public hookName: string; | ||
|
||
registerHook(name: string, func: any) { | ||
this.registerCalled = true; | ||
this.hookName = name; | ||
} | ||
}; | ||
} | ||
|
||
describe("AutoCollection/AutoCollectAzureFunctions", () => { | ||
describe("AutoCollection/AzureFunctionsHook", () => { | ||
let sandbox: sinon.SinonSandbox; | ||
let client: TelemetryClient; | ||
|
||
it("constructor", () => { | ||
let client = new TelemetryClient("1aa11111-bbbb-1ccc-8ddd-eeeeffff3333"); | ||
let auto = new AutoCollectAzureFunctions(client); | ||
assert.equal(auto["_functionsCoreModule"], undefined, "Module is not available so it should be undefined unless running in AzFn env"); | ||
before(() => { | ||
client = new TelemetryClient("1aa11111-bbbb-1ccc-8ddd-eeeeffff3333"); | ||
sandbox = sinon.sandbox.create(); | ||
}); | ||
|
||
it("enable", () => { | ||
let client = new TelemetryClient("1aa11111-bbbb-1ccc-8ddd-eeeeffff3333"); | ||
let auto = new AutoCollectAzureFunctions(client); | ||
auto["_functionsCoreModule"] = testModule; | ||
const addStub = sinon.stub(auto, "_addPreInvocationHook"); | ||
const removeStub = sinon.stub(auto, "_removePreInvocationHook"); | ||
auto.enable(true); | ||
assert.ok(removeStub.notCalled); | ||
assert.ok(addStub.calledOnce); | ||
afterEach(() => { | ||
sandbox.restore(); | ||
CorrelationContextManager.enable(false); | ||
}); | ||
|
||
it("disable", () => { | ||
let client = new TelemetryClient("1aa11111-bbbb-1ccc-8ddd-eeeeffff3333"); | ||
let auto = new AutoCollectAzureFunctions(client); | ||
auto["_functionsCoreModule"] = testModule; | ||
const addStub = sinon.stub(auto, "_addPreInvocationHook"); | ||
const removeStub = sinon.stub(auto, "_removePreInvocationHook"); | ||
auto.enable(false); | ||
assert.ok(removeStub.calledOnce); | ||
assert.ok(addStub.notCalled); | ||
|
||
it("Hook not added if not running in Azure Functions", () => { | ||
const spy = sandbox.spy(Logging, "info"); | ||
let hook = new AzureFunctionsHook(client); | ||
assert.equal(hook["_functionsCoreModule"], undefined); | ||
assert.ok(spy.called); | ||
assert.equal(spy.args[0][0], "AzureFunctionsHook failed to load, not running in Azure Functions"); | ||
}); | ||
|
||
it("Hook added if running in Azure Functions", () => { | ||
let hook = new AzureFunctionsHook(client); | ||
let testCore = new TestFunctionCore(); | ||
hook["_functionsCoreModule"] = testCore; | ||
hook["_addPreInvocationHook"](); | ||
assert.ok(testCore.registerCalled); | ||
assert.equal(testCore.hookName, "preInvocation"); | ||
}); | ||
|
||
it("enable/disable", () => { | ||
let hook = new AzureFunctionsHook(client); | ||
hook.enable(true); | ||
assert.equal(hook["_autoGenerateIncomingRequests"], true); | ||
hook.enable(false); | ||
assert.equal(hook["_autoGenerateIncomingRequests"], false); | ||
}); | ||
|
||
it("_addPreInvocationHook", () => { | ||
let client = new TelemetryClient("1aa11111-bbbb-1ccc-8ddd-eeeeffff3333"); | ||
let auto = new AutoCollectAzureFunctions(client); | ||
const registerHook = sinon.stub(testModule, "registerHook"); | ||
auto["_functionsCoreModule"] = testModule; | ||
auto["_addPreInvocationHook"](); | ||
assert.ok(registerHook.calledOnce); | ||
it("Context propagation", () => { | ||
CorrelationContextManager.enable(true); | ||
let hook = new AzureFunctionsHook(client); | ||
hook.enable(true); | ||
let flushStub = sandbox.stub(hook["_client"], "flush"); | ||
let trackRequestSpy = sandbox.stub(hook["_client"], "trackRequest"); | ||
let contextSpy = sandbox.spy(CorrelationContextManager, "wrapCallback"); | ||
let ctx = { | ||
res: { "status": 400 }, | ||
traceContext: { | ||
traceparent: "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01", | ||
tracestate: "", | ||
attributes: {} | ||
} | ||
}; | ||
let request: HttpRequest = { | ||
method: "HEAD", | ||
url: "test.com", | ||
headers: { "": "" } | ||
}; | ||
let originalCallbackCalled = false; | ||
let originalCallback = () => { originalCallbackCalled = true }; | ||
hook["_propagateContext"](ctx as any, request, originalCallback); | ||
assert.ok(contextSpy.called); | ||
assert.ok(originalCallbackCalled); | ||
assert.ok(flushStub.called); | ||
assert.ok(trackRequestSpy.called); | ||
let propagatedContext = contextSpy.args[0][1]; | ||
assert.equal(propagatedContext.operation.id, "0af7651916cd43dd8448eb211c80319c"); | ||
assert.equal(propagatedContext.operation.name, "HEAD /"); | ||
assert.equal(propagatedContext.operation.parentId, "|0af7651916cd43dd8448eb211c80319c.b7ad6b7169203331."); | ||
let incomingRequest = trackRequestSpy.args[0][0]; | ||
assert.equal(incomingRequest.id, "|0af7651916cd43dd8448eb211c80319c.b7ad6b7169203331."); | ||
assert.equal(incomingRequest.name, "HEAD test.com"); | ||
assert.equal(incomingRequest.resultCode, 400); | ||
assert.equal(incomingRequest.success, false); | ||
assert.equal(incomingRequest.url, "test.com"); | ||
}); | ||
}); |
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
Oops, something went wrong.