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

**URL:** <https://discuss.elastic.co/t/in-the-python-elastic-apm-v-6-2-3-library-how-do-i-set-the-user-roles-field/298982>\
**Category:** APM\
**Tags:** python\
**Created:** [March 7, 2022, 3:27pm UTC](https://discuss.elastic.co/t/in-the-python-elastic-apm-v-6-2-3-library-how-do-i-set-the-user-roles-field/298982 "2022-03-07T15:27:18Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![fakeelasticjr](https://avatars.discourse-cdn.com/v4/letter/f/8797f3/32.png) [@fakeelasticjr](https://discuss.elastic.co/u/fakeelasticjr)\
**Post date:** [March 7, 2022, 3:27pm UTC](https://discuss.elastic.co/t/in-the-python-elastic-apm-v-6-2-3-library-how-do-i-set-the-user-roles-field/298982/1 "2022-03-07T15:27:18Z")

</div>

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](https://www.elastic.co/guide/en/apm/server/current/exported-fields-ecs.html#_user) 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"

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/1/8/1815e9827ea8f4c454c4b1a05a1992c07416a566.png)

---

<div class="post-metadata">

**Author:** ![basepi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/basepi/32/53056_2.png) [@basepi](https://discuss.elastic.co/u/basepi)\
**Post date:** [March 7, 2022, 6:41pm UTC](https://discuss.elastic.co/t/in-the-python-elastic-apm-v-6-2-3-library-how-do-i-set-the-user-roles-field/298982/2 "2022-03-07T18:41:32Z")

</div>

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

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

```

You can see how `set_user_context()` utilizes `set_context()` [here](https://github.com/elastic/apm-agent-python/blob/97188c362497910762e5b1ef8fc53e4011ec75b8/elasticapm/traces.py#L1106-L1114).

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.

---

<div class="post-metadata">

**Author:** ![fakeelasticjr](https://avatars.discourse-cdn.com/v4/letter/f/8797f3/32.png) [@fakeelasticjr](https://discuss.elastic.co/u/fakeelasticjr)\
**Post date:** [March 7, 2022, 9:42pm UTC](https://discuss.elastic.co/t/in-the-python-elastic-apm-v-6-2-3-library-how-do-i-set-the-user-roles-field/298982/3 "2022-03-07T21:42:41Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![basepi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/basepi/32/53056_2.png) [@basepi](https://discuss.elastic.co/u/basepi)\
**Post date:** [March 8, 2022, 4:52pm UTC](https://discuss.elastic.co/t/in-the-python-elastic-apm-v-6-2-3-library-how-do-i-set-the-user-roles-field/298982/4 "2022-03-08T16:52:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![fakeelasticjr](https://avatars.discourse-cdn.com/v4/letter/f/8797f3/32.png) [@fakeelasticjr](https://discuss.elastic.co/u/fakeelasticjr)\
**Post date:** [March 8, 2022, 9:51pm UTC](https://discuss.elastic.co/t/in-the-python-elastic-apm-v-6-2-3-library-how-do-i-set-the-user-roles-field/298982/5 "2022-03-08T21:51:29Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![basepi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/basepi/32/53056_2.png) [@basepi](https://discuss.elastic.co/u/basepi)\
**Post date:** [March 9, 2022, 6:27pm UTC](https://discuss.elastic.co/t/in-the-python-elastic-apm-v-6-2-3-library-how-do-i-set-the-user-roles-field/298982/6 "2022-03-09T18:27:54Z")

</div>

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](https://github.com/elastic/apm-server/blob/ba905ab6b4d02ccad1682db246e16826c8e5d85e/docs/spec/v2/transaction.json#L629-L672). 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`](https://www.elastic.co/guide/en/apm/agent/python/current/api.html#api-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.

---

<div class="post-metadata">

**Author:** ![basepi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/basepi/32/53056_2.png) [@basepi](https://discuss.elastic.co/u/basepi)\
**Post date:** [March 10, 2022, 4:13pm UTC](https://discuss.elastic.co/t/in-the-python-elastic-apm-v-6-2-3-library-how-do-i-set-the-user-roles-field/298982/7 "2022-03-10T16:13:44Z")

</div>

I recommend opening an issue in [GitHub - elastic/apm-server: APM Server](https://github.com/elastic/apm-server) asking for `user.roles` to be added to the intake.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [March 31, 2022, 12:13pm UTC](https://discuss.elastic.co/t/in-the-python-elastic-apm-v-6-2-3-library-how-do-i-set-the-user-roles-field/298982/8 "2022-03-31T12:13:56Z")

</div>

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