Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(instrumentation-mongodb): support aggregation commands and support nested statements #1728

Merged
merged 5 commits into from
Jun 7, 2024

Conversation

Fox32
Copy link
Contributor

@Fox32 Fox32 commented Oct 11, 2023

Which problem is this PR solving?

At Serrala, we have a lot of micro services build with different languages that we observe with OpenTelemetry. We noticed that the mongo instrumentation of Node lacks some details compared to the Java instrumentation:

  • Aggregations are not supported (they are just displayed as mongo.command).
  • db.statement is sanitized to a degree that it doesn't contain enough details to pin down what the query is doing.

Short description of the changes

This PR adds support for aggregation command. As aggregation pipelines typically have a nested structure to describe the statement, the current way of sanitizing only leaves a low amount of hints what the command is about:

For example:

collection
  .aggregate([
    { $match: { key: 'value' } },
    { $group: { _id: '$a', count: { $sum: 1 } } },
  ])
  .toArray()

Is sanitized to:

{
    aggregate: "?",
    pipeline: "?",
    cursor: "?",
}

The Java instrumentation is handling statement sanitizing differently. Instead of replacing all values of the top level object, it traverses into nested objects and array, keeping the existing structure. This PR implements the same behavior for the Node instrumentation. Therefore db.statement is in this case:

{
    aggregate: "?",
    pipeline: [
      { $match: { key: "?" } },
      { $group: { _id: "?", count: { $sum: "?" } } },
    ],
    cursor: {},
}

While it is possible to configure a customer sanitizer for commands, I think it would better to add support for this upstream for better compatibility to the Java instrumentation.

I fixed some small typos I stumbled upon while reading through the contribution guidelines - I hope it is fine to include them here.

@Fox32 Fox32 requested a review from a team as a code owner October 11, 2023 21:04
@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Oct 11, 2023

CLA Signed

The committers listed above are authorized under a signed CLA.

  • ✅ login: Fox32 / name: Oliver Sand (ea52e6a, 033f51c)
  • ✅ login: pichlermarc / name: Marc Pichler (1d74905, c62e76b)
  • ✅ login: JamieDanielson / name: Jamie Danielson (b60d3d3)

@pichlermarc pichlermarc changed the title feat(opentelemetry-instrumentation-mongodb): support aggregation commands and support nested statements feat(instrumentation-mongodb): support aggregation commands and support nested statements Nov 14, 2023
Copy link

codecov bot commented Nov 14, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 91.43%. Comparing base (dfb2dff) to head (e1a8bed).
Report is 152 commits behind head on main.

Current head e1a8bed differs from pull request most recent head b60d3d3

Please upload reports for the commit b60d3d3 to get more accurate results.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1728      +/-   ##
==========================================
+ Coverage   90.97%   91.43%   +0.45%     
==========================================
  Files         146      139       -7     
  Lines        7492     7179     -313     
  Branches     1502     1449      -53     
==========================================
- Hits         6816     6564     -252     
+ Misses        676      615      -61     
Files Coverage Δ
...try-instrumentation-mongodb/src/instrumentation.ts 53.95% <100.00%> (+0.68%) ⬆️
...etry-instrumentation-mongodb/src/internal-types.ts 100.00% <100.00%> (ø)

... and 40 files with indirect coverage changes

@Fox32
Copy link
Contributor Author

Fox32 commented Nov 14, 2023

@pichlermarc I'm not sure whether the failing tests are related to my changes at all, looks more like a flaky test?

@david-luna
Copy link
Contributor

@Fox32 thanks for contributing to opentelementry :)

there has been recent changes that conflict with your branch (#1760)
resolve the conflicts and push again (ping me if you need help). As a side effect it will run again the CI so we can check if we get the same error in propagation-utils package.

…ands and support nested statements

Signed-off-by: Oliver Sand <[email protected]>
@Fox32
Copy link
Contributor Author

Fox32 commented Nov 30, 2023

@david-luna Thanks for notifying me. I updated the PR, can you retrigger the pipeline?

@david-luna
Copy link
Contributor

Sorry I can't trigger the CI

cc: @open-telemetry/javascript-maintainers

Copy link
Contributor

@david-luna david-luna left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks again @Fox32 for your contribution :)

Semantic conventions define the db.statement as a string value (link). Also the specific section for mongodb does not mention anything about statements (link)

However Java instrumentation and even this one also send an object in some cases (haven't checked other agents). I guess this is something that should be updated in semantic-conventions and we should check if there is an issue for that (or crate one if not).

Saying that this PR looks good :)

update semantic conventions imports
@JamieDanielson
Copy link
Member

Looks like the merge from main made the change for SemanticAttributes.* changing to SEMATTRS_*, which is what caused the tests to fail. I've just commited those changes are will see how the tests do this time around 😄

@pichlermarc pichlermarc enabled auto-merge (squash) June 7, 2024 12:53
@pichlermarc pichlermarc merged commit 2b1360d into open-telemetry:main Jun 7, 2024
4 checks passed
@dyladan dyladan mentioned this pull request Jun 6, 2024
@Fox32 Fox32 deleted the mongo-statement-serializer branch June 7, 2024 14:52
@Fox32
Copy link
Contributor Author

Fox32 commented Jun 7, 2024

@JamieDanielson thanks for taking care!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants