|
| 1 | +/** |
| 2 | + * MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2020-present, Elastic NV |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in |
| 14 | + * all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | + * THE SOFTWARE. |
| 23 | + * |
| 24 | + */ |
| 25 | + |
| 26 | +import { HrTime, metrics, SpanStatusCode } from '@opentelemetry/api'; |
| 27 | +import { AttributeNames, DurationSpanProcessor } from '../../src/otel'; |
| 28 | +import { ReadableSpan } from '@opentelemetry/sdk-trace-base'; |
| 29 | +import { |
| 30 | + ATTR_URL_FULL, |
| 31 | + METRIC_HTTP_CLIENT_REQUEST_DURATION, |
| 32 | +} from '@opentelemetry/semantic-conventions'; |
| 33 | + |
| 34 | +const commonAttributes = { |
| 35 | + [AttributeNames.JOURNEY_ID]: 'journey-id', |
| 36 | + [AttributeNames.JOURNEY_NAME]: 'journey name', |
| 37 | + [AttributeNames.STEP_NAME]: 'step name', |
| 38 | +}; |
| 39 | +const createSpan = ( |
| 40 | + attributes: Record<string, any> = {}, |
| 41 | + duration: HrTime = [0, 0.1 * 1e9], |
| 42 | + code: SpanStatusCode = SpanStatusCode.UNSET |
| 43 | +): ReadableSpan => { |
| 44 | + const span: Partial<ReadableSpan> = { |
| 45 | + duration, |
| 46 | + status: { code }, |
| 47 | + attributes: { |
| 48 | + ...commonAttributes, |
| 49 | + ...attributes, |
| 50 | + 'extra.attribute': 'extra attribute value', |
| 51 | + }, |
| 52 | + }; |
| 53 | + return span as ReadableSpan; |
| 54 | +}; |
| 55 | + |
| 56 | +describe('DurationSpanProcessor', () => { |
| 57 | + let records: [number, Record<string, any>][]; |
| 58 | + const mockedRecord = (duration: number, attributes: Record<string, any>) => { |
| 59 | + records.push([duration, attributes]); |
| 60 | + }; |
| 61 | + const mockedCreateGauge = jest.fn().mockReturnValue({ record: mockedRecord }); |
| 62 | + const mockedCreateHistogram = jest |
| 63 | + .fn() |
| 64 | + .mockReturnValue({ record: mockedRecord }); |
| 65 | + |
| 66 | + jest.spyOn(metrics, 'getMeter').mockImplementation( |
| 67 | + () => |
| 68 | + ({ |
| 69 | + createGauge: mockedCreateGauge, |
| 70 | + createHistogram: mockedCreateHistogram, |
| 71 | + } as any) |
| 72 | + ); |
| 73 | + |
| 74 | + beforeEach(() => { |
| 75 | + records = []; |
| 76 | + }); |
| 77 | + |
| 78 | + it('records gauge metrics for journeys and steps', () => { |
| 79 | + const spans: ReadableSpan[] = [ |
| 80 | + createSpan({ [AttributeNames.SPAN_SUBTYPE]: 'journey' }, [0, 0.5 * 1e9]), |
| 81 | + createSpan({ [AttributeNames.SPAN_SUBTYPE]: 'step' }), |
| 82 | + createSpan( |
| 83 | + { [AttributeNames.SPAN_SUBTYPE]: 'step' }, |
| 84 | + [1, 0], |
| 85 | + SpanStatusCode.ERROR |
| 86 | + ), |
| 87 | + createSpan({ [AttributeNames.SPAN_SUBTYPE]: 'other' }), |
| 88 | + ]; |
| 89 | + const spanProcessor = new DurationSpanProcessor(); |
| 90 | + |
| 91 | + spans.forEach(span => spanProcessor.onEnd(span)); |
| 92 | + |
| 93 | + expect(mockedCreateGauge).toBeCalledWith('synthetics.duration', { |
| 94 | + description: 'Duration in seconds', |
| 95 | + unit: 's', |
| 96 | + }); |
| 97 | + expect(mockedCreateHistogram).not.toBeCalled(); |
| 98 | + expect(records).toEqual([ |
| 99 | + [ |
| 100 | + 0.5, |
| 101 | + { |
| 102 | + ...commonAttributes, |
| 103 | + 'synthetics.status': 'success', |
| 104 | + 'synthetics.type': 'journey', |
| 105 | + }, |
| 106 | + ], |
| 107 | + [ |
| 108 | + 0.1, |
| 109 | + { |
| 110 | + ...commonAttributes, |
| 111 | + 'synthetics.status': 'success', |
| 112 | + 'synthetics.type': 'step', |
| 113 | + }, |
| 114 | + ], |
| 115 | + [ |
| 116 | + 1.0, |
| 117 | + { |
| 118 | + ...commonAttributes, |
| 119 | + 'synthetics.status': 'error', |
| 120 | + 'synthetics.type': 'step', |
| 121 | + }, |
| 122 | + ], |
| 123 | + ]); |
| 124 | + }); |
| 125 | + |
| 126 | + it('records histogram metrics for requests', () => { |
| 127 | + const spans: ReadableSpan[] = [ |
| 128 | + createSpan({ [ATTR_URL_FULL]: 'https://example.com/first' }, [ |
| 129 | + 0, |
| 130 | + 0.5 * 1e9, |
| 131 | + ]), |
| 132 | + createSpan({ |
| 133 | + [ATTR_URL_FULL]: 'https://example.com/second', |
| 134 | + }), |
| 135 | + createSpan({ |
| 136 | + 'span-without-url': 'value', |
| 137 | + }), |
| 138 | + ]; |
| 139 | + const spanProcessor = new DurationSpanProcessor(); |
| 140 | + |
| 141 | + spans.forEach(span => spanProcessor.onEnd(span)); |
| 142 | + |
| 143 | + expect(mockedCreateGauge).not.toBeCalled(); |
| 144 | + expect(mockedCreateHistogram).toBeCalledWith( |
| 145 | + METRIC_HTTP_CLIENT_REQUEST_DURATION, |
| 146 | + { |
| 147 | + description: 'Duration in seconds for HTTP server spans', |
| 148 | + unit: 's', |
| 149 | + } |
| 150 | + ); |
| 151 | + expect(records).toEqual([ |
| 152 | + [0.5, commonAttributes], |
| 153 | + [0.1, commonAttributes], |
| 154 | + ]); |
| 155 | + }); |
| 156 | +}); |
0 commit comments