In the python elastic-apm (v 6.2.3) library, how do I set the "user.roles" field?

I'm using the python elastic-apm library (v 6.2.3) to handle our elastic logging. I want to set the "user.roles" field described here ECS fields | APM Server Reference [7.15] | Elastic How do I do this?

The python elastic-apm library includes a set_user_context() method, but that doesn't take a "roles" parameter. The python elastic-apm library also includes a set_context() method, but when I call this method with the data parameter set to {"user.roles": ["test_role"]}, I don't see any effect. The method functions like a no-op. How do I set the "user.roles" field? Do you have a code example?

Description of the problem including expected versus actual behavior. Please include screenshots (if relevant):
Here's a screenshot of the two python methods I mentioned. Neither has a parameter for "user.roles"

I think your set_context() usage is not quite right. Try:

set_context({"roles": ["test_role"]}, "user")

You can see how set_user_context() utilizes set_context() here.

It would also be pretty trivial to add more ECS user fields to set_user_context, I'm just unsure how many/which fields to add, since there are a lot of fields under user in ECS.

Is there something else I need to do for this to work? I tried it and it's not working. The code comment mentions that the transaction needs to be sampled in order for this to work. I double-checked and my transactions are sampled.

Hmm, that's surprising. Are you just looking at the document in Elasticsearch or somewhere in the APM UI?

When I view a transaction in our Kibana, I see all of the fields that I've set for a transaction, except for user.roles. For instance, I'm setting the username by calling set_user_context('my_username'). What is special about this field?

The call set_user_context("my_username") is exactly equivalent to set_context({"username": "my_username"}, "user")

Looks like the APM Server intake is not looking for this field. We'll want to have you open an issue for this, I'm just checking which repo is best for this issue.

In the meantime, you could use set_custom_context to get this information onto your transaction documents, it just won't be top level in the same way as the other user information.

I recommend opening an issue in GitHub - elastic/apm-server: APM Server asking for user.roles to be added to the intake.

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