-
Notifications
You must be signed in to change notification settings - Fork 809
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
feat(opentelemetry-resources): add schema url #5070
base: main
Are you sure you want to change the base?
Conversation
Signed-off-by: taylorhelene <[email protected]>
Signed-off-by: taylorhelene <[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.
Thanks for taking the time to work on this - there's a few comment regarding possible breaking changes that need to be addressed before we can move on. 🙂
Working on the Resource
package is kind of tricky as it's essentially used everywhere. We need to be very sure that we don't introduce any breaking changes and there's lots of areas where this can happen.
/** | ||
* Returns the schema URL of the resource. | ||
*/ | ||
getSchemaUrl(): string ; // Add this line |
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 is a stable interface, we can only add new optional properties here - this is likely why you had to adjust the tests (adding as Resource
and as any
)
...((other as Resource)._syncAttributes ?? other.attributes), | ||
}; | ||
|
||
// Merge schema URLs, handling conflicts | ||
const mergedSchemaUrl = this._mergeSchemaUrls(this._schemaUrl || '', (other as Resource)._schemaUrl || ''); |
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 since IResource#getSchemaUrl()
is defined, we can use
const mergedSchemaUrl = this._mergeSchemaUrls(this._schemaUrl || '', (other as Resource)._schemaUrl || ''); | |
const mergedSchemaUrl = this._mergeSchemaUrls(this._schemaUrl || '', other.getSchemaUrl?.() || ''); |
and we can get rid of the back-cast. 🙂
if (schemaUrl1 && schemaUrl2 && schemaUrl1 !== schemaUrl2) { | ||
diag.warn('Schema URLs differ. Using the original schema URL.'); | ||
} |
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.
here we've already established that schema URL is a string (not undefined or null) though the type-system - why add null-checks? 🤔
* Helper function to merge schema URLs. If both schema URLs are present and differ, | ||
* a warning is logged and the first schema URL is prioritized. | ||
*/ | ||
private _mergeSchemaUrls( |
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 this would be better as a stand-alone utility function as it does not access anything on the instance.
} | ||
} | ||
} |
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.
missing newline 🙂
} | |
} | |
@@ -35,6 +35,7 @@ export class Resource implements IResource { | |||
private _syncAttributes?: ResourceAttributes; | |||
private _asyncAttributesPromise?: Promise<ResourceAttributes>; | |||
private _attributes?: ResourceAttributes; | |||
private _schemaUrl?: string; // Added schemaUrl property |
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.
private _schemaUrl?: string; // Added schemaUrl property | |
private _schemaUrl?: string; |
attributes: ResourceAttributes, | ||
asyncAttributesPromise?: Promise<ResourceAttributes> | ||
asyncAttributesPromise?: Promise<ResourceAttributes>, | ||
schemaUrl: string = '' // Added schemaUrl parameter |
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.
schemaUrl: string = '' // Added schemaUrl parameter | |
schemaUrl: string = '' |
@@ -90,6 +87,7 @@ export class Resource implements IResource { | |||
return {}; | |||
} | |||
); | |||
this._schemaUrl = schemaUrl; // Store the schemaUrl |
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._schemaUrl = schemaUrl; // Store the schemaUrl | |
this._schemaUrl = schemaUrl; |
const debugStub = sinon.spy(diag, 'warn'); | ||
const mergedResource = resource1.merge(resource2); | ||
|
||
assert.strictEqual(mergedResource.getSchemaUrl(), schemaUrl1); | ||
assert.ok(debugStub.calledWithMatch('Schema URLs differ. Using the original schema URL.')); |
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.
const debugStub = sinon.spy(diag, 'warn'); | |
const mergedResource = resource1.merge(resource2); | |
assert.strictEqual(mergedResource.getSchemaUrl(), schemaUrl1); | |
assert.ok(debugStub.calledWithMatch('Schema URLs differ. Using the original schema URL.')); | |
const warnStub = sinon.spy(diag, 'warn'); | |
const mergedResource = resource1.merge(resource2); | |
assert.strictEqual(mergedResource.getSchemaUrl(), schemaUrl1); | |
assert.ok(warnStub.calledWithMatch('Schema URLs differ. Using the original schema URL.')); |
@@ -20,7 +20,7 @@ import * as assert from 'assert'; | |||
// 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(); | |||
return Resource.empty() as any; |
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.
The comment above this detector still holds - if this needs to be modified, the changes in this PR are breaking.
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #5070 +/- ##
=======================================
Coverage 93.26% 93.27%
=======================================
Files 317 317
Lines 8195 8203 +8
Branches 1641 1646 +5
=======================================
+ Hits 7643 7651 +8
Misses 552 552
|
Regarding the question about the OTLP exporters - this is now mostly related to We have transformation code where we currently don't set the schema url (note that opentelemetry-js/experimental/packages/otlp-transformer/src/resource/internal.ts Lines 20 to 25 in 7ed67f9
opentelemetry-js/experimental/packages/otlp-transformer/src/resource/types.ts Lines 20 to 26 in 7ed67f9
so the schema url would be dropped on export. The last point I put on the issue was to ensure that we also add code for the transformation on the export, otherwise we'll hold it internally but we'd never export. 🙂 |
git commit -s -am "chore(opentelemetry-resources): merge"
…rters Signed-off-by: taylorhelene <[email protected]>
I have added some more changes, can you please review them . Thank you. |
@@ -342,11 +342,73 @@ describe('Resource', () => { | |||
); | |||
const oldResource = new Resource190({ fromold: 'fromold' }); | |||
|
|||
const mergedResource = resource.merge(oldResource); | |||
const mergedResource = resource.merge(oldResource as any); |
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 is also an indicator that this is a breaking change.
/** | ||
* Returns the schema URL of the resource. | ||
*/ | ||
getSchemaUrl(): string; // Add this line |
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 is a breaking change - @opentelemetry/resources
is a stable SDK package, and new properties added to interfaces must be optional.
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 need to address the comments from my previous review - otherwise we'll introduce breaking changes that will affect a large part of our userbase.
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.
(marking as changes requested)
Okay, I'll work on the requested changes. |
Signed-off-by: taylorhelene <[email protected]>
I got stuck. Do I also change the tests where I had introduced the breaking changes? |
Which problem is this PR solving?
Add Schema URL for resources : #4182
Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change.
I have added the schema URL to the Resource.ts and made the necessary changes to the IResource.ts. I have fixed the first two checkboxes and I am asking for clarification on the third because most of the exporters packages in the experimental folder are already exporting resources. I am trying to add a new feature request as per the issue request.
Fixes # (Schema URL can be added using the Resource constructor and Schema URL is handled according to the specification on Resource#merge)
Short description of the changes
I have added schemaUrl parameter, added a return function and merged schema URLs, handling conflicts
Type of change
Please delete options that are not relevant.
How Has This Been Tested?
Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration
I added some tests to the Resource.test.ts within the test suite.
cd opentelemetry-js/packages/opentelemetry-resources
npm run test
should initialize resource with schema URL
should merge resources with different schema URLs
should retain the same schema URL when merging resources with identical URL
should retain schema URL from the resource that has it when merging
Checklist: