Tempo: Escape backslash in span name for promsql query (#83024)

* Tempo: Escape backslash in span name for promsql query

In promsql, backslashes in regexes must be doubly escaped for them to be
interpreted as a literal backslash character.

Fixes #79791

* Fix regex replace

---------

Co-authored-by: Andrej Ocenas <mr.ocenas@gmail.com>
This commit is contained in:
ttshivers
2024-05-14 09:57:35 -05:00
committed by GitHub
parent 5f147644d4
commit b07b927a94
2 changed files with 5 additions and 1 deletions

View File

@@ -724,12 +724,14 @@ describe('Tempo service graph view', () => {
'/actuator/health/**', '/actuator/health/**',
'$type + [test]|HTTP POST - post', '$type + [test]|HTTP POST - post',
'server.cluster.local:9090^/sample.test(.*)?', 'server.cluster.local:9090^/sample.test(.*)?',
'test\\path',
]; ];
let escaped = getEscapedSpanNames(spanNames); let escaped = getEscapedSpanNames(spanNames);
expect(escaped).toEqual([ expect(escaped).toEqual([
'/actuator/health/\\\\*\\\\*', '/actuator/health/\\\\*\\\\*',
'\\\\$type \\\\+ \\\\[test\\\\]\\\\|HTTP POST - post', '\\\\$type \\\\+ \\\\[test\\\\]\\\\|HTTP POST - post',
'server\\\\.cluster\\\\.local:9090\\\\^/sample\\\\.test\\\\(\\\\.\\\\*\\\\)\\\\?', 'server\\\\.cluster\\\\.local:9090\\\\^/sample\\\\.test\\\\(\\\\.\\\\*\\\\)\\\\?',
'test\\\\\\\\path',
]); ]);
}); });

View File

@@ -955,8 +955,10 @@ function makePromLink(title: string, expr: string, datasourceUid: string, instan
}; };
} }
// TODO: this is basically the same as prometheus/datasource.ts#prometheusSpecialRegexEscape which is used to escape
// template variable values. It would be best to move it to some common place.
export function getEscapedSpanNames(values: string[]) { export function getEscapedSpanNames(values: string[]) {
return values.map((value: string) => value.replace(/[.*+?^${}()|[\]\\]/g, '\\\\$&')); return values.map((value: string) => value.replace(/\\/g, '\\\\\\\\').replace(/[$^*{}\[\]\'+?.()|]/g, '\\\\$&'));
} }
export function getFieldConfig( export function getFieldConfig(