-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
OS and Process resource detectors #1788
OS and Process resource detectors #1788
Conversation
Codecov Report
@@ Coverage Diff @@
## main #1788 +/- ##
=======================================
+ Coverage 78.3% 78.5% +0.2%
=======================================
Files 135 137 +2
Lines 7220 7288 +68
=======================================
+ Hits 5656 5727 +71
+ Misses 1318 1316 -2
+ Partials 246 245 -1
|
I think this seems like a reasonable way to add detection of these resources. Please add some tests and a |
924efe5
to
505a5d8
Compare
@Aneurysm9 Done! Let me know if it's more or less what you had in mind. |
I worry somewhat that the tests are just restatements of the logic being tested. Given the nature of these resource detectors though, I'm not sure there's a good way to mock up the environment for testing other than perhaps creating an interface and a struct with proxy methods hanging off of it so that you can mock that interface. Is that worth the effort here? Do we expect |
I had the same impression while writing the tests. I also thought about the interface/mock approach, as I have done it before to test functions with dependencies on Additionally, there is a reason why I replicated the logic in the test file (the I could have defined those functions in the Instead my reasoning was "I assume the Besides that (which was more of a conceptual relief I've to admit 😅), the tests mostly validates that each function sets the expected attribute name (with the exception of the runtime description test maybe, that also validates the string format). I'm a bit concerned for the command args test, because is the most susceptible to what the test runner put on them. I will search for other ways to mock the environment, but so far didn't find anything. |
6154e84
to
70cb5c4
Compare
…d the os.type attribute
70cb5c4
to
811ad6a
Compare
I found this question, which gave me an idea for a possible lightweight form of indirection, allowing to mock the expected attribute values. I've added 98a3c8d to further explore the idea. But if you think the extra complexity doesn't pay for the benefits, I can revert to the previous version. |
886314c
to
98a3c8d
Compare
The approach of having local function variables that can be replaced in tests could work. I think I'd prefer to see those function variables only reference the For instance, rather than have: executableName executableNameProvider = func() string { return filepath.Base(os.Args[0]) }
commandArgs commandArgsProvider = func() []string { return os.Args }
...
// Detect returns a *Resource that describes the name of the process executable.
func (processExecutableNameDetector) Detect(ctx context.Context) (*Resource, error) {
return NewWithAttributes(semconv.ProcessExecutableNameKey.String(executableName())), nil
} I would have: commandArgs commandArgsProvider = func() []string { return os.Args }
...
// Detect returns a *Resource that describes the name of the process executable.
func (processExecutableNameDetector) Detect(ctx context.Context) (*Resource, error) {
return NewWithAttributes(semconv.ProcessExecutableNameKey.String(filepath.Base(commandArgs()[0]))), nil
} That way you can set up a table-based test for that detector that tests various inputs and ensures it produces the expected output:
|
That makes sense. I've streamlined the initial implementation based on your feedback, let me know what you think. These are the main changes:
p.s.: I think the CI is failing randomly. Is there a way to manually trigger a new run? |
Hi! I'm keeping this PR up to date with If you think the current approach ended up a bit twisted, maybe we can revert back to the more "naive" way of testing, and iterate the mocked approach in a separate PR? I also have a cross platform implementation for the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think these detectors are a great idea, thanks for the contribution. I'm a bit worried about the tests being more change detectors than regression tests as was pointed out. I'm good with accepting them as is though and if we determine them to be truly deficient we can address then.
🥇
Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>
Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>
Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>
* Replaced ToUpper with ToLower to match the current convention. * Used runtimeOS provider to get the OS name.
Hi @MrAlias, thanks for the feedback! I think I solved all the points, but let me know otherwise (don't know why the CI stalled with the latests commits). Yeah, I also feel that the tests could be expanded and/or improved somehow. Maybe as the codebase evolves (and some other resource detectors are added) a more common pattern emerges. At that point, count on me to revisit this implementation :) |
Hi,
This PR adds detectors for most of the attributes under the
OS
andProcess
resources.The only not implemented are:
process.command
process.command_line
os.description
New functions are provided under the
resource
package:resource.WithOSType()
resource.WithProcess()
resource.WithProcessPID()
resource.WithProcessExecutableName()
resource.WithProcessExecutablePath()
resource.WithProcessCommandArgs()
resource.WithProcessOwner()
resource.WithProcessRuntimeName()
resource.WithProcessRuntimeVersion()
resource.WithProcessRuntimeDescription()
Tests are
TODO
atm. Will add them if you think the PR it's a good fit for the package.Some output examples:
Linux (container):
Darwin:
Windows: