How to use apm.setUserContext in Angular with the logged in user info

Kibana version: 7.9.2

Elasticsearch version: 7.9.2

APM Server version: 7.9.2

APM Agent language and version: Angular 8

Hi,

I'm trying to use the Angular Rum agent for a frontend application.
It's working and sending transaction information to APM.
Now, I want to add the user information of the logged in user to the transactions with apm.setUserContext.
I'm able to do this with hardcoded variables, but when I try to get the info from a function it's empty.

This is my working app.module.ts part

export class AppModule {

  userContextId = '666';
  userContextName = 'TestUserName';
  userContextEmail = 'testemail@foo.com';

  constructor(@Inject(ApmService) service: ApmService){

    const apm = service.init({
      serviceName: 'Frontend Local',
      environment: 'Local',
      serverUrl: constants.apmURL,
      distributedTracingOrigins: [constants.distributedTracingOrigins],
      serviceVersion: version
    })

    apm.setUserContext({
      'id': this.userContextId,
      'username': this.userContextName,
      'email': this.userContextEmail
    })
  }
 }

How do I send real info instead of the test info?
I have an authService where the userprofile info is available, but I can't get this into the apm events.
For example with:
getUserName() {
return this._authService.authContext.userProfile.name
}

Even with the following very simple test it doesn't work:
getUserName() {
return 'MyName'
}
With of course:
apm.setUserContext({
'id': this.userContextId,
'username': this.getUserName,
'email': this.userContextEmail
})

Kind regards,
Peter

Hi @PeterDK,

Thanks for reaching out.

Your questions seems to be related to Angular rather than to the elastic APM. But you should be able to inject your AuthService into the AppModule and use that to configure the user information. Please see Angular's dependency injection guide for more detail.

Hope this helps,
Hamid

That's what I tried, but that doesn't seem to work. On some point I will need to call a function for that, but even with a simple function it does not get the info.

I tried this again:

apm.setUserContext({
      'id': '666',
      'username': this.getUserName,
      'email': 'testemail@foo.com'
    })
  }

  getUserName() {
    return 'My user name'
  }

I see this in the events:
user.email:testemail@foo.com
user.id:666

But user.name is missing.

Or I'm I just doing something stupid :thinking:

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.