DataDog / dd-trace-js

JavaScript APM Tracer
https://docs.datadoghq.com/tracing/
Other
617 stars 296 forks source link

Masking mongodb statements . #3776

Open kosukeoya opened 8 months ago

kosukeoya commented 8 months ago

I would like the ability to mask mongodb statements. In older versions, as shown in the image below, the part corresponding to the value of a statement is masked by ? as shown in the image below.

スクリーンショット 2023-11-08 11 30 53

In newer versions, the actual value is displayed. There are two use cases.

Masking was confirmed in version 0.34.0. I am not sure in which version the change was made, but I can confirm that the masking is at least no longer applied after 3.21.0.

vanstinator commented 7 months ago

We wanted to do something similar at Plex and came up with the following:

Set this mongo option in your tracer config:

tracer.use('mongodb-core', {
  queryInResourceName: true
});

And on your Agent config set the following env var (I can't promise it's the most efficient regex in the world, but it works for us and we're processing an eye watering amount of mongodb spans):

    - name: DD_APM_REPLACE_TAGS
      value: '[{"name":"resource.name","pattern":":\\[(?:\"[^\"]+(?:\"|$)|,|null|\"|\\[])+(?:]|$)","repl":":[\"{val}\"]"},{"name":"resource.name","pattern":":\"(?:[^\"]+)\"|:null","repl":":\"{val}\""},{"name":"resource.name","pattern":":\\d+","repl":":{num}"},{"name":"resource.name","pattern":"\\[(?:[\\d]+|,)+]","repl":"[{num}]"}]'

This will give you unique resource names per query shape similar to how express or fastify would be represented with the url path as the resource name. You can then avoid custom metrics entirely (which have the potential to be sampled if you're generating them from indexed traces) and you're getting the full rich APM data at the granular query level like you're getting elsewhere. Watchdog can then pick up on latency changes for specific queries too which has been really great.

We've been using this pattern at Plex for almost a year and love it.

EDIT: Honestly I'm almost surprised something like this isn't the default behavior because it's so much more useful, and richer, than the default OOTB experience.