ApplicationInsights-node.js icon indicating copy to clipboard operation
ApplicationInsights-node.js copied to clipboard

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

Open jerryjren opened this issue 1 year ago • 26 comments

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

jerryjren avatar Mar 17 '24 09:03 jerryjren

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.

JacksonWeber avatar Mar 20 '24 23:03 JacksonWeber

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

jerryjren avatar Mar 21 '24 00:03 jerryjren

@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?

JacksonWeber avatar Mar 25 '24 05:03 JacksonWeber

@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",

jerryjren avatar Mar 25 '24 20:03 jerryjren

@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 avatar Mar 26 '24 06:03 JacksonWeber

@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;

jerryjren avatar Apr 18 '24 06:04 jerryjren

@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.

JacksonWeber avatar Apr 18 '24 22:04 JacksonWeber

@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

jerryjren avatar Apr 18 '24 22:04 jerryjren

@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!

JacksonWeber avatar Apr 19 '24 00:04 JacksonWeber

@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"} []

jerryjren avatar Apr 19 '24 00:04 jerryjren

@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?

JacksonWeber avatar Apr 22 '24 16:04 JacksonWeber

@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.

jerryjren avatar Apr 22 '24 21:04 jerryjren

@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.

JacksonWeber avatar Apr 23 '24 00:04 JacksonWeber

@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

jerryjren avatar Apr 23 '24 04:04 jerryjren

@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/[email protected] 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.

balman avatar May 17 '24 20:05 balman

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.

gunzip avatar May 18 '24 21:05 gunzip

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

kwooccli avatar May 20 '24 16:05 kwooccli

@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/[email protected] 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 avatar May 22 '24 00:05 JacksonWeber

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.

JacksonWeber avatar May 22 '24 00:05 JacksonWeber

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.

magnum6actual avatar Jul 01 '24 16:07 magnum6actual

@magnum6actual Progress on support for the Azure Functions v4 programming model used by functions v4.5.0 can be found here: https://github.com/Azure/azure-functions-nodejs-library/issues/245.

JacksonWeber avatar Jul 03 '24 17:07 JacksonWeber

This will be fixed in next release https://github.com/Azure/azure-sdk-for-js/pull/30452

hectorhdzg avatar Jul 22 '24 19:07 hectorhdzg

It is not very clear if we can use applicationinsights node package now with Azure Functions. I am getting the same error with the latest versions of both applicationinsights (3.2.2) and @azure/functions (4.5).

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

51 export declare function startOperation(arg1: azureFunctionsTypes.Context | (http.IncomingMessage | azureFunctionsTypes.HttpRequest) | SpanContext | Span, arg2?: HttpRequest | string): ICorrelationContext | null;

gmantri avatar Aug 29 '24 01:08 gmantri

@gmantri I was able to produce a type issue with using the startOperation method in 3.2.2 myself. I'll investigate and work on getting these types compatible. In the meantime, could you provide a code snippet of the azure function that you're running that thows this message?

JacksonWeber avatar Aug 29 '24 19:08 JacksonWeber

@JacksonWeber - Thank you for looking into this issue. For some reason I did not get any notification about your reply. Essentially mine is a bit complicated project. I am using monorepo and combining NextJS and Azure Functions. Basically, I have a custom class called AppInsightsLogger and there I have implemented methods for logging traces, exceptions and events. This works great for my NextJS project but in Azure Function the moment I add this Logger, I start getting this error.

Here's the partial code:

export default class AppInsightsLogger implements ILogger {
  private readonly _client: TelemetryClient;

  public constructor() {
    const appInsights = require('applicationinsights');
    appInsights
      .setup(process.env.NEXT_PUBLIC_APP_INSIGHTS_CONNECTION_STRING)
      .start();
    this._client = appInsights.defaultClient;
  }

  public logOperation(operationContext: OperationContext): void {
    const data: Contracts.TraceTelemetry = {
      message: operationContext.message!,
      severity: KnownSeverityLevel.Verbose,
      time: operationContext.startTime,
      properties:
        this.getTelemetryPropertiesFromOperationContext(operationContext),
    };
    this._client.trackTrace(data);
  }
}

gmantri avatar Sep 03 '24 03:09 gmantri

@gmantri Thank you for the context! The above PR should solve that type error you were seeing and will be available upon the next release.

JacksonWeber avatar Sep 04 '24 00:09 JacksonWeber

This update is available publicly as of release. Please let me know if you have any further issues.

JacksonWeber avatar Sep 25 '25 23:09 JacksonWeber