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

Converge sync and async resources #5350

Open
wants to merge 18 commits into
base: main
Choose a base branch
from

Conversation

dyladan
Copy link
Member

@dyladan dyladan commented Jan 17, 2025

Fixes: #3582

This PR makes it so that any resource detector can detect sync and async resources, returning both in a single resource object.

  • Internally, the resource now stores all key/value pairs in a list. This means the override order comes from the order of detection, not the order of when the promises are resolved. Rejected promises are skipped. This should make the order of resource detection significantly more obvious to end users. It also paves the way for entities which explicitly require detection priority to be defined by the order in which entity detectors are configured.
  • Resource detectors now return plain objects instead of new Resource instances. This should improve type compatibility in the future.
  • Detected resource interface contains an optional attributes key, which all attributes are nested under. This will make it easier to extend for entities in the future by adding a new key to the object.

To be done in follow-up:

@dyladan dyladan requested a review from a team as a code owner January 17, 2025 15:19
Copy link
Member

@pichlermarc pichlermarc left a comment

Choose a reason for hiding this comment

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

Thank you for working on this 🙂
A quick preliminary review - I'll have a closer look at the implementation details next week :)

packages/opentelemetry-resources/src/Resource.ts Outdated Show resolved Hide resolved
packages/opentelemetry-resources/src/Resource.ts Outdated Show resolved Hide resolved
packages/opentelemetry-resources/src/index.ts Outdated Show resolved Hide resolved
packages/opentelemetry-resources/src/index.ts Outdated Show resolved Hide resolved
packages/opentelemetry-resources/test/Resource.test.ts Outdated Show resolved Hide resolved
Copy link

codecov bot commented Jan 22, 2025

Codecov Report

Attention: Patch coverage is 85.16484% with 27 lines in your changes missing coverage. Please review.

Project coverage is 94.69%. Comparing base (c00f36e) to head (15f54f0).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
...lemetry-resources/src/detectors/BrowserDetector.ts 43.75% 9 Missing ⚠️
...ntelemetry-browser-detector/src/BrowserDetector.ts 0.00% 4 Missing ⚠️
...entelemetry-resources/src/detectors/EnvDetector.ts 93.87% 3 Missing ⚠️
...es/opentelemetry-resources/src/detect-resources.ts 80.00% 2 Missing ⚠️
...ntelemetry-resources/src/detectors/NoopDetector.ts 33.33% 2 Missing ⚠️
...tectors/platform/node/ServiceInstanceIdDetector.ts 33.33% 2 Missing ⚠️
packages/opentelemetry-resources/src/Resource.ts 97.87% 1 Missing ⚠️
...tors/platform/browser/ServiceInstanceIdDetector.ts 0.00% 1 Missing ⚠️
...ces/src/detectors/platform/node/ProcessDetector.ts 91.66% 1 Missing ⚠️
...rs/platform/node/machine-id/getMachineId-darwin.ts 66.66% 1 Missing ⚠️
... and 1 more
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5350      +/-   ##
==========================================
+ Coverage   94.54%   94.69%   +0.14%     
==========================================
  Files         318      311       -7     
  Lines        8051     7993      -58     
  Branches     1694     1686       -8     
==========================================
- Hits         7612     7569      -43     
+ Misses        439      424      -15     
Files with missing lines Coverage Δ
...imental/packages/opentelemetry-sdk-node/src/sdk.ts 96.01% <100.00%> (ø)
...ental/packages/opentelemetry-sdk-node/src/utils.ts 88.23% <100.00%> (ø)
...ges/opentelemetry-resources/src/detectors/index.ts 100.00% <100.00%> (ø)
...elemetry-resources/src/detectors/platform/index.ts 100.00% <100.00%> (ø)
...ources/src/detectors/platform/node/HostDetector.ts 100.00% <100.00%> (ø)
...esources/src/detectors/platform/node/OSDetector.ts 100.00% <100.00%> (ø)
...try-resources/src/detectors/platform/node/index.ts 100.00% <100.00%> (ø)
...ctors/platform/node/machine-id/getMachineId-bsd.ts 100.00% <100.00%> (ø)
...ors/platform/node/machine-id/getMachineId-linux.ts 100.00% <100.00%> (ø)
...ctors/platform/node/machine-id/getMachineId-win.ts 93.33% <100.00%> (ø)
... and 12 more

@@ -73,7 +73,7 @@ describe('getMachineId on BSD', () => {

const machineId = await getMachineId();

assert.equal(machineId, '');
assert.equal(machineId, undefined);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
assert.equal(machineId, undefined);
assert.strictEqual(machineId, undefined);

@@ -80,7 +80,7 @@ describe('getMachineId on Darwin', () => {

const machineId = await getMachineId();

assert.equal(machineId, '');
assert.equal(machineId, undefined);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
assert.equal(machineId, undefined);
assert.strictEqual(machineId, undefined);

@@ -60,7 +60,7 @@ describe('getMachineId on linux', () => {

const machineId = await getMachineId();

assert.equal(machineId, '');
assert.equal(machineId, undefined);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
assert.equal(machineId, undefined);
assert.strictEqual(machineId, undefined);

class EnvDetector implements Detector {
class EnvDetector implements ResourceDetector {
// Type, attribute keys, and attribute values should not exceed 256 characters.
private readonly _MAX_LENGTH = 255;
Copy link
Contributor

Choose a reason for hiding this comment

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

nit but is there a reason to prefer to have these as private readonly fields, as opposed to un-exported module-level constant? the latter seems more "truly" private, easier to type, and more mangle/tree-shaking friendly.

});
}

public get asyncAttributesPending() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
public get asyncAttributesPending() {
public get asyncAttributesPending(): boolean {

return attrs;
}

public getRawAttributes() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
public getRawAttributes() {
public getRawAttributes(): RawResourceAttribute[] {

detect(config?: ResourceDetectionConfig): Promise<IResource> {
return Promise.resolve(browserDetectorSync.detect(config));
class BrowserDetector implements ResourceDetector {
detect(config?: ResourceDetectionConfig) {
Copy link
Contributor

Choose a reason for hiding this comment

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

: DetectedResource ...if we want to be consistent about public interface return types, I'd leave a suggestion but this requires adding the import too

/**
* Returns a {@link Resource} populated with attributes from the
* OTEL_RESOURCE_ATTRIBUTES environment variable. Note this is an async
* function to conform to the Detector interface.
*
* @param config The resource detection config
*/
detect(config?: ResourceDetectionConfig): Promise<IResource> {
return Promise.resolve(envDetectorSync.detect(config));
detect(_config?: ResourceDetectionConfig) {
Copy link
Contributor

Choose a reason for hiding this comment

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

: DetectedResource ...if we want to be consistent about public interface return types, I'd leave a suggestion but this requires adding the import too

detect(): Promise<IResource> {
return Promise.resolve(noopDetectorSync.detect());
export class NoopDetector implements ResourceDetector {
detect() {
Copy link
Contributor

Choose a reason for hiding this comment

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

: DetectedResource ...if we want to be consistent about public interface return types, I'd leave a suggestion but this requires adding the import too

detect(_config?: ResourceDetectionConfig): Promise<IResource> {
return Promise.resolve(osDetectorSync.detect(_config));
class OSDetector implements ResourceDetector {
detect(_config?: ResourceDetectionConfig) {
Copy link
Contributor

Choose a reason for hiding this comment

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

: DetectedResource ...if we want to be consistent about public interface return types, I'd leave a suggestion but this requires adding the import too

detect(config?: ResourceDetectionConfig): Promise<IResource> {
return Promise.resolve(processDetectorSync.detect(config));
class ProcessDetector implements ResourceDetector {
detect(_config?: ResourceDetectionConfig) {
Copy link
Contributor

Choose a reason for hiding this comment

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

: DetectedResource ...if we want to be consistent about public interface return types, I'd leave a suggestion but this requires adding the import too

Copy link
Contributor

@chancancode chancancode left a comment

Choose a reason for hiding this comment

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

Take it with an unhealthy amount of salt, but other than the suggestions I left (feel free to make your own judgment call), it looks good to me!

Copy link
Contributor

@trentm trentm left a comment

Choose a reason for hiding this comment

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

I think this LGTM.

I asked a Q at #5358 (comment)
I'm not totally clear on what the user's experience with resources (usage with the SDK I mean) will be after these changes at #5358, especially when Entities come along.

Comment on lines 135 to 138
return Resource.FromAttributeList([
...resource._rawAttributes,
...this._rawAttributes,
]);
Copy link
Contributor

Choose a reason for hiding this comment

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

Might be nice to ahve a comment here that resource is first here because (a) the implementation is "first one wins" and (b) the spec says:

https://opentelemetry.io/docs/specs/otel/resource/sdk/#merge

If a key exists on both the old and updating resource, the value of the updating resource MUST be picked (even if the updated value is empty).

Copy link
Contributor

Choose a reason for hiding this comment

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

The package-lock diff is a little scary, but they often are.
Some deps are moving locations in the tree, which is often fine.
There are some dep downgrades, probably because "main" has received some renovatebot updates since this branch was created.

@dyladan Were you still going to revert this change:

--- a/packages/opentelemetry-resources/package.json
+++ b/packages/opentelemetry-resources/package.json
@@ -64,7 +64,6 @@
   },
   "devDependencies": {
     "@opentelemetry/api": ">=1.0.0 <1.10.0",
-    "@opentelemetry/resources_1.9.0": "npm:@opentelemetry/[email protected]",

and the package-lock.json change to do that on a separate PR?

I don't have a strong preference. The package-lock.json churn isn't the fault of this PR.

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.

Consolidate sync and async resource interfaces
4 participants