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 3 commits into
base: main
Choose a base branch
from

Conversation

dyladan
Copy link
Member

@dyladan dyladan commented Jan 17, 2025

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.

@dyladan dyladan requested a review from a team as a code owner January 17, 2025 15:19
Comment on lines 16 to 26
import * as assert from 'assert';
import { ResourceDetectionConfig } from '../../src';
import { DetectedResource, ResourceDetector } from '../../src/types';

// DO NOT MODIFY THIS DETECTOR: Previous detectors used Resource as IResource did not yet exist.
// If compilation fails at this point then the changes made are breaking.
class RegressionTestResourceDetector_1_9_1 implements Detector {
async detect(_config?: ResourceDetectionConfig): Promise<Resource> {
return Resource.empty();
class RegressionTestResourceDetector_1_9_1 implements ResourceDetector {
detect(_config?: ResourceDetectionConfig): DetectedResource {
return {};
}
}
Copy link
Member

@pichlermarc pichlermarc Jan 17, 2025

Choose a reason for hiding this comment

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

This test can actually go now - there's no need to test for compatibility with 1.9.1 anymore in 2.x
Edit: also allows us to drop the dependency on @opentelemetry/[email protected]

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 :)

@@ -17,6 +17,7 @@ import { Suite } from 'mocha';
import * as assert from 'assert';
import { BROWSER_ATTRIBUTES } from '../src/types';
import { IResource } from '@opentelemetry/resources';
import { DetectedResource } from '@opentelemetry/resources/src/types';
Copy link
Member

Choose a reason for hiding this comment

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

Is this an intentional deep-import? 🤔


public static EMPTY = new Resource({});
Copy link
Member

Choose a reason for hiding this comment

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

If in the future resources become mutable, would we be able to defend ourselves against this being mutated? 🤔

return attrs;
}

public merge(resource: Resource | null) {
Copy link
Member

Choose a reason for hiding this comment

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

Hmm, I think here we may run into a #5283-like situation, we won't be able to merge resources of different package versions.


/**
* BrowserDetector will be used to detect the resources related to browser.
* BrowserDetectorSync will be used to detect the resources related to browser.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* BrowserDetectorSync will be used to detect the resources related to browser.
* BrowserDetector can be used to detect the resources related to browser.


/**
* EnvDetector can be used to detect the presence of and create a Resource
* EnvDetectorSync can be used to detect the presence of and create a Resource
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* EnvDetectorSync can be used to detect the presence of and create a Resource
* EnvDetector can be used to detect the presence of and create a Resource


/**
* OSDetector detects the resources related to the operating system (OS) on
* OSDetectorSync detects the resources related to the operating system (OS) on
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* OSDetectorSync detects the resources related to the operating system (OS) on
* OSDetector detects the resources related to the operating system (OS) on

export { ResourceDetectionConfig } from './config';
export { detectResources } from './detect-resources';
export {
browserDetector,
browserDetectorSync,
Copy link
Member

Choose a reason for hiding this comment

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

Hmm, is there a reason browserDetectorSync is still here? 🤔

hostDetector,
hostDetectorSync,
noopDetector,
Copy link
Member

Choose a reason for hiding this comment

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

We did not have this exported, before - is this on purpose?

Comment on lines +340 to +362
// describe('compatibility', () => {
// it('should merge resource with old implementation', () => {
// const resource = Resource.EMPTY;
// const oldResource = new Resource190({ fromold: 'fromold' });

const mergedResource = resource.merge(oldResource);
// const mergedResource = resource.merge(oldResource);

assert.strictEqual(mergedResource.attributes['fromold'], 'fromold');
});
// assert.strictEqual(mergedResource.attributes['fromold'], 'fromold');
// });

it('should merge resource containing async attributes with old implementation', async () => {
const resource = new Resource(
{},
Promise.resolve({ fromnew: 'fromnew' })
);
const oldResource = new Resource190({ fromold: 'fromold' });
// it('should merge resource containing async attributes with old implementation', async () => {
// const resource = new Resource({
// attributes: { fromNew: Promise.resolve('fromnew') },
// });
// const oldResource = new Resource190({ fromold: 'fromold' });

const mergedResource = resource.merge(oldResource);
assert.strictEqual(mergedResource.attributes['fromold'], 'fromold');
// const mergedResource = resource.merge(oldResource);
// assert.strictEqual(mergedResource.attributes['fromold'], 'fromold');

await mergedResource.waitForAsyncAttributes?.();
assert.strictEqual(mergedResource.attributes['fromnew'], 'fromnew');
});
});
// await mergedResource.waitForAsyncAttributes?.();
// assert.strictEqual(mergedResource.attributes['fromnew'], 'fromnew');
// });
// });
Copy link
Member

Choose a reason for hiding this comment

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

hmm, this looks like a leftover.

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.

2 participants