SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.
TypeScript apmapplication-monitoringdistributed-tracinggogood-first-issuejaegerloglogsmetricsmonitoringnextjsobservabilityopen-sourceopentelemetryprometheusreactreactjsself-hostedtracingtypescript
fix(dashboard): substitute per-row groupBy values in context-link URLs (#12328)
Abhi kumar98030c29efaf
diff --git a/frontend/src/container/NewWidget/RightContainer/ContextLinks/__test__/utils.test.ts b/frontend/src/container/NewWidget/RightContainer/ContextLinks/__test__/utils.test.tsnew file mode 100644index 0000000000..100e21b40e--- /dev/null+++ b/frontend/src/container/NewWidget/RightContainer/ContextLinks/__test__/utils.test.ts@@ -0,0 +1,40 @@+import { processContextLinks } from '../utils';++describe('processContextLinks', () => {+ // Regression for #11325.+ it('substitutes per-row groupBy values in the path and query params', () => {+ const [link] = processContextLinks(+ [+ {+ id: '1',+ label: 'Open trace {{trace_id}}',+ url: '/trace/{{trace_id}}?spanId={{span_id}}&levelUp=0&levelDown=0',+ },+ ],+ { _trace_id: 'abc123', _span_id: 'def456' },+ );++ expect(link.url).toBe('/trace/abc123?spanId=def456&levelUp=0&levelDown=0');+ expect(link.label).toBe('Open trace abc123');+ });++ it('resolves dashboard and global variables alongside row fields', () => {+ const [link] = processContextLinks(+ [+ { id: '1', label: 'Logs', url: '/logs/{{service}}?ts={{timestamp_start}}' },+ ],+ { service: 'frontend', timestamp_start: '1720512000000', _service: 'redis' },+ );++ expect(link.url).toBe('/logs/frontend?ts=1720512000000');+ });++ it('leaves an unresolvable token in place', () => {+ const [link] = processContextLinks(+ [{ id: '1', label: 'Open', url: '/trace/{{trace_id}}' }],+ {},+ );++ expect(link.url).toBe('/trace/{{trace_id}}');+ });+});diff --git a/frontend/src/hooks/dashboard/__test__/useContextVariables.test.ts b/frontend/src/hooks/dashboard/__test__/useContextVariables.test.tsnew file mode 100644index 0000000000..b1f9adc949--- /dev/null+++ b/frontend/src/hooks/dashboard/__test__/useContextVariables.test.ts@@ -0,0 +1,61 @@+import { resolveTexts } from '../useContextVariables';++const ROW_VARIABLES = { _trace_id: 'abc123', _span_id: 'def456' };++const resolveOne = (+ text: string,+ processedVariables: Record<string, string>,+): string => resolveTexts({ texts: [text], processedVariables }).fullTexts[0];++describe('resolveTexts', () => {+ it('resolves bare {{field}} placeholders from per-row field variables', () => {+ expect(+ resolveOne('/trace/{{trace_id}}?spanId={{span_id}}', ROW_VARIABLES),+ ).toBe('/trace/abc123?spanId=def456');+ });++ it('still resolves the explicitly prefixed {{_field}} form', () => {+ expect(resolveOne('/trace/{{_trace_id}}', ROW_VARIABLES)).toBe(+ '/trace/abc123',+ );+ });++ it.each([+ ['{{.trace_id}}', '/trace/{{.trace_id}}'],+ ['[[trace_id]]', '/trace/[[trace_id]]'],+ ['$trace_id', '/trace/$trace_id'],+ ])('resolves the %s placeholder syntax', (_syntax, text) => {+ expect(resolveOne(text, ROW_VARIABLES)).toBe('/trace/abc123');+ });++ it('resolves a field name containing dots', () => {+ expect(+ resolveOne('/svc/{{service.name}}', { '_service.name': 'redis' }),+ ).toBe('/svc/redis');+ });++ it('gives a dashboard variable precedence over a same-named row field', () => {+ expect(+ resolveOne('/svc/{{service}}', {+ service: 'from-dashboard',+ _service: 'from-row',+ }),+ ).toBe('/svc/from-dashboard');+ });++ it('leaves an unknown placeholder untouched', () => {+ expect(resolveOne('/trace/{{unknown}}', ROW_VARIABLES)).toBe(+ '/trace/{{unknown}}',+ );+ });++ it('picks the truncated side of a multi-value field for truncatedTexts', () => {+ const { fullTexts, truncatedTexts } = resolveTexts({+ texts: ['services: {{service}}'],+ processedVariables: { _service: 'a, b +1-|-a, b, c' },+ });++ expect(fullTexts[0]).toBe('services: a, b, c');+ expect(truncatedTexts[0]).toBe('services: a, b +1');+ });+});diff --git a/frontend/src/hooks/dashboard/useContextVariables.tsx b/frontend/src/hooks/dashboard/useContextVariables.tsxindex 71a92250e3..1b23ead766 100644--- a/frontend/src/hooks/dashboard/useContextVariables.tsx+++ b/frontend/src/hooks/dashboard/useContextVariables.tsx@@ -223,6 +223,14 @@ const extractVarName = (return match;};+// Per-row fields are registered `_`-prefixed, but templates use the bare name (`{{trace_id}}`).+// Exact match first so dashboard/global variables keep precedence over a same-named row field.+const lookupVariableValue = (+ varName: string,+ processedVariables: Record<string, string>,+): string | undefined =>+ processedVariables[varName] ?? processedVariables[`_${varName}`];+// Utility function to resolve text with processed variablesconst resolveText = (text: string,@@ -233,7 +241,7 @@ const resolveText = (return text.replace(combinedPattern, (match) => {const varName = extractVarName(match, matcher, processedVariables);- const value = processedVariables[varName];+ const value = lookupVariableValue(varName, processedVariables);if (value != null) {const parts = value.split('-|-');@@ -254,7 +262,7 @@ const resolveTextWithTruncation = (const result = text.replace(combinedPattern, (match) => {const varName = extractVarName(match, matcher, processedVariables);- const value = processedVariables[varName];+ const value = lookupVariableValue(varName, processedVariables);if (value != null) {const parts = value.split('-|-');