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

feat(opentelemetry-resources): add schema url #5070

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

Conversation

taylorhelene
Copy link

@taylorhelene taylorhelene commented Oct 14, 2024

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.

  • [ x ] Bug fix (non-breaking change which fixes an issue)
  • [ x ] New feature (non-breaking change which adds functionality)

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:

  • Followed the style guidelines of this project
  • Unit tests have been added
  • Documentation has been updated

@taylorhelene taylorhelene requested a review from a team as a code owner October 14, 2024 17:59
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.

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
Copy link
Member

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 || '');
Copy link
Member

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

Suggested change
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. 🙂

Comment on lines 173 to 175
if (schemaUrl1 && schemaUrl2 && schemaUrl1 !== schemaUrl2) {
diag.warn('Schema URLs differ. Using the original schema URL.');
}
Copy link
Member

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(
Copy link
Member

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.

}
}
}
Copy link
Member

Choose a reason for hiding this comment

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

missing newline 🙂

Suggested change
}
}

@@ -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
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
private _schemaUrl?: string; // Added schemaUrl property
private _schemaUrl?: string;

attributes: ResourceAttributes,
asyncAttributesPromise?: Promise<ResourceAttributes>
asyncAttributesPromise?: Promise<ResourceAttributes>,
schemaUrl: string = '' // Added schemaUrl parameter
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
schemaUrl: string = '' // Added schemaUrl parameter
schemaUrl: string = ''

@@ -90,6 +87,7 @@ export class Resource implements IResource {
return {};
}
);
this._schemaUrl = schemaUrl; // Store the schemaUrl
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
this._schemaUrl = schemaUrl; // Store the schemaUrl
this._schemaUrl = schemaUrl;

Comment on lines 369 to 373
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.'));
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
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;
Copy link
Member

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.

Copy link

codecov bot commented Oct 15, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 93.27%. Comparing base (7293e69) to head (ab3f9c6).
Report is 2 commits behind head on main.

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           
Files with missing lines Coverage Δ
packages/opentelemetry-resources/src/Resource.ts 100.00% <100.00%> (ø)

@pichlermarc
Copy link
Member

Regarding the question about the OTLP exporters - this is now mostly related to @opentelemetry/otlp-transformer where the internal representation of Metrics/Spans/Logs is converted to OTLP (json or protobuf), that also includes the resource.

We have transformation code where we currently don't set the schema url (note that IResource there and IResource from @opentelemetry/resources are different types:

export function createResource(resource: ISdkResource): IResource {
return {
attributes: toAttributes(resource.attributes),
droppedAttributesCount: 0,
};
}

export interface IResource {
/** Resource attributes */
attributes: IKeyValue[];
/** Resource droppedAttributesCount */
droppedAttributesCount: number;
}

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. 🙂

@pichlermarc pichlermarc changed the title chore(opentelemetry-resopurces): add schema url feat(opentelemetry-resources): add schema url Oct 15, 2024
@taylorhelene
Copy link
Author

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);
Copy link
Member

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
Copy link
Member

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.

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.

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.

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.

(marking as changes requested)

@taylorhelene
Copy link
Author

taylorhelene commented Oct 28, 2024

Okay, I'll work on the requested changes.

@taylorhelene
Copy link
Author

I got stuck. Do I also change the tests where I had introduced the breaking changes?

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