Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add back Azure Functions correlation context hook #1070

Merged
merged 14 commits into from
Feb 7, 2023

Conversation

hectorhdzg
Copy link
Member

No description provided.

AutoCollection/AzureFunctionsHook.ts Outdated Show resolved Hide resolved
AutoCollection/AzureFunctionsHook.ts Outdated Show resolved Hide resolved
AutoCollection/AzureFunctionsHook.ts Outdated Show resolved Hide resolved
AutoCollection/AzureFunctionsHook.ts Show resolved Hide resolved

private _createIncomingRequestTelemetry(ctx: Context, request: HttpRequest, startTime: number, parentId: string) {
let statusCode = 200; //Default
if (ctx.res) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's annoying, but there's actually three ways for users to set the response. Instead of just using ctx.res, here's a method I would use to get the response:

private _getResponse(postInvocationContext: PostInvocationContext, ctx: Context): HttpResponse {
    const httpOutputBinding = ctx.bindingDefinitions.find(b => b.direction === "out" && b.type.toLowerCase() === "http");
    if (httpOutputBinding.name === "$return") {
        return postInvocationContext.result;
    } else if (ctx.bindings[httpOutputBinding.name] !== undefined) {
        return ctx.bindings[httpOutputBinding.name];
    } else {
        return ctx.res;
    }
}

If you're curious, here is sample code for each way you can set the response. (Also documented here to some extent)

Option 1

index.ts:

context.res = {
    // status: 200, /* Defaults to 200 */
    body: 'Hello context.res',
    statusCode: 201,
};

function.json:

{
  "type": "http",
  "direction": "out",
  "name": "canBeAnything" // This is ignored if they use `context.res`
}

Option 2

index.ts:

context.bindings.res123 = {
    // status: 200, /* Defaults to 200 */
    body: 'Hello res123 binding',
    statusCode: 202,
};

function.json:

{
  "type": "http",
  "direction": "out",
  "name": "res123" // must match if they use `context.bindings`
}

Option 3

index.ts:

return {
    // status: 200, /* Defaults to 200 */
    body: 'Hello return',
    statusCode: 203,
};

function.json:

{
  "type": "http",
  "direction": "out",
  "name": "$return"
}

AutoCollection/AzureFunctionsHook.ts Outdated Show resolved Hide resolved
AutoCollection/AzureFunctionsHook.ts Show resolved Hide resolved
Copy link
Member

@ejizba ejizba left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

@hectorhdzg hectorhdzg merged commit d405a4d into develop Feb 7, 2023
@hectorhdzg hectorhdzg deleted the hectorhdzg/azurefnfix branch March 23, 2023 20:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants