mirror of https://github.com/grafana/grafana.git
Tempo: Better fallbacks for metrics query (#83619)
* Use query as fallback when there's one series and no labels. Use "" as the fallback for empty label values. Stop using the `promLabels` value from the Tempo response * Set refId to remove mentions of promLabels * Add quotes around the string value, add space after comma separator * Use name as refId when possible
This commit is contained in:
parent
9f617191da
commit
036e19037e
|
|
@ -714,7 +714,7 @@ export class TempoDatasource extends DataSourceWithBackend<TempoQuery, TempoJson
|
||||||
}).pipe(
|
}).pipe(
|
||||||
map((response) => {
|
map((response) => {
|
||||||
return {
|
return {
|
||||||
data: formatTraceQLMetrics(response.data),
|
data: formatTraceQLMetrics(queryValue, response.data),
|
||||||
};
|
};
|
||||||
}),
|
}),
|
||||||
catchError((err) => {
|
catchError((err) => {
|
||||||
|
|
|
||||||
|
|
@ -633,21 +633,31 @@ function transformToTraceData(data: TraceSearchMetadata) {
|
||||||
}
|
}
|
||||||
|
|
||||||
const metricsValueToString = (value: ProtoValue): string => {
|
const metricsValueToString = (value: ProtoValue): string => {
|
||||||
return '' + (value.stringValue || value.intValue || value.doubleValue || value.boolValue || '');
|
if (value.stringValue) {
|
||||||
|
return `"${value.stringValue}"`;
|
||||||
|
}
|
||||||
|
return '' + (value.intValue || value.doubleValue || value.boolValue || '""');
|
||||||
};
|
};
|
||||||
|
|
||||||
export function formatTraceQLMetrics(data: TraceqlMetricsResponse) {
|
export function formatTraceQLMetrics(query: string, data: TraceqlMetricsResponse) {
|
||||||
const frames = data.series.map((series) => {
|
const frames = data.series.map((series, index) => {
|
||||||
const labels: Labels = {};
|
const labels: Labels = {};
|
||||||
series.labels.forEach((label) => {
|
series.labels?.forEach((label) => {
|
||||||
labels[label.key] = metricsValueToString(label.value);
|
labels[label.key] = metricsValueToString(label.value);
|
||||||
});
|
});
|
||||||
const displayName =
|
// If it's a single series, use the query as the displayName fallback
|
||||||
series.labels.length === 1
|
let name = data.series.length === 1 ? query : '';
|
||||||
? metricsValueToString(series.labels[0].value)
|
if (series.labels) {
|
||||||
: `{${series.labels.map((label) => `${label.key}=${metricsValueToString(label.value)}`).join(',')}}`;
|
if (series.labels.length === 1) {
|
||||||
|
// For single label series, use the label value as the displayName to improve readability
|
||||||
|
name = metricsValueToString(series.labels[0].value);
|
||||||
|
} else {
|
||||||
|
// otherwise build a string using the label keys and values
|
||||||
|
name = `{${series.labels.map((label) => `${label.key}=${metricsValueToString(label.value)}`).join(', ')}}`;
|
||||||
|
}
|
||||||
|
}
|
||||||
return createDataFrame({
|
return createDataFrame({
|
||||||
refId: series.promLabels,
|
refId: name || `A${index}`,
|
||||||
fields: [
|
fields: [
|
||||||
{
|
{
|
||||||
name: 'time',
|
name: 'time',
|
||||||
|
|
@ -655,12 +665,12 @@ export function formatTraceQLMetrics(data: TraceqlMetricsResponse) {
|
||||||
values: series.samples.map((sample) => parseInt(sample.timestampMs, 10)),
|
values: series.samples.map((sample) => parseInt(sample.timestampMs, 10)),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: series.promLabels,
|
name: name,
|
||||||
labels,
|
labels,
|
||||||
type: FieldType.number,
|
type: FieldType.number,
|
||||||
values: series.samples.map((sample) => sample.value),
|
values: series.samples.map((sample) => sample.value),
|
||||||
config: {
|
config: {
|
||||||
displayNameFromDS: displayName,
|
displayNameFromDS: name,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue