Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: http endpoint filtering according to design review #248

Merged
286 changes: 107 additions & 179 deletions src/samplers/lumigoSampler.test.js
Original file line number Diff line number Diff line change
@@ -1,217 +1,145 @@
import { extractUrl, shouldSkipSpanOnRouteMatch } from './lumigoSampler';
import { doesEndpointMatchRegexes, extractEndpoint, parseStringToArray } from './lumigoSampler';
import { SpanKind } from '@opentelemetry/api';

describe('lumigo sampler', () => {
afterEach(() => {
jest.clearAllMocks();
});

[
// Test happy flow - regex matches url
{
url: 'https://example.com',
regex: '.*example.*',
shouldSkip: true,
rawArrayString: '["a"]',
expectedArray: ['a'],
},
{
url: 'https://example.com',
regex: 'https://example.com',
shouldSkip: true,
rawArrayString: '["a", "b"]',
expectedArray: ['a', 'b'],
},
{
url: 'https://example.com/about',
regex: 'https://example.com/about',
shouldSkip: true,
rawArrayString: '[]',
expectedArray: [],
},
{
url: 'https://example.com/about',
regex: 'https?://.+/about',
shouldSkip: true,
rawArrayString: '"a","b"',
expectedArray: [],
},

// Test url doesn't match regex
{
url: 'https://example.com',
regex: '.*not-example.*',
shouldSkip: false,
rawArrayString: 'Not an array',
expectedArray: [],
},
{
url: 'https://example.com',
regex: 'https://not-example.com',
shouldSkip: false,
rawArrayString: null,
expectedArray: [],
},
{
url: 'https://example.com/about',
regex: 'https://example.com/not-about',
shouldSkip: false,
rawArrayString: undefined,
expectedArray: [],
},
{
rawArrayString: '["a", 2]',
expectedArray: [],
},

// Test regex is invalid
{
url: 'https://example.com',
// The regex is invalid because a char must come before the * operator
regex: '*',
shouldSkip: false,
rawArrayString: '["a", true]',
expectedArray: [],
},
].map(({ rawArrayString, expectedArray }) => {
test(`test parse array string - ${rawArrayString}`, () => {
sagivoululumigo marked this conversation as resolved.
Show resolved Hide resolved
expect(parseStringToArray(rawArrayString)).toEqual(expectedArray);
});
});

// Test regex is undefined
[
{
endpoint: 'https://example.com',
regexes: ['.*example.*'],
shouldMatch: true,
},
{
endpoint: '/orders/123',
regexes: ['.*orders.*'],
shouldMatch: true,
},
{
endpoint: '/orders/123',
regexes: ['.*will-not-match.*', '.*orders.*'],
shouldMatch: true,
},
{
endpoint: '/orders/123',
regexes: [],
shouldMatch: false,
},
{
url: 'https://example.com',
regex: undefined,
shouldSkip: false,
endpoint: '/orders/123',
regexes: ['no-match-1', 'no-match-2'],
shouldMatch: false,
},
{
url: null,
regex: '.*',
shouldSkip: false,
endpoint: '',
regexes: ['.*'],
shouldMatch: false,
},
{
url: undefined,
regex: '.*',
shouldSkip: false,
endpoint: null,
regexes: ['.*'],
shouldMatch: false,
},
].map(({ url, regex, shouldSkip }) => {
test('test should skip span with url', () => {
if (regex) {
process.env.LUMIGO_AUTO_FILTER_HTTP_ENDPOINTS_REGEX = regex;
}
expect(shouldSkipSpanOnRouteMatch(url)).toEqual(shouldSkip);
{
endpoint: undefined,
regexes: ['.*'],
shouldMatch: false,
},
].map(({ endpoint, regexes, shouldMatch }) => {
test(`test regex match - ${endpoint}`, () => {
expect(doesEndpointMatchRegexes(endpoint, regexes)).toEqual(shouldMatch);
});
});

[
{
description: 'happy flow - full url field exists',
cases: [
{
attributes: {
'http.url': 'https://example.com',
},
expectedUrl: 'https://example.com',
},
{
attributes: {
'http.url': 'https://example.com?page=1',
},
expectedUrl: 'https://example.com?page=1',
},
],
},
{
description: 'happy flow - schema host and target fields exist',
cases: [
{
attributes: {
'http.scheme': 'https',
'http.host': 'example.com',
'http.target': '/',
},
expectedUrl: 'https://example.com',
},
{
attributes: {
'http.scheme': 'https',
'http.host': 'example.com',
'http.target': '/about',
},
expectedUrl: 'https://example.com/about',
},
{
attributes: {
'http.scheme': 'https',
'http.host': 'example.com',
'http.target': '/about?page=1',
},
expectedUrl: 'https://example.com/about?page=1',
},
],
},
{
description: 'http endpoint standard port',
cases: [
{
attributes: {
'http.url': 'https://example.com:443',
},
expectedUrl: 'https://example.com',
},
{
attributes: {
'http.url': 'http://example.com:80',
},
expectedUrl: 'http://example.com',
},
],
},
{
description: 'http endpoint non standard port',
cases: [
{
attributes: {
'http.url': 'https://example.com:80',
},
expectedUrl: 'https://example.com:80',
},
{
attributes: {
'http.url': 'https://example.com:80/about',
},
expectedUrl: 'https://example.com:80/about',
},
{
attributes: {
'http.url': 'http://example.com:443',
},
expectedUrl: 'http://example.com:443',
},
{
attributes: {
'http.url': 'http://example.com:443/about',
},
expectedUrl: 'http://example.com:443/about',
},
{
attributes: {
'http.scheme': 'https',
'http.host': 'example.com:80',
'http.target': '/about',
},
expectedUrl: 'https://example.com:80/about',
},
],
},
{
description: 'http root url',
cases: [
{
attributes: {
'http.url': 'https://example.com/',
},
expectedUrl: 'https://example.com',
},
],
},
{
description: 'missing values',
cases: [
{
attributes: null,
expectedUrl: null,
},
{
attributes: undefined,
expectedUrl: null,
},
{
attributes: {},
expectedUrl: null,
},
],
},
].map(({ description, cases }) => {
return cases.map(({ attributes, expectedUrl }) => {
test(`test extract url from span - ${description}`, () => {
expect(extractUrl(attributes)).toEqual(expectedUrl);
});
attributes: { 'url.path': 'urlPath', 'http.target': 'httpTarget' },
spanKind: SpanKind.SERVER,
expectedEndpoint: 'urlPath',
},
{
attributes: { a: 'a', 'http.target': 'httpTarget' },
spanKind: SpanKind.SERVER,
expectedEndpoint: 'httpTarget',
},
{
attributes: { 'url.full': 'fullUrl', 'http.url': 'httpUrl' },
spanKind: SpanKind.CLIENT,
expectedEndpoint: 'fullUrl',
},
{
attributes: { a: 'a', 'http.url': 'httpUrl' },
spanKind: SpanKind.CLIENT,
expectedEndpoint: 'httpUrl',
},
{
attributes: {
'url.path': 'urlPath',
'http.target': 'httpTarget',
'url.full': 'fullUrl',
'http.url': 'httpUrl',
},
spanKind: SpanKind.INTERNAL,
expectedEndpoint: null,
},
{
attributes: {},
spanKind: SpanKind.SERVER,
expectedEndpoint: null,
},
{
attributes: {},
spanKind: SpanKind.CLIENT,
expectedEndpoint: null,
},
].map(({ attributes, spanKind, expectedEndpoint }) => {
test(`test extract endpoint - ${JSON.stringify(attributes)}`, () => {
expect(extractEndpoint(attributes, spanKind)).toEqual(expectedEndpoint);
});
});
});
Loading
Loading