Skip to content

Commit

Permalink
Add types for "@azure/functions-core" (#1065)
Browse files Browse the repository at this point in the history
  • Loading branch information
ejizba authored Jan 18, 2023
1 parent dd7c195 commit db4425b
Show file tree
Hide file tree
Showing 7 changed files with 542 additions and 11 deletions.
3 changes: 2 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
**/*.js
/out
/Tests
/Tests
/types
16 changes: 8 additions & 8 deletions AutoCollection/AzureFunctionsHook.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import { Disposable, FunctionCallback, PreInvocationContext } from "@azure/functions-core";
import { Context, HttpRequest } from "../Library/Functions";
import Logging = require("../Library/Logging");
import TelemetryClient = require("../Library/TelemetryClient");
import { CorrelationContext, CorrelationContextManager } from "./CorrelationContextManager";


/** Node.js Azure Functions handle incoming HTTP requests before Application Insights SDK is available,
* this code generate incoming request telemetry and generate correlation context to be used
* this code generate incoming request telemetry and generate correlation context to be used
* by outgoing requests and other telemetry, we rely on hooks provided by Azure Functions
*/
export class AzureFunctionsHook {
private _client: TelemetryClient;
private _functionsCoreModule: any;
private _functionsCoreModule: typeof import("@azure/functions-core");
private _autoGenerateIncomingRequests: boolean;
private _preInvocationHook: any;
private _preInvocationHook: Disposable;

constructor(client: TelemetryClient) {
this._client = client;
this._autoGenerateIncomingRequests = false;
try {
this._functionsCoreModule = require('@azure/functions-core');
this._functionsCoreModule = require("@azure/functions-core");
}
catch (error) {
Logging.info("AzureFunctionsHook failed to load, not running in Azure Functions");
Expand All @@ -39,7 +39,7 @@ export class AzureFunctionsHook {

private _addPreInvocationHook() {
if (!this._preInvocationHook) {
this._preInvocationHook = this._functionsCoreModule.registerHook('preInvocation', async (preInvocationContext: any) => {
this._preInvocationHook = this._functionsCoreModule.registerHook("preInvocation", async (preInvocationContext: PreInvocationContext) => {
const originalCallback = preInvocationContext.functionCallback;
preInvocationContext.functionCallback = async (ctx: Context, request: HttpRequest) => {
this._propagateContext(ctx, request, originalCallback);
Expand All @@ -48,7 +48,7 @@ export class AzureFunctionsHook {
}
}

private async _propagateContext(ctx: Context, request: HttpRequest, originalCallback: any) {
private async _propagateContext(ctx: Context, request: HttpRequest, originalCallback: FunctionCallback) {
// Update context to use Azure Functions one
let extractedContext: CorrelationContext = null;
try {
Expand Down Expand Up @@ -96,7 +96,7 @@ export class AzureFunctionsHook {
url: request.url,
time: new Date(startTime),
duration: Date.now() - startTime,
id: extractedContext.operation?.parentId,
id: extractedContext.operation?.parentId
});
this._client.flush();
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/AutoCollection/AzureFunctionsHook.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe("AutoCollection/AzureFunctionsHook", () => {
it("Hook added if running in Azure Functions", () => {
let hook = new AzureFunctionsHook(client);
let testCore = new TestFunctionCore();
hook["_functionsCoreModule"] = testCore;
hook["_functionsCoreModule"] = <any>testCore;
hook["_addPreInvocationHook"]();
assert.ok(testCore.registerCalled);
assert.equal(testCore.hookName, "preInvocation");
Expand Down
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
},
"devDependencies": {
"@types/cls-hooked": "^4.3.3",
"@types/long": "^4.0.2",
"@types/mocha": "^7.0.2",
"@types/node": "^8.0.0",
"@types/sinon": "2.1.2",
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"noImplicitAny": true,
"outDir": "./out",
"typeRoots": [
"./node_modules/@types"
"./node_modules/@types",
"./types"
]
},
"include": [
Expand Down
Loading

0 comments on commit db4425b

Please sign in to comment.