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

Base type inside envelope always equals to request data. #713

Closed
goto1 opened this issue Dec 9, 2020 · 3 comments
Closed

Base type inside envelope always equals to request data. #713

goto1 opened this issue Dec 9, 2020 · 3 comments

Comments

@goto1
Copy link

goto1 commented Dec 9, 2020

Hello,

I would like to be able to exclude specific requests based on the evenlope.data.baseType.

Currently, I have the following setup:

appInsights.setup().setSendLiveMetrics(true).start()

appInsights.defaultClient.addTelemetryProcessor(
  (envelope, context) => {
    const telemetryTypes = {
      Request: appInsights.Contracts.TelemetryTypeString.Request,
      Exception: appInsights.Contracts.TelemetryTypeString.Exception,
    }
    const isOfTelemetryType = (telemetryType) => envelope.data.baseType === telemetryType

    const isExceptionTelemetry = isOfTelemetryType(telemetryTypes.Exception)
    const isRequestTelemetry = isOfTelemetryType(telemetryTypes.Request)

    if (isExceptionTelemetry) return true
    if (isRequestTelemetry && process.env.NODE_ENV !== "development") return true

    return false
  }
)

However, it appears that evenlope.data.baseType always comes back as RequestData and I am never seeing any other types, such as ExceptionData, as specified in the example from the documentation,

const baseType = envelope.data.baseType // RequestData

This is true when I manually try to trigger an exception in one of my middleware functions,

// express middleware
app.use((req, res, next) => {
  appInsights.defaultClient.trackException({ exception: new Error("Hello World") })
  next()
})

I've looked at this issue #325, which suggests to look at the value of envelope.data.baseType to determine whether to send the telemetry or not, but I am always getting RequestData for baseType.

In the Azure portal, however, I am seeing those exceptions being logged no problem, so technically they should be getting processed through my custom telemetry processor.

Any tips would be greatly appreciated, thank you!


Setup information:

  • node v14.15.1
  • applicationinsights v1.8.8
@hectorhdzg
Copy link
Member

@goto1 I tried code snippet you added and telemetry processor works as expected, I can see "ExceptionData" as envelope.data.baseType, is it possible another telemetry processor was added and is modifying the envelope? if you are seeing correct events in Azure this could means there is some issue in your local environment, is also possible multiple telemetry events are triggered in your middleware function please validate all of them while debugging.

@goto1
Copy link
Author

goto1 commented Dec 9, 2020

@hectorhdzg thank you for your response, I re-installed my project dependencies and that seemed to fix the issue, thanks!


Also, if I may ask about something else, would you happen to know if there's a way to populate the client_Browser field, similarly to setting deviceOSVersion and userId context keys?

Currently I am using a custom property to store this information but don't always have the ability to filter or sort logs by custom properties.

appInsights.defaultClient.addTelemetryProcessor(
  (envelope, context) => {
    const getContextKey = (key) => appInsights.defaultClient.context.keys[key]
    const setContextKey = (key, value) => {
      envelope.tags[key] = value
    }
   
    const requestContext = appInsights.getCorrelationContext().requestContext // custom request context
    const data = envelope.data.baseData

    const { userId, os, browser } = requestContext

    // this is what I would like to do for `client_Browser`, or something similar
    // so that `client_Browser` field gets populated with appropriate information
    setContextKey(getContextKey("deviceOSVersion"), os)
    setContextKey(getContextKey("userId"), userId)

    // setting a custom property, but this doesn't populate the `client_Browser` field
    data.properties.browser = browser 

    return true
  }
)

Any help would be greatly appreciated, thanks!

@hectorhdzg
Copy link
Member

Glad it works now, you could use envelope.tags["ai.device.browser"]

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

No branches or pull requests

2 participants