microsoft / ApplicationInsights-node.js

Microsoft Application Insights SDK for Node.js
MIT License
321 stars 138 forks source link

3.0.0-beta.11 - has no exported member 'Context' #1299

Open jerryjren opened 3 months ago

jerryjren commented 3 months ago

Hi there,

I have added application insights library to my Azure function app in order to send custom events. https://learn.microsoft.com/en-us/azure/azure-monitor/app/opentelemetry-add-modify?tabs=nodejs#send-custom-telemetry-using-the-application-insights-classic-api

After npm start, I'm getting the below error. Any suggestion? image

Another question, my function has associated application insights. Do I still need to use this library? The reason to use this library was I want my logs to be in OpenTelemetry format, hence followed the tutorial below. https://learn.microsoft.com/en-us/azure/azure-monitor/app/opentelemetry-enable?tabs=nodejs

Func version: 4.0.5455

JacksonWeber commented 3 months ago

Hi @jerryjren, if you've already auto-instrumented your application when you created the Azure Function, you shouldn't need to do any manual instrumentation with this SDK.

If your main goal is to be using OpenTelemetry concepts and instrumentations, I'd suggest using the Azure Monitor OpenTelemetry Distro for Node.js https://www.npmjs.com/package/@azure/monitor-opentelemetry. The intention of the shim is primarily to provide customers already heavily manually instrumented with ApplicationInsights SDK 2.x a simple path forward.

jerryjren commented 3 months ago

Hi @jerryjren, if you've already auto-instrumented your application when you created the Azure Function, you shouldn't need to do any manual instrumentation with this SDK.

If your main goal is to be using OpenTelemetry concepts and instrumentations, I'd suggest using the Azure Monitor OpenTelemetry Distro for Node.js https://www.npmjs.com/package/@azure/monitor-opentelemetry. The intention of the shim is primarily to provide customers already heavily manually instrumented with ApplicationInsights SDK 2.x a simple path forward.

Hi,

According to this tutorial that opentelemetry nodejs api currently does not support custom events.

It suggested to use application insights class api instead. See below. https://learn.microsoft.com/en-us/azure/azure-monitor/app/opentelemetry-add-modify?tabs=nodejs#send-custom-telemetry-using-the-application-insights-classic-api

I might be reading it wrong, any suggestions are welcome.

thank you

JacksonWeber commented 3 months ago

@jerryjren Ah, I see. Yes, if you're looking for custom events, you're on the right track.

Based on the error it looks like there's just a type Context that's expected out of your @azure/functions package. Could you let me know what version of @azure/functions you have?

jerryjren commented 3 months ago

@jerryjren Ah, I see. Yes, if you're looking for custom events, you're on the right track.

Based on the error it looks like there's just a type Context that's expected out of your @azure/functions package. Could you let me know what version of @azure/functions you have?

    "@azure/functions": "^4.3.0",
    "@azure/monitor-opentelemetry": "^1.2.0",
    "@opentelemetry/api-logs": "^0.49.1",
    "@opentelemetry/resources": "^1.20.0",
    "@opentelemetry/sdk-logs": "^0.49.1",
    "@opentelemetry/semantic-conventions": "^1.22.0",
    "applicationinsights": "^3.0.0-beta.11",
JacksonWeber commented 3 months ago

@jerryjren It looks like the SDK is not compatible with @azure/functions 4.3.0's types. Could you try setting your @azure/functions package version to 3.2.0?

jerryjren commented 2 months ago

@jerryjren It looks like the SDK is not compatible with @azure/functions 4.3.0's types. Could you try setting your @azure/functions package version to 3.2.0?

hey, sorry about late reply.

I tried with @azure/functions package version to 3.2.0, and it works. Thanks for that.

Although I'm encounter another issue saying invalid metric name, even though I'm trying to track event.

Here is the statcktrace


[2024-04-18T06:42:47.497Z] Executing 'Functions.HttpExample' (Reason='This function was programmatically called via the host APIs.', Id=cf6da69c-fbf9-436a-89a6-94030011cf6f)
[2024-04-18T06:42:47.577Z] @azure/opentelemetry-instrumentation-azure-sdk [
[2024-04-18T06:42:47.577Z]   'Module @azure/core-tracing has been loaded before @azure/opentelemetry-instrumentation-azure-sdk so it might not work, please initialize it before requiring @azure/core-tracing'
[2024-04-18T06:42:47.577Z] ]
[2024-04-18T06:42:47.593Z] Invalid metric name: "\ASP.NET Applications(??APP_W3SVC_PROC??)\Request Execution Time". The metric name should be a ASCII string with a length no greater than 255 characters. []
[2024-04-18T06:42:47.593Z] Invalid metric name: "\ASP.NET Applications(??APP_W3SVC_PROC??)\Requests/Sec". The metric name should be a ASCII string with a length no greater than 255 characters. []
[2024-04-18T06:42:47.593Z] Invalid metric name: "\Process(??APP_WIN32_PROC??)\Private Bytes". The metric name should be a ASCII string with a length no greater than 255 characters. []
[2024-04-18T06:42:47.593Z] Invalid metric name: "\Memory\Available Bytes". The metric name should be a ASCII string with a length no greater than 255 characters. []
[2024-04-18T06:42:47.594Z] Invalid metric name: "\Processor(_Total)\% Processor Time". The metric name should be a ASCII string with a length no greater than 255 characters. []
[2024-04-18T06:42:47.594Z] Invalid metric name: "\Process(??APP_WIN32_PROC??)\% Processor Time". The metric name should be a ASCII string with a length no greater than 255 characters. []
[2024-04-18T06:42:47.605Z] Accessing resource attributes before async attributes settled []
[2024-04-18T06:42:47.606Z] Accessing resource attributes before async attributes settled []
[2024-04-18T06:42:47.608Z] Accessing resource attributes before async attributes settled []
[2024-04-18T06:42:47.608Z] Accessing resource attributes before async attributes settled []
[2024-04-18T06:42:47.655Z] HTTP trigger function processed a request.
[2024-04-18T06:42:47.732Z] Executed 'Functions.HttpExample' (Succeeded, Id=cf6da69c-fbf9-436a-89a6-94030011cf6f, Duration=259ms)
[2024-04-18T06:42:52.609Z] Envelopes could not be exported and are not retriable. Error message: [
[2024-04-18T06:42:52.609Z]   'Error "body.data.baseData.ver cannot be null or undefined." occurred in serializing the payload - "body".'
[2024-04-18T06:42:52.610Z] ]
[2024-04-18T06:42:52.610Z] {"stack":"Error: Error \"body.data.baseData.ver cannot be null or undefined.\" occurred in serializing the payload - \"body\".\n    at serializeRequestBody (/Users/jren/src/function-v3/node_modules/@azure/core-client/dist/commonjs/serializationPolicy.js:119:19)\n    at Object.sendRequest (/Users/jren/src/function-v3/node_modules/@azure/core-client/dist/commonjs/serializationPolicy.js:28:17)\n    at /Users/jren/src/function-v3/node_modules/@azure/core-rest-pipeline/dist/commonjs/pipeline.js:54:31\n    at HttpPipeline.sendRequest (/Users/jren/src/function-v3/node_modules/@azure/core-rest-pipeline/dist/commonjs/pipeline.js:57:16)\n    at ApplicationInsightsClient.sendRequest (/Users/jren/src/function-v3/node_modules/@azure/core-client/dist/commonjs/serviceClient.js:48:30)\n    at ApplicationInsightsClient.sendOperationRequest (/Users/jren/src/function-v3/node_modules/@azure/core-client/dist/commonjs/serviceClient.js:110:44)\n    at ApplicationInsightsClient.track (/Users/jren/src/function-v3/node_modules/@azure/monitor-opentelemetry-exporter/dist/index.js:1834:21)\n    at HttpSender.send (/Users/jren/src/function-v3/node_modules/@azure/monitor-opentelemetry-exporter/dist/index.js:2800:38)\n    at HttpSender.exportEnvelopes (/Users/jren/src/function-v3/node_modules/@azure/monitor-opentelemetry-exporter/dist/index.js:2589:55)\n    at /Users/jren/src/function-v3/node_modules/@azure/monitor-opentelemetry-exporter/dist/index.js:3864:47","message":"Error \"body.data.baseData.ver cannot be null or undefined.\" occurred in serializing the payload - \"body\".","name":"Error"} []

Here is my function app

import { AzureFunction, Context, HttpRequest } from "@azure/functions"
import { TelemetryClient } from "applicationinsights";

const telemetryClient = new TelemetryClient();

const httpTrigger: AzureFunction = async function (context: Context, req: HttpRequest): Promise<void> {
    context.log('HTTP trigger function processed a request.');
    const name = (req.query.name || (req.body && req.body.name));
    const responseMessage = name
        ? "Hello, " + name + ". This HTTP triggered function executed successfully."
        : "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.";

    // Create an event telemetry object.
    let eventTelemetry = {
        name: "testEvent"
      };

    // Send the event telemetry object to Azure Monitor Application Insights.
    telemetryClient.trackEvent(eventTelemetry);

    context.res = {
        // status: 200, /* Defaults to 200 */
        body: responseMessage
    };

};

export default httpTrigger;
JacksonWeber commented 2 months ago

@jerryjren That's normal for the OpenTelemetry based SDK. OpenTelemetry complains about the existence of spaces in the metric names for performance counters, but functionality shouldn't be impacted.

jerryjren commented 2 months ago

@jerryjren That's normal for the OpenTelemetry based SDK. OpenTelemetry complains about the existence of spaces in the metric names for performance counters, but functionality shouldn't be impacted.

Cool, just a warning then. Thanks

Last question, do you know which table in App insights will contain the above events? I cannot see anything in customEvents.

I do see logs in requests table, so that can confirm the connection string is correct.

Thanks again

JacksonWeber commented 2 months ago

@jerryjren In OpenTelemetry context (which the beta and now application insights 3.X SDK is built on) customEvents are a form of log. They should appear in the "Transaction Search" blade in the Azure portal. Please let me know if you're not seeing them there. Thanks!

jerryjren commented 2 months ago

@jerryjren In OpenTelemetry context (which the beta and now application insights 3.X SDK is built on) customEvents are a form of log. They should appear in the "Transaction Search" blade in the Azure portal. Please let me know if you're not seeing them there. Thanks!

unfortunately, I'm not seeing anything :(

image

Below is from requests table of same Application insights, just to confirm the application is able to connect to Application insights image

Update, I'm seeing this error in my function output. Could this be why?

[2024-04-19T05:11:03.217Z] {"stack":"Error: Error \"body.data.baseData.ver cannot be null or undefined.\" occurred in serializing the payload - \"body\".\n at serializeRequestBody (/Users/jren/src/function-v3/node_modules/@azure/core-client/dist/commonjs/serializationPolicy.js:119:19)\n at Object.sendRequest (/Users/jren/src/function-v3/node_modules/@azure/core-client/dist/commonjs/serializationPolicy.js:28:17)\n at /Users/jren/src/function-v3/node_modules/@azure/core-rest-pipeline/dist/commonjs/pipeline.js:54:31\n at HttpPipeline.sendRequest (/Users/jren/src/function-v3/node_modules/@azure/core-rest-pipeline/dist/commonjs/pipeline.js:57:16)\n at ApplicationInsightsClient.sendRequest (/Users/jren/src/function-v3/node_modules/@azure/core-client/dist/commonjs/serviceClient.js:48:30)\n at ApplicationInsightsClient.sendOperationRequest (/Users/jren/src/function-v3/node_modules/@azure/core-client/dist/commonjs/serviceClient.js:110:44)\n at ApplicationInsightsClient.track (/Users/jren/src/function-v3/node_modules/@azure/monitor-opentelemetry-exporter/dist/index.js:1834:21)\n at HttpSender.send (/Users/jren/src/function-v3/node_modules/@azure/monitor-opentelemetry-exporter/dist/index.js:2800:38)\n at HttpSender.exportEnvelopes (/Users/jren/src/function-v3/node_modules/@azure/monitor-opentelemetry-exporter/dist/index.js:2589:55)\n at /Users/jren/src/function-v3/node_modules/@azure/monitor-opentelemetry-exporter/dist/index.js:3864:47","message":"Error \"body.data.baseData.ver cannot be null or undefined.\" occurred in serializing the payload - \"body\".","name":"Error"} []

JacksonWeber commented 2 months ago

@jerryjren Yeah, that looks to be the reason you're not receiving telemetry. Are you still on 3.0.0-beta.11? Or did you upgrade to version 3.0.0 of Application Insights? If you did upgrade, are you using the client.track() method in your code?

jerryjren commented 2 months ago

@jerryjren Yeah, that looks to be the reason you're not receiving telemetry. Are you still on 3.0.0-beta.11? Or did you upgrade to version 3.0.0 of Application Insights? If you did upgrade, are you using the client.track() method in your code?

I'm using 3.0.0-beta.12 now, and I'm using telemetryClient.trackEvent(eventTelemetry); in my code.

Just tried using 3.0.0, saw same errors in the output.

JacksonWeber commented 2 months ago

@jerryjren Thank you for raising this issue. After investigation, I've resolved the issue with this PR: https://github.com/microsoft/ApplicationInsights-node.js/pull/1312 and the fix will be available in the next release.

jerryjren commented 2 months ago

@jerryjren Thank you for raising this issue. After investigation, I've resolved the issue with this PR: #1312 and the fix will be available in the next release.

Thanks for addressing this issue promptly.

One last question, assuming this is working and we are able to see logs in Application Insights, how could we export the logs in Open Telemetry format and forward them to 3rd party i.e.cloudwatch ?

I know this may not be the right place to ask this question. All good if you don't know.

Thanks again

balman commented 1 month ago

@jerryjren It looks like the SDK is not compatible with @azure/functions 4.3.0's types. Could you try setting your @azure/functions package version to 3.2.0?

@JacksonWeber, is downgrading to @azure/functions 3.2.0 the recommended fix for the error originally reported as part of this issue? When running a TypeScript-based function app with @azure/functions 4.5.0 and applicationinsights 3.1.0 published four days ago, I'm seeing the same error:

Namespace '"/.../node_modules/@azure/functions/types/index"' has no exported member 'Context'.

Downgrading to @azure/functions@3.2.0 results in issues of its own, since some methods/type declarations built in to the Azure Functions v4 programming model are missing in that version.

gunzip commented 1 month ago

Same issue here.

It looks like the SDK is not compatible with @azure/functions 4.3.0's types.

Is there any plan to make the AI sdk compatible with Azure Functions v4 programming model (types and runtime)? @JacksonWeber

We re encountering difficulties with correlation in Azure Functions v4 due to mismatches in context formatting. For instance, the traceparent property has been renamed to traceParent in the latest version (4.x) InvocationContext, among other discrepancies. This issue is particularly problematic because, as far as I know, there isn't a current workaround to enable correlation in Azure Function v4. You might consider manually invoking appInsights.startOperation(ctx), but even this approach fails due to the inconsistency in context properties. This situation is a significant blocker.

kwooccli commented 1 month ago

fyi. #1312 did not fix this issue. still seeing this same error with 3.1.0

JacksonWeber commented 1 month ago

@jerryjren It looks like the SDK is not compatible with @azure/functions 4.3.0's types. Could you try setting your @azure/functions package version to 3.2.0?

@JacksonWeber, is downgrading to @azure/functions 3.2.0 the recommended fix for the error originally reported as part of this issue? When running a TypeScript-based function app with @azure/functions 4.5.0 and applicationinsights 3.1.0 published four days ago, I'm seeing the same error:

Namespace '"/.../node_modules/@azure/functions/types/index"' has no exported member 'Context'.

Downgrading to @azure/functions@3.2.0 results in issues of its own, since some methods/type declarations built in to the Azure Functions v4 programming model are missing in that version.

Yes, support for Azure Functions v4 programming model will be coming to OpenTelemetry and we'll be adding that instrumentation to Application Insights. I can alert folks on this issue when that support is rolled out. For now, there isn't support past the v3 programming model unfortunately.

JacksonWeber commented 1 month ago

fyi. #1312 did not fix this issue. still seeing this same error with 3.1.0

When you refer to "this issue", you're referring to the type issue with the v4 programming model, correct? 3.1.0 resolved serialization errors, but does not add support for the v4 programming model. That support will be coming later based on an OpenTelmetry Azure Functions instrumentation.

magnum6actual commented 19 hours ago

What's the timeline on this? We're on functions 4.5.0 and would really like to implement application insights for custom events/tags.