diff --git a/docs/sources/datasources/jaeger.md b/docs/sources/datasources/jaeger.md index a740f617744..58579bb194f 100644 --- a/docs/sources/datasources/jaeger.md +++ b/docs/sources/datasources/jaeger.md @@ -40,6 +40,15 @@ This is a configuration for the [trace to logs feature]({{< relref "../explore/t ![Trace to logs settings](/static/img/docs/explore/trace-to-logs-settings-8-2.png 'Screenshot of the trace to logs settings') +### Trace to metrics + +> **Note:** This feature is behind the `traceToMetrics` feature toggle. + +To configure trace to metrics, select the target Prometheus data source and enter the desired query. + +-- **Data source -** Target data source. +-- **Query -** Query that runs when navigating from a trace to the metrics data source. + ### Node Graph This is a configuration for the beta Node Graph visualization. The Node Graph is shown after the trace view is loaded and is disabled by default. diff --git a/docs/sources/datasources/tempo.md b/docs/sources/datasources/tempo.md index be78cf56908..bc0afeb7f90 100644 --- a/docs/sources/datasources/tempo.md +++ b/docs/sources/datasources/tempo.md @@ -39,6 +39,15 @@ This is a configuration for the [trace to logs feature]({{< relref "../explore/t {{< figure src="/static/img/docs/explore/traces-to-logs-settings-8-2.png" class="docs-image--no-shadow" caption="Screenshot of the trace to logs settings" >}} +### Trace to metrics + +> **Note:** This feature is behind the `traceToMetrics` feature toggle. + +To configure trace to metrics, select the target Prometheus data source and enter the desired query. + +-- **Data source -** Target data source. +-- **Query -** Query that runs when navigating from a trace to the metrics data source. + ### Service Graph This is a configuration for the Service Graph feature. diff --git a/docs/sources/datasources/zipkin.md b/docs/sources/datasources/zipkin.md index 867952bfb91..8d9766a73c9 100644 --- a/docs/sources/datasources/zipkin.md +++ b/docs/sources/datasources/zipkin.md @@ -40,6 +40,15 @@ This is a configuration for the [trace to logs feature]({{< relref "../explore/t ![Trace to logs settings](/static/img/docs/explore/trace-to-logs-settings-8-2.png 'Screenshot of the trace to logs settings') +### Trace to metrics + +> **Note:** This feature is behind the `traceToMetrics` feature toggle. + +To configure trace to metrics, select the target Prometheus data source and enter the desired query. + +-- **Data source -** Target data source. +-- **Query -** Query that runs when navigating from a trace to the metrics data source. + ### Node Graph This is a configuration for the beta Node Graph visualization. The Node Graph is shown after the trace view is loaded and is disabled by default. diff --git a/public/app/core/components/TraceToMetrics/TraceToMetricsSettings.tsx b/public/app/core/components/TraceToMetrics/TraceToMetricsSettings.tsx index fc450315580..4a0d5733f7c 100644 --- a/public/app/core/components/TraceToMetrics/TraceToMetricsSettings.tsx +++ b/public/app/core/components/TraceToMetrics/TraceToMetricsSettings.tsx @@ -8,10 +8,11 @@ import { updateDatasourcePluginJsonDataOption, } from '@grafana/data'; import { DataSourcePicker } from '@grafana/runtime'; -import { Button, InlineField, InlineFieldRow, useStyles } from '@grafana/ui'; +import { Button, InlineField, InlineFieldRow, Input, useStyles } from '@grafana/ui'; export interface TraceToMetricsOptions { datasourceUid?: string; + query: string; } export interface TraceToMetricsData extends DataSourceJsonData { @@ -49,10 +50,10 @@ export function TraceToMetricsSettings({ options, onOptionsChange }: Props) { {options.jsonData.tracesToMetrics?.datasourceUid ? ( ) : null} + + + + { + updateDatasourcePluginJsonDataOption({ onOptionsChange, options }, 'tracesToMetrics', { + ...options.jsonData.tracesToMetrics, + query: e.currentTarget.value, + }); + }} + /> + + ); } diff --git a/public/app/features/explore/TraceView/createSpanLink.test.ts b/public/app/features/explore/TraceView/createSpanLink.test.ts index 89d2e05c16a..2145c2ee331 100644 --- a/public/app/features/explore/TraceView/createSpanLink.test.ts +++ b/public/app/features/explore/TraceView/createSpanLink.test.ts @@ -1,6 +1,7 @@ import { DataSourceInstanceSettings, MutableDataFrame } from '@grafana/data'; import { setDataSourceSrv, setTemplateSrv } from '@grafana/runtime'; import { TraceSpan } from '@jaegertracing/jaeger-ui-components'; +import { TraceToMetricsOptions } from 'app/core/components/TraceToMetrics/TraceToMetricsSettings'; import { DatasourceSrv } from 'app/features/plugins/datasource_srv'; import { TraceToLogsOptions } from '../../../core/components/TraceToLogs/TraceToLogsSettings'; @@ -399,6 +400,7 @@ describe('createSpanLinkFactory', () => { splitOpenFn, traceToMetricsOptions: { datasourceUid: 'prom1', + query: 'customQuery', }, }); expect(createLink).toBeDefined(); @@ -406,6 +408,27 @@ describe('createSpanLinkFactory', () => { const links = createLink!(createTraceSpan()); const linkDef = links?.metricLinks?.[0]; + expect(linkDef).toBeDefined(); + expect(linkDef!.href).toBe( + `/explore?left=${encodeURIComponent( + '{"range":{"from":"2020-10-14T01:00:00.000Z","to":"2020-10-14T01:00:01.000Z"},"datasource":"prom1","queries":[{"expr":"customQuery","refId":""}],"panelsState":{}}' + )}` + ); + }); + + it('uses default query if no query specified', () => { + const splitOpenFn = jest.fn(); + const createLink = createSpanLinkFactory({ + splitOpenFn, + traceToMetricsOptions: { + datasourceUid: 'prom1', + } as TraceToMetricsOptions, + }); + expect(createLink).toBeDefined(); + + const links = createLink!(createTraceSpan()); + const linkDef = links?.metricLinks?.[0]; + expect(linkDef).toBeDefined(); expect(linkDef!.href).toBe( `/explore?left=${encodeURIComponent( diff --git a/public/app/features/explore/TraceView/createSpanLink.tsx b/public/app/features/explore/TraceView/createSpanLink.tsx index 9eb6e5f075a..4681390b80b 100644 --- a/public/app/features/explore/TraceView/createSpanLink.tsx +++ b/public/app/features/explore/TraceView/createSpanLink.tsx @@ -150,6 +150,7 @@ function legacyCreateSpanLinkFactory( // Get metrics links if (metricsDataSourceSettings && traceToMetricsOptions) { + const defaultQuery = `histogram_quantile(0.5, sum(rate(tempo_spanmetrics_latency_bucket{operation="${span.operationName}"}[5m])) by (le))`; const dataLink: DataLink = { title: metricsDataSourceSettings.name, url: '', @@ -157,7 +158,7 @@ function legacyCreateSpanLinkFactory( datasourceUid: metricsDataSourceSettings.uid, datasourceName: metricsDataSourceSettings.name, query: { - expr: `histogram_quantile(0.5, sum(rate(tempo_spanmetrics_latency_bucket{operation="${span.operationName}"}[5m])) by (le))`, + expr: traceToMetricsOptions.query ?? defaultQuery, refId: '', }, },