TrilonIO/angular-application-insights

AppInsightsService.context is null, unable to add telemetry initializer

jason2211 opened this issue · 1 comments

Disregard! After posting this I restarted the ng server and the context started being populated. I'm guessing something didn't get built properly after the package update. Sorry!!

I'm working with the latest version and the context always seems to be null. I'm not able to use the addTelemetryInitializer to track a custom tag.

Versions
@markpieszak/ng-application-insights = 6.0.1
angular = 6.1.10

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { NgModule } from '@angular/core';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { ServiceWorkerModule } from '@angular/service-worker';
import { environment } from '../environments/environment';
import { CoreModule } from './core/core.module';

import { ApplicationInsightsModule, AppInsightsService } from '@markpieszak/ng-application-insights';

@NgModule({
  declarations: [
    AppComponent,
  ],
  imports: [
    BrowserModule,
    BrowserAnimationsModule,
    AppRoutingModule,
    ServiceWorkerModule.register('/ngsw-worker.js', { enabled: environment.production }),
    CoreModule.forRoot(),
    ApplicationInsightsModule.forRoot({ instrumentationKey: environment.applicationInsights.instrumentationKey })
  ],
  providers: [AppInsightsService],
  bootstrap: [AppComponent]
})
export class AppModule { }

app.comonent.ts

import { Component, OnInit } from '@angular/core';
import { AdalService } from 'adal-angular4';
import { environment } from '../environments/environment';
import { SocketService } from './core/services/socket.service';
import { AppInsightsService } from '@markpieszak/ng-application-insights';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent  {

  constructor(private adalService: AdalService, private socketService: SocketService, private appInsights: AppInsightsService) {
    adalService.init(environment.config);

    console.log('AppComponent ngOnInit', this.appInsights);

    this.appInsights.queue.push(() => {
      console.log('appInsights queue', this.appInsights, this.appInsights.context);

      this.appInsights.context.addTelemetryInitializer((envelope: Microsoft.ApplicationInsights.IEnvelope) => {
        console.log('telemetry init', envelope);
        envelope.tags['ai.device.roleName'] = "FetErp.Web";
      });

      console.log('appInsights queue end', this.appInsights, this.appInsights.context);
    });

    
  }
}

console output

app.component.ts:17 AppComponent ngOnInit AppInsightsService {_injector: NgModuleRef_, config: {…}, queue: Array(0), context: undefined}
app.component.ts:20 appInsights queue AppInsightsService {_injector: NgModuleRef_, config: {…}, queue: Array(2), context: undefined} undefined

The console logs for "telemetry init" and "appInsights queue end" are never displayed.

Should I be calling this from another location besides the app.component constructor?

No worries! I'm glad you got it all working. 👍
Strange it acted up like that, but it happens!