Skip to content

Commit

Permalink
[Azure Monitor OpenTelemetry] Add support for Azure Functions program…
Browse files Browse the repository at this point in the history
…ming model v4 (#27161)

### Packages impacted by this PR
@azure/monitor-opentelemetry

Related to
microsoft/ApplicationInsights-node.js#1181
  • Loading branch information
hectorhdzg authored Sep 19, 2023
1 parent 5a617a5 commit 245548f
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 36 deletions.
11 changes: 1 addition & 10 deletions sdk/monitor/monitor-opentelemetry/src/traces/azureFnHook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,7 @@ export class AzureFunctionsHook {
try {
// TODO: Add types files when publicly available
this._functionsCoreModule = require("@azure/functions-core");
// Only v3 of Azure Functions library is supported right now. See matrix of versions here:
// https://github.com/Azure/azure-functions-nodejs-library
const funcProgModel = this._functionsCoreModule.getProgrammingModel();
if (funcProgModel.name === "@azure/functions" && funcProgModel.version.startsWith("3.")) {
this._addPreInvocationHook();
} else {
Logger.getInstance().debug(
`AzureFunctionsHook does not support model "${funcProgModel.name}" version "${funcProgModel.version}"`
);
}
this._addPreInvocationHook();
} catch (error) {
Logger.getInstance().debug(
"@azure/functions-core failed to load, not running in Azure Functions"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,32 +55,6 @@ describe("Library/AzureFunctionsHook", () => {
Module.prototype.require = originalRequire;
});

it("Hook not added if using not supported programming model", () => {
var Module = require("module");
var preInvocationCalled = false;
Module.prototype.require = function () {
if (arguments[0] === "@azure/functions-core") {
return {
registerHook(name: string) {
if (name === "preInvocation") {
preInvocationCalled = true;
}
},
getProgrammingModel() {
return {
name: "@azure/functions",
version: "2.x",
};
},
};
}
return originalRequire.apply(this, arguments);
};
let azureFnHook = new AzureFunctionsHook();
assert.ok(azureFnHook, "azureFnHook");
assert.ok(!preInvocationCalled, "preInvocationCalled");
});

it("Pre Invokation Hook added if running in Azure Functions and context is propagated", () => {
let Module = require("module");
let preInvocationCalled = false;
Expand Down

0 comments on commit 245548f

Please sign in to comment.