-
Notifications
You must be signed in to change notification settings - Fork 929
feat(metrics): add advisory attributes parameter to metric instruments #5783
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
Open
pratstick
wants to merge
6
commits into
open-telemetry:main
Choose a base branch
from
pratstick:feature/advisory-attributes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
bad0c9f
feat(api): add advisory attributes parameter to MetricAdvice
pratstick 09c03bd
feat(sdk-metrics): implement advisory attributes filtering
pratstick 82948e1
test(metrics): add tests for advisory attributes feature
pratstick d7c17af
docs(metrics): document advisory attributes parameter with example
pratstick 957e0bf
Update api/src/metrics/Metric.ts
pratstick b5eca96
feat(metrics): add advisory attributes parameter to metric instruments
pratstick File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
194 changes: 194 additions & 0 deletions
194
packages/sdk-metrics/test/advisory-attributes.integration.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import * as assert from 'assert'; | ||
import { MeterProvider } from '../src/MeterProvider'; | ||
import { TestMetricReader } from './export/TestMetricReader'; | ||
import { | ||
defaultInstrumentationScope, | ||
testResource, | ||
} from './util'; | ||
|
||
describe('Advisory attributes parameter - Integration test', () => { | ||
let meterProvider: MeterProvider; | ||
let metricReader: TestMetricReader; | ||
|
||
beforeEach(() => { | ||
metricReader = new TestMetricReader(); | ||
meterProvider = new MeterProvider({ | ||
resource: testResource, | ||
readers: [metricReader], | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
metricReader.shutdown(); | ||
}); | ||
|
||
it('should filter attributes based on advisory attributes parameter', async () => { | ||
const meter = meterProvider.getMeter( | ||
defaultInstrumentationScope.name, | ||
defaultInstrumentationScope.version, | ||
{ | ||
schemaUrl: defaultInstrumentationScope.schemaUrl, | ||
} | ||
); | ||
|
||
// Create counter with advisory attributes | ||
const counter = meter.createCounter('test_counter', { | ||
description: 'Test counter with advisory attributes', | ||
advice: { | ||
attributes: ['allowed_key1', 'allowed_key2'], // @experimental | ||
}, | ||
}); | ||
|
||
// Record measurements with various attributes | ||
counter.add(1, { | ||
allowed_key1: 'value1', | ||
allowed_key2: 'value2', | ||
filtered_key: 'filtered_value', // This should be filtered out | ||
}); | ||
|
||
counter.add(2, { | ||
allowed_key1: 'value3', | ||
another_filtered_key: 'another_filtered_value', // This should be filtered out | ||
}); | ||
|
||
// Collect metrics | ||
const metrics = await metricReader.collect(); | ||
|
||
assert.strictEqual(metrics.resourceMetrics.scopeMetrics.length, 1); | ||
|
||
const scopeMetrics = metrics.resourceMetrics.scopeMetrics[0]; | ||
assert.strictEqual(scopeMetrics.metrics.length, 1); | ||
|
||
const metric = scopeMetrics.metrics[0]; | ||
assert.strictEqual(metric.descriptor.name, 'test_counter'); | ||
assert.strictEqual(metric.dataPoints.length, 2); | ||
|
||
// Verify that only allowed attributes are present | ||
const firstDataPoint = metric.dataPoints[0]; | ||
assert.deepStrictEqual(firstDataPoint.attributes, { | ||
allowed_key1: 'value1', | ||
allowed_key2: 'value2', | ||
}); | ||
|
||
const secondDataPoint = metric.dataPoints[1]; | ||
assert.deepStrictEqual(secondDataPoint.attributes, { | ||
allowed_key1: 'value3', | ||
}); | ||
}); | ||
|
||
it('should keep all attributes when no advisory attributes are specified', async () => { | ||
const meter = meterProvider.getMeter( | ||
defaultInstrumentationScope.name, | ||
defaultInstrumentationScope.version, | ||
{ | ||
schemaUrl: defaultInstrumentationScope.schemaUrl, | ||
} | ||
); | ||
|
||
// Create counter without advisory attributes | ||
const counter = meter.createCounter('test_counter_no_filter', { | ||
description: 'Test counter without advisory attributes', | ||
}); | ||
|
||
// Record measurements with various attributes | ||
counter.add(1, { | ||
key1: 'value1', | ||
key2: 'value2', | ||
key3: 'value3', | ||
}); | ||
|
||
// Collect metrics | ||
const metrics = await metricReader.collect(); | ||
|
||
assert.strictEqual(metrics.resourceMetrics.scopeMetrics.length, 1); | ||
|
||
const scopeMetrics = metrics.resourceMetrics.scopeMetrics[0]; | ||
assert.strictEqual(scopeMetrics.metrics.length, 1); | ||
|
||
const metric = scopeMetrics.metrics[0]; | ||
assert.strictEqual(metric.descriptor.name, 'test_counter_no_filter'); | ||
assert.strictEqual(metric.dataPoints.length, 1); | ||
|
||
// Verify that all attributes are present | ||
const dataPoint = metric.dataPoints[0]; | ||
assert.deepStrictEqual(dataPoint.attributes, { | ||
key1: 'value1', | ||
key2: 'value2', | ||
key3: 'value3', | ||
}); | ||
}); | ||
|
||
it('should work with different instrument types', async () => { | ||
const meter = meterProvider.getMeter( | ||
defaultInstrumentationScope.name, | ||
defaultInstrumentationScope.version, | ||
{ | ||
schemaUrl: defaultInstrumentationScope.schemaUrl, | ||
} | ||
); | ||
|
||
// Test with Histogram | ||
const histogram = meter.createHistogram('test_histogram', { | ||
description: 'Test histogram with advisory attributes', | ||
advice: { | ||
attributes: ['service'], // @experimental | ||
}, | ||
}); | ||
|
||
histogram.record(10, { | ||
service: 'api', | ||
endpoint: '/users', // This should be filtered out | ||
}); | ||
|
||
// Test with UpDownCounter | ||
const upDownCounter = meter.createUpDownCounter('test_updown', { | ||
description: 'Test updown counter with advisory attributes', | ||
advice: { | ||
attributes: ['region'], // @experimental | ||
}, | ||
}); | ||
|
||
upDownCounter.add(5, { | ||
region: 'us-west', | ||
instance: 'i-12345', // This should be filtered out | ||
}); | ||
|
||
// Collect metrics | ||
const metrics = await metricReader.collect(); | ||
|
||
assert.strictEqual(metrics.resourceMetrics.scopeMetrics.length, 1); | ||
|
||
const scopeMetrics = metrics.resourceMetrics.scopeMetrics[0]; | ||
assert.strictEqual(scopeMetrics.metrics.length, 2); | ||
|
||
// Verify histogram filtering | ||
const histogramMetric = scopeMetrics.metrics.find((m: any) => m.descriptor.name === 'test_histogram'); | ||
assert.ok(histogramMetric); | ||
assert.deepStrictEqual(histogramMetric.dataPoints[0].attributes, { | ||
service: 'api', | ||
}); | ||
|
||
// Verify updown counter filtering | ||
const upDownMetric = scopeMetrics.metrics.find((m: any) => m.descriptor.name === 'test_updown'); | ||
assert.ok(upDownMetric); | ||
assert.deepStrictEqual(upDownMetric.dataPoints[0].attributes, { | ||
region: 'us-west', | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
My guess is that creating a view always is what's eventually failing the tests. You might have to look into which part of the code calls
findViews()
to investigate what's wrong.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.
Sure , I will look into all the issues and get back to you
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.
Hi @pichlermarc
I've addressed the aforementioned issues
Could you take a look
Thank you
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.
looks like it's still failing due to the issue I mentioned above, did you force-push over the change that addressed this? 🤔