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

[TypeScript Compile Error] Property 'sessionManager' does not exist on type 'ITelemetryContext' #1526

Closed
thoo1 opened this issue Apr 8, 2021 · 3 comments

Comments

@thoo1
Copy link

thoo1 commented Apr 8, 2021

Description/Screenshot

  • when trying to access the automatic session id, by referencing appInsights.context.sessionManager.automaticSession.id, typescript throws an error "Property 'sessionManager' does not exist on type 'ITelemetryContext'"

Steps to Reproduce

  • try to reference appInsights.context.sessionManager
  • OS/Browser: N/A
  • SDK Version [e.g. 22]: "@microsoft/applicationinsights-web": "^2.6.1",
  • How you initialized the SDK:
    this.appInsights = new ApplicationInsights({
      config: {
        instrumentationKey: Environment.config.appInsightsInstrumentationKey,
        endpointUrl: Environment.config.appInsightsEndpointUrl,
        extensions: [this.reactPlugin],
        extensionConfig: {
          [this.reactPlugin.identifier]: { history }
        },
        disableFetchTracking: false,
        disableAjaxTracking: false,
        enableCorsCorrelation: true,
        disableCorrelationHeaders: false,
        enableAutoRouteTracking: true,
        maxAjaxCallsPerView: -1,
        enableResponseHeaderTracking: true,
        enableAjaxPerfTracking: true
      }
    });

Expected behavior

  • no typescript error

Additional context

@MSNev
Copy link
Collaborator

MSNev commented Apr 8, 2021

This is correct ITelemetryContext doesn't define or require a sessionManager as not all instances have it, this is because not all usage patterns actually include the PropertiesPlugin (which is where the sessionManager implementation is coming from), even though in your case the context does actually have the property.

For example, if you load this same Sku from a legacy snippet then the appInsights.context does NOT contain the sessionManager as the context instance is not that of the PropertiesPlugin. This is the primary reason it's not on the ITelemetryContext interface.

And as part of the tree-shaking improvements in the latest release the interfaces where better defined to identify these cases... Hence your current compile error.

Additionally the _SessionManager class (which is the current implementation of the sessionManager value) is not considered to be part of the public API surface and as such it is not explicitly exported, this is an internal helper class for managing the internal sessions -- Although, I find digging through the code it is exported via the PropertiesPlugin context type via the IPropTelemetryContext interface ** sigh ** will need to address this. I've actually found some internal code that is still broken because previously this was (and still should be -- IMHO) _sessionManager

So while in your specific case the sessionManager does (currently) exist, the only option would be to cast it and as the internal type or any

id = (appInsights.context as IPropTelemetryContext).sessionManager.automaticSession.id;
id = (appInsights.context as any).sessionManager.automaticSession.id;


Notes for person fixing/addressing this issue

  • Due to the public documented usage it looks like we will need to introduce a proper "public" definition of the sessionManager and expose as optional on the ITelemetryContext
  • The public sessionManager should only expose the minimal accessors (ie. the automaticSession)
  • Check and validate this doesn't break internal extensions of the ITelemetryContext including all the @microsoft/ids-xxx packages

Alternative would be to expose a "helper" to fetch the current "id" as would be applied to the event, and therefore avoid people having to check and access both the session.id and automaticSession.id for the current value that WILL get applied to an event.

  • add a getSessionId() or sesId() to the ITelemetryContext (like the appId())

@MSNev MSNev changed the title [BUG] Property 'sessionManager' does not exist on type 'ITelemetryContext' [TypeScript Compile Error] Property 'sessionManager' does not exist on type 'ITelemetryContext' Apr 8, 2021
@MSNev MSNev added this to the 2.x.x (Next Release) milestone Aug 4, 2021
@xiao-lix xiao-lix added the fixed - waiting release PR Committed and waiting deployment label Aug 6, 2021
@MSNev
Copy link
Collaborator

MSNev commented Sep 13, 2021

v2.7.0 is not fully deployed to NPM and all CDN endpoints

@MSNev MSNev closed this as completed Sep 13, 2021
@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 14, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants