123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589 |
- 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';
- import { LinkSrv, setLinkSrv } from '../../panel/panellinks/link_srv';
- import { TemplateSrv } from '../../templating/template_srv';
- import { createSpanLinkFactory } from './createSpanLink';
- describe('createSpanLinkFactory', () => {
- it('returns no links if there is no data source uid', () => {
- const splitOpenFn = jest.fn();
- const createLink = createSpanLinkFactory({ splitOpenFn: splitOpenFn });
- const links = createLink!(createTraceSpan());
- expect(links?.logLinks).toBeUndefined();
- expect(links?.metricLinks).toBeUndefined();
- expect(links?.traceLinks).toHaveLength(0);
- });
- describe('should return loki link', () => {
- beforeAll(() => {
- setDataSourceSrv({
- getInstanceSettings(uid: string): DataSourceInstanceSettings | undefined {
- return { uid: 'loki1', name: 'loki1', type: 'loki' } as any;
- },
- } as any);
- setLinkSrv(new LinkSrv());
- setTemplateSrv(new TemplateSrv());
- });
- it('with default keys when tags not configured', () => {
- const createLink = setupSpanLinkFactory();
- expect(createLink).toBeDefined();
- const links = createLink!(createTraceSpan());
- const linkDef = links?.logLinks?.[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":"loki1","queries":[{"expr":"{cluster=\\"cluster1\\", hostname=\\"hostname1\\"}","refId":""}],"panelsState":{}}'
- )}`
- );
- });
- it('with tags that passed in and without tags that are not in the span', () => {
- const createLink = setupSpanLinkFactory({
- tags: ['ip', 'newTag'],
- });
- expect(createLink).toBeDefined();
- const links = createLink!(
- createTraceSpan({
- process: {
- serviceName: 'service',
- tags: [
- { key: 'hostname', value: 'hostname1' },
- { key: 'ip', value: '192.168.0.1' },
- ],
- },
- })
- );
- const linkDef = links?.logLinks?.[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":"loki1","queries":[{"expr":"{ip=\\"192.168.0.1\\"}","refId":""}],"panelsState":{}}'
- )}`
- );
- });
- it('from tags and process tags as well', () => {
- const createLink = setupSpanLinkFactory({
- tags: ['ip', 'host'],
- });
- expect(createLink).toBeDefined();
- const links = createLink!(
- createTraceSpan({
- process: {
- serviceName: 'service',
- tags: [
- { key: 'hostname', value: 'hostname1' },
- { key: 'ip', value: '192.168.0.1' },
- ],
- },
- })
- );
- const linkDef = links?.logLinks?.[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":"loki1","queries":[{"expr":"{ip=\\"192.168.0.1\\", host=\\"host\\"}","refId":""}],"panelsState":{}}'
- )}`
- );
- });
- it('with adjusted start and end time', () => {
- const createLink = setupSpanLinkFactory({
- spanStartTimeShift: '1m',
- spanEndTimeShift: '1m',
- });
- expect(createLink).toBeDefined();
- const links = createLink!(
- createTraceSpan({
- process: {
- serviceName: 'service',
- tags: [
- { key: 'hostname', value: 'hostname1' },
- { key: 'ip', value: '192.168.0.1' },
- ],
- },
- })
- );
- const linkDef = links?.logLinks?.[0];
- expect(linkDef).toBeDefined();
- expect(linkDef!.href).toBe(
- `/explore?left=${encodeURIComponent(
- '{"range":{"from":"2020-10-14T01:01:00.000Z","to":"2020-10-14T01:01:01.000Z"},"datasource":"loki1","queries":[{"expr":"{hostname=\\"hostname1\\"}","refId":""}],"panelsState":{}}'
- )}`
- );
- });
- it('filters by trace and span ID', () => {
- const createLink = setupSpanLinkFactory({
- filterBySpanID: true,
- filterByTraceID: true,
- });
- expect(createLink).toBeDefined();
- const links = createLink!(createTraceSpan());
- const linkDef = links?.logLinks?.[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":"loki1","queries":[{"expr":"{cluster=\\"cluster1\\", hostname=\\"hostname1\\"} |=\\"7946b05c2e2e4e5a\\" |=\\"6605c7b08e715d6c\\"","refId":""}],"panelsState":{}}'
- )}`
- );
- });
- it('creates link from dataFrame', () => {
- const splitOpenFn = jest.fn();
- const createLink = createSpanLinkFactory({
- splitOpenFn,
- dataFrame: new MutableDataFrame({
- fields: [
- { name: 'traceID', values: ['testTraceId'] },
- {
- name: 'spanID',
- config: { links: [{ title: 'link', url: '${__data.fields.spanID}' }] },
- values: ['testSpanId'],
- },
- ],
- }),
- });
- expect(createLink).toBeDefined();
- const links = createLink!(createTraceSpan());
- const linkDef = links?.logLinks?.[0];
- expect(linkDef).toBeDefined();
- expect(linkDef!.href).toBe('testSpanId');
- });
- it('handles renamed tags', () => {
- const createLink = setupSpanLinkFactory({
- mapTagNamesEnabled: true,
- mappedTags: [
- { key: 'service.name', value: 'service' },
- { key: 'k8s.pod.name', value: 'pod' },
- ],
- });
- expect(createLink).toBeDefined();
- const links = createLink!(
- createTraceSpan({
- process: {
- serviceName: 'service',
- tags: [
- { key: 'service.name', value: 'serviceName' },
- { key: 'k8s.pod.name', value: 'podName' },
- ],
- },
- })
- );
- const linkDef = links?.logLinks?.[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":"loki1","queries":[{"expr":"{service=\\"serviceName\\", pod=\\"podName\\"}","refId":""}],"panelsState":{}}'
- )}`
- );
- });
- it('handles incomplete renamed tags', () => {
- const createLink = setupSpanLinkFactory({
- mapTagNamesEnabled: true,
- mappedTags: [
- { key: 'service.name', value: '' },
- { key: 'k8s.pod.name', value: 'pod' },
- ],
- });
- expect(createLink).toBeDefined();
- const links = createLink!(
- createTraceSpan({
- process: {
- serviceName: 'service',
- tags: [
- { key: 'service.name', value: 'serviceName' },
- { key: 'k8s.pod.name', value: 'podName' },
- ],
- },
- })
- );
- const linkDef = links?.logLinks?.[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":"loki1","queries":[{"expr":"{service.name=\\"serviceName\\", pod=\\"podName\\"}","refId":""}],"panelsState":{}}'
- )}`
- );
- });
- it('handles empty queries', () => {
- const createLink = setupSpanLinkFactory({
- tags: [],
- });
- expect(createLink).toBeDefined();
- const links = createLink!(
- createTraceSpan({
- process: {
- serviceName: 'service',
- tags: [
- { key: 'service.name', value: 'serviceName' },
- { key: 'k8s.pod.name', value: 'podName' },
- ],
- },
- })
- );
- expect(links?.logLinks).toBeUndefined();
- });
- });
- describe('should return splunk link', () => {
- const splunkUID = 'splunkUID';
- beforeAll(() => {
- setDataSourceSrv({
- getInstanceSettings(uid: string): DataSourceInstanceSettings | undefined {
- return { uid: splunkUID, name: 'Splunk 8', type: 'grafana-splunk-datasource' } as any;
- },
- } as any);
- setLinkSrv(new LinkSrv());
- setTemplateSrv(new TemplateSrv());
- });
- it('the `query` keyword is used in the link rather than `expr` that loki uses', () => {
- const createLink = setupSpanLinkFactory({
- datasourceUid: splunkUID,
- });
- const links = createLink!(createTraceSpan());
- const linkDef = links?.logLinks?.[0];
- expect(linkDef).toBeDefined();
- expect(linkDef!.href).toContain(`${encodeURIComponent('datasource":"Splunk 8","queries":[{"query"')}`);
- expect(linkDef!.href).not.toContain(`${encodeURIComponent('datasource":"Splunk 8","queries":[{"expr"')}`);
- });
- it('automatically timeshifts the timerange by one second in a splunk query', () => {
- const createLink = setupSpanLinkFactory({
- datasourceUid: splunkUID,
- });
- const links = createLink!(createTraceSpan());
- const linkDef = links?.logLinks?.[0];
- expect(linkDef).toBeDefined();
- expect(linkDef!.href).toContain(
- `${encodeURIComponent('{"range":{"from":"2020-10-14T01:00:00.000Z","to":"2020-10-14T01:00:01.000Z"}')}`
- );
- expect(linkDef!.href).not.toContain(
- `${encodeURIComponent('{"range":{"from":"2020-10-14T01:00:00.000Z","to":"2020-10-14T01:00:00.000Z"}')}`
- );
- });
- it('formats query correctly if filterByTraceID and or filterBySpanID is true', () => {
- const createLink = setupSpanLinkFactory({
- datasourceUid: splunkUID,
- filterByTraceID: true,
- filterBySpanID: true,
- });
- expect(createLink).toBeDefined();
- const links = createLink!(createTraceSpan());
- const linkDef = links?.logLinks?.[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":"Splunk 8","queries":[{"query":"cluster=\\"cluster1\\" hostname=\\"hostname1\\" \\"7946b05c2e2e4e5a\\" \\"6605c7b08e715d6c\\"","refId":""}],"panelsState":{}}'
- )}`
- );
- });
- it('should format one tag correctly', () => {
- const createLink = setupSpanLinkFactory({
- tags: ['ip'],
- });
- expect(createLink).toBeDefined();
- const links = createLink!(
- createTraceSpan({
- process: {
- serviceName: 'service',
- tags: [{ key: 'ip', value: '192.168.0.1' }],
- },
- })
- );
- const linkDef = links?.logLinks?.[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":"Splunk 8","queries":[{"query":"ip=\\"192.168.0.1\\"","refId":""}],"panelsState":{}}'
- )}`
- );
- });
- it('should format multiple tags correctly', () => {
- const createLink = setupSpanLinkFactory({
- tags: ['ip', 'hostname'],
- });
- expect(createLink).toBeDefined();
- const links = createLink!(
- createTraceSpan({
- process: {
- serviceName: 'service',
- tags: [
- { key: 'hostname', value: 'hostname1' },
- { key: 'ip', value: '192.168.0.1' },
- ],
- },
- })
- );
- const linkDef = links?.logLinks?.[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":"Splunk 8","queries":[{"query":"hostname=\\"hostname1\\" ip=\\"192.168.0.1\\"","refId":""}],"panelsState":{}}'
- )}`
- );
- });
- it('handles renamed tags', () => {
- const createLink = setupSpanLinkFactory({
- mapTagNamesEnabled: true,
- mappedTags: [
- { key: 'service.name', value: 'service' },
- { key: 'k8s.pod.name', value: 'pod' },
- ],
- });
- expect(createLink).toBeDefined();
- const links = createLink!(
- createTraceSpan({
- process: {
- serviceName: 'service',
- tags: [
- { key: 'service.name', value: 'serviceName' },
- { key: 'k8s.pod.name', value: 'podName' },
- ],
- },
- })
- );
- const linkDef = links?.logLinks?.[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":"Splunk 8","queries":[{"query":"service=\\"serviceName\\" pod=\\"podName\\"","refId":""}],"panelsState":{}}'
- )}`
- );
- });
- });
- describe('should return metric link', () => {
- beforeAll(() => {
- setDataSourceSrv({
- getInstanceSettings(uid: string): DataSourceInstanceSettings | undefined {
- return { uid: 'prom1', name: 'prom1', type: 'prometheus' } as any;
- },
- } as any);
- setLinkSrv(new LinkSrv());
- setTemplateSrv(new TemplateSrv());
- });
- it('returns single query with span', () => {
- const splitOpenFn = jest.fn();
- const createLink = createSpanLinkFactory({
- splitOpenFn,
- traceToMetricsOptions: {
- datasourceUid: 'prom1',
- queries: [{ query: 'customQuery' }],
- },
- });
- expect(createLink).toBeDefined();
- 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":"A"}],"panelsState":{}}'
- )}`
- );
- });
- it('returns nothing if no queries specified', () => {
- const splitOpenFn = jest.fn();
- const createLink = createSpanLinkFactory({
- splitOpenFn,
- traceToMetricsOptions: {
- datasourceUid: 'prom1',
- } as TraceToMetricsOptions,
- });
- expect(createLink).toBeDefined();
- const links = createLink!(createTraceSpan());
- expect(links?.metricLinks).toBeUndefined();
- });
- it('returns multiple queries including default', () => {
- const splitOpenFn = jest.fn();
- const createLink = createSpanLinkFactory({
- splitOpenFn,
- traceToMetricsOptions: {
- datasourceUid: 'prom1',
- queries: [
- { name: 'Named Query', query: 'customQuery' },
- { name: 'defaultQuery', query: '' },
- { query: 'no_name_here' },
- ],
- } as TraceToMetricsOptions,
- });
- expect(createLink).toBeDefined();
- const links = createLink!(createTraceSpan());
- expect(links?.metricLinks).toBeDefined();
- expect(links?.metricLinks).toHaveLength(3);
- const namedLink = links?.metricLinks?.[0];
- expect(namedLink).toBeDefined();
- expect(namedLink!.title).toBe('Named Query');
- expect(namedLink!.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":"A"}],"panelsState":{}}'
- )}`
- );
- const defaultLink = links?.metricLinks?.[1];
- expect(defaultLink).toBeDefined();
- expect(defaultLink!.title).toBe('defaultQuery');
- expect(defaultLink!.href).toBe(
- `/explore?left=${encodeURIComponent(
- '{"range":{"from":"2020-10-14T01:00:00.000Z","to":"2020-10-14T01:00:01.000Z"},"datasource":"prom1","queries":[{"expr":"histogram_quantile(0.5, sum(rate(tempo_spanmetrics_latency_bucket{operation=\\"operation\\"}[5m])) by (le))","refId":"A"}],"panelsState":{}}'
- )}`
- );
- const unnamedQuery = links?.metricLinks?.[2];
- expect(unnamedQuery).toBeDefined();
- expect(unnamedQuery!.title).toBeUndefined();
- expect(unnamedQuery!.href).toBe(
- `/explore?left=${encodeURIComponent(
- '{"range":{"from":"2020-10-14T01:00:00.000Z","to":"2020-10-14T01:00:01.000Z"},"datasource":"prom1","queries":[{"expr":"no_name_here","refId":"A"}],"panelsState":{}}'
- )}`
- );
- });
- });
- describe('should return span links', () => {
- beforeAll(() => {
- setDataSourceSrv(new DatasourceSrv());
- setLinkSrv(new LinkSrv());
- setTemplateSrv(new TemplateSrv());
- });
- it('ignores parent span link', () => {
- const createLink = setupSpanLinkFactory();
- expect(createLink).toBeDefined();
- const links = createLink!(
- createTraceSpan({ references: [{ refType: 'CHILD_OF', spanID: 'parent', traceID: 'traceID' }] })
- );
- const traceLinks = links?.traceLinks;
- expect(traceLinks).toBeDefined();
- expect(traceLinks).toHaveLength(0);
- });
- it('returns links for references and subsidiarilyReferencedBy references', () => {
- const createLink = setupSpanLinkFactory();
- expect(createLink).toBeDefined();
- const links = createLink!(
- createTraceSpan({
- references: [
- {
- refType: 'FOLLOWS_FROM',
- spanID: 'span1',
- traceID: 'traceID',
- span: { operationName: 'SpanName' } as any,
- },
- ],
- subsidiarilyReferencedBy: [{ refType: 'FOLLOWS_FROM', spanID: 'span3', traceID: 'traceID2' }],
- })
- );
- const traceLinks = links?.traceLinks;
- expect(traceLinks).toBeDefined();
- expect(traceLinks).toHaveLength(2);
- expect(traceLinks![0]).toEqual(
- expect.objectContaining({
- href: 'traceID-span1',
- title: 'SpanName',
- })
- );
- expect(traceLinks![1]).toEqual(
- expect.objectContaining({
- href: 'traceID2-span3',
- title: 'View linked span',
- })
- );
- });
- });
- });
- function setupSpanLinkFactory(options: Partial<TraceToLogsOptions> = {}, datasourceUid = 'lokiUid') {
- const splitOpenFn = jest.fn();
- return createSpanLinkFactory({
- splitOpenFn,
- traceToLogsOptions: {
- datasourceUid,
- ...options,
- },
- createFocusSpanLink: (traceId, spanId) => {
- return {
- href: `${traceId}-${spanId}`,
- } as any;
- },
- });
- }
- function createTraceSpan(overrides: Partial<TraceSpan> = {}): TraceSpan {
- return {
- spanID: '6605c7b08e715d6c',
- traceID: '7946b05c2e2e4e5a',
- processID: 'processId',
- operationName: 'operation',
- logs: [],
- startTime: new Date('2020-10-14T01:00:00Z').valueOf() * 1000,
- duration: 1000 * 1000,
- flags: 0,
- hasChildren: false,
- dataFrameRowIndex: 0,
- tags: [
- {
- key: 'host',
- value: 'host',
- },
- ],
- process: {
- serviceName: 'test service',
- tags: [
- {
- key: 'cluster',
- value: 'cluster1',
- },
- {
- key: 'hostname',
- value: 'hostname1',
- },
- {
- key: 'label2',
- value: 'val2',
- },
- ],
- },
- ...overrides,
- } as any;
- }
|