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

Chore: Migrate #99348 from grafana/grafana #52

Merged
merged 1 commit into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/helpers/MetricDataSourceHelper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,21 @@ describe('MetricDatasourceHelper', () => {
const result = await metricDatasourceHelper.isNativeHistogram('non_histogram_metric');
expect(result).toBe(false);
});

it('should return false if metric is a classic histogram', async () => {
const result = await metricDatasourceHelper.isNativeHistogram('test_metric_bucket');
expect(result).toBe(false);
});

it('should return true if metric is a native histogram and has metadata but does not have a classic histogram to compare to', async () => {
metricDatasourceHelper._metricsMetadata = {
solo_native_histogram: {
type: 'histogram',
help: 'test',
},
};
const result = await metricDatasourceHelper.isNativeHistogram('solo_native_histogram');
expect(result).toBe(true);
});
});
});
35 changes: 29 additions & 6 deletions src/helpers/MetricDatasourceHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ export class MetricDatasourceHelper {
return this._datasource;
}

_metricsMetadata?: Promise<PromMetricsMetadata | undefined>;
// store metadata in a more easily accessible form
_metricsMetadata?: PromMetricsMetadata | undefined;

private async _getMetricsMetadata() {
const ds = await this.getDatasource();
Expand All @@ -77,7 +78,7 @@ export class MetricDatasourceHelper {
return undefined;
}
if (!this._metricsMetadata) {
this._metricsMetadata = this._getMetricsMetadata();
this._metricsMetadata = await this._getMetricsMetadata();
}

const metadata = await this._metricsMetadata;
Expand All @@ -90,9 +91,12 @@ export class MetricDatasourceHelper {
public listNativeHistograms() {
return this._nativeHistograms;
}

/**
* Identify native histograms by querying classic histograms and all metrics,
* Identify native histograms by 2 strategies.
* 1. querying classic histograms and all metrics,
* then comparing the results and build the collection of native histograms.
* 2. querying all metrics and checking if the metric is a histogram type and dies not have the bucket suffix.
*
* classic histogram = test_metric_bucket
* native histogram = test_metric
Expand All @@ -109,6 +113,11 @@ export class MetricDatasourceHelper {
this._classicHistograms[m.text] = 1;
});

if (!this._metricsMetadata && !ds.languageProvider.metricsMetadata) {
await ds.languageProvider.loadMetricsMetadata();
this._metricsMetadata = ds.languageProvider.metricsMetadata;
}

Comment on lines +116 to +120
Copy link
Contributor Author

@yangkb09 yangkb09 Jan 30, 2025

Choose a reason for hiding this comment

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

i removed the wrapper

if (ds.languageProvider instanceof PromQlLanguageProvider) {

because we're already checking if (ds && above on 106

allMetrics.forEach((m) => {
if (this.isNativeHistogram(m.text)) {
// Build the collection of native histograms.
Expand All @@ -119,19 +128,33 @@ export class MetricDatasourceHelper {
}

/**
*
* If a metric name + _bucket exists in the classic histograms, then it is a native histogram
* Identify native histograms by 2 strategies.
* 1. querying classic histograms and all metrics,
* then comparing the results and build the collection of native histograms.
* 2. querying all metrics and checking if the metric is a histogram type and dies not have the bucket suffix.
*
* classic histogram = test_metric_bucket
* native histogram = test_metric
*
* @param metric
* @returns
* @returns boolean
*/
public isNativeHistogram(metric: string): boolean {
if (!metric) {
return false;
}

// check when fully migrated, we only have metadata, and there are no more classic histograms
const metadata = this._metricsMetadata;
// suffix is not 'bucket' and type is histogram
const suffix: string = metric.split('_').pop() ?? '';
// the string is not equal to bucket
const notClassic = suffix !== 'bucket';
if (metadata?.[metric]?.type === 'histogram' && notClassic) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

return true;
}

// check for comparison when there is overlap between native and classic histograms
if (this._classicHistograms[`${metric}_bucket`]) {
return true;
}
Expand Down
Loading