-
Notifications
You must be signed in to change notification settings - Fork 836
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
base: main
Are you sure you want to change the base?
Converge sync and async resources #5350
Conversation
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 {}; | ||
} | ||
} |
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.
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]
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.
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'; |
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.
Is this an intentional deep-import? 🤔
|
||
public static EMPTY = new Resource({}); |
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.
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) { |
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.
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. |
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.
* 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 |
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.
* 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 |
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.
* 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, |
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.
Hmm, is there a reason browserDetectorSync
is still here? 🤔
hostDetector, | ||
hostDetectorSync, | ||
noopDetector, |
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.
We did not have this exported, before - is this on purpose?
// 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'); | ||
// }); | ||
// }); |
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.
hmm, this looks like a leftover.
This PR makes it so that any resource detector can detect sync and async resources, returning both in a single resource object.
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.