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

add container id to ApiServer.State and send in header #38

Merged
merged 3 commits into from
Oct 23, 2021
Merged

add container id to ApiServer.State and send in header #38

merged 3 commits into from
Oct 23, 2021

Conversation

dallincrane
Copy link
Contributor

@dallincrane dallincrane commented Mar 10, 2021

Adds a Datadog-Container-ID header when a container is being used.
Modeled on the DD Node.js client code

Happy to add a couple tests if required but would need some guidance — the file read might require a mock setup.

headers = @headers ++ [{"X-Datadog-Trace-Count", length(traces)}]
headers = headers ++ List.wrap(if container_id, do: {"Datadog-Container-ID", container_id})
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@cgroup_uuid "[0-9a-f]{8}[-_][0-9a-f]{4}[-_][0-9a-f]{4}[-_][0-9a-f]{4}[-_][0-9a-f]{12}"
@cgroup_ctnr "[0-9a-f]{64}"
@cgroup_task "[0-9a-f]{32}-\\d+"
@cgroup_regex Regex.compile!("/(#{@cgroup_uuid}|#{@cgroup_ctnr}|#{@cgroup_task})(?:.scope)?$", "m")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@rocpatel
Copy link

adding a link to an issue that references this concern. #25

Copy link
Contributor

@kamilkowalski kamilkowalski left a comment

Choose a reason for hiding this comment

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

Hi @dallincrane - thanks for contributing! We could actually use this feature at my company, so I took the liberty to check it. I have just one comment as to the implementation.

When it comes to testing, I have two ideas:

  1. Inject the cgroup file path either through opts, or through application env, and default to /proc/self/cgroup. Then override that value in tests as e.g. test/support/cgroup.
  2. Extract cgroup parsing to a separate module and expose a function like parse/1 which takes the file path as an argument. Test that module separately, then use it in ApiServer. You could even use mox to mock that parsing module in tests.

Let me know what you think.

lib/spandex_datadog/api_server.ex Outdated Show resolved Hide resolved
@kamilkowalski
Copy link
Contributor

I can also help to test this - I'll spin up an environment with this change and see if it works.

@kamilkowalski
Copy link
Contributor

Seems to be working 👏
Screenshot_2021-04-14 refresh GET probe (env pr-23974-ns) Datadog

@dallincrane
Copy link
Contributor Author

Seems to be working 👏
Screenshot_2021-04-14 refresh GET probe (env pr-23974-ns) Datadog

Nice! At my last company we had tested it and had it in production. Glad you are seeing the same!

@dallincrane
Copy link
Contributor Author

Hi @kamilkowalski - thanks for input! Do you by chance have write access? If you don't, it looks like we need someone with write access to provide a review and perhaps give a little input into some of your suggestions.

For some background, I was trying to choose code that provided the least resistance and least amount of project structure change so it could be accepted and quickly released.

1. Inject the `cgroup` file path either through opts, or through application env, and default to `/proc/self/cgroup`. Then override that value in tests as e.g. `test/support/cgroup`.

I used Datadog's Javascript code as a refernence. It is hard coded there. I'm not sure how many projects would have a custom cgroup file path

2. Extract `cgroup` parsing to a separate module and expose a function like `parse/1` which takes the file path as an argument. Test that module separately, then use it in `ApiServer`. You could even use `mox` to mock that parsing module in tests.

Generally I do really like what you are suggesting and it even matches the Datadog code! But because adding a new file and module requires a naming discussion and mox isn't currently in the project, I decided to keep the PR simple.

@kamilkowalski
Copy link
Contributor

@dallincrane I'm not a maintainer so we probably need to wait for @zachdaniel or @GregMefford to chip in.

As to testing - I think sometimes it makes sense to extend the library's public interface to allow for testing (if the alternative is no tests). Also, the second solution doesn't necessarily require Mox - it only requires us to extract the cgroups functionality to a separate module.

@moleskin-smile
Copy link

moleskin-smile commented May 4, 2021

FYI: regex used in the reference implementation has been changed since this PR was opened: DataDog/dd-trace-js#1314.

@dallincrane
Copy link
Contributor Author

thank you @moleskin-smile! updated

@GregMefford
Copy link
Member

Hey there! Thanks again for this contribution! I finally got a chance to try it out and verified that it's working, so I will merge now and work on releasing an update version that include this. ❤️ 🚀

@GregMefford GregMefford merged commit 7fb6c34 into spandex-project:master Oct 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants