Skip to content
This repository has been archived by the owner on Jun 13, 2023. It is now read-only.

Commit

Permalink
Feat(reduce-epsagon-network): expose otl batching and sampling (#65)
Browse files Browse the repository at this point in the history
* lint fixes

* expose otl batch config

* expose otl sampling
  • Loading branch information
Nimrod Shlagman authored Oct 27, 2021
1 parent a27dda7 commit 9208b90
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 33 deletions.
1 change: 1 addition & 0 deletions packages/web/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = {
extends: 'airbnb-base',
ignorePatterns: ['*.test.js', '**/dist/*.js', '**/test/*.js'],
rules: { 'max-len': ['error', { code: 120 }] },
};
16 changes: 15 additions & 1 deletion packages/web/src/consts.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
const VERSION = require('../package.json').version;

const DEFAULT_CONFIGURATIONS = {
appName: 'Epsagon Application',
collectorURL: 'https://opentelemetry.tc.epsagon.com/traces',
pageLoadTimeout: 30000,
redirectTimeout: 3000,
maxBatchSize: 512,
maxQueueSize: 2048,
scheduledDelayMillis: 5000,
exportTimeoutMillis: 30000,
networkSamplingRatio: 1,
};

const ROOT_TYPE = {
EPS: 'epsagon_init',
DOC: 'document_load',
Expand Down Expand Up @@ -28,4 +40,6 @@ const SPAN_ATTRIBUTES_NAMES = {
MESSAGE: 'message',
};

export { VERSION, ROOT_TYPE, SPAN_ATTRIBUTES_NAMES };
export {
VERSION, DEFAULT_CONFIGURATIONS, ROOT_TYPE, SPAN_ATTRIBUTES_NAMES,
};
5 changes: 3 additions & 2 deletions packages/web/src/exporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class EpsagonExporter extends CollectorTraceExporter {
}

convert(spans) {
const errorSpans = spans.filter((s) => s.exceptionData);
try {
const errorSpans = spans.filter((s) => s.exceptionData);
const convertedSpans = super.convert(spans);
let spansList = EpsagonUtils.getFirstResourceSpan(convertedSpans).instrumentationLibrarySpans;
const rootSpan = {
Expand Down Expand Up @@ -142,7 +142,7 @@ class EpsagonExporter extends CollectorTraceExporter {
spansList.splice(rootSpan.eps.position, 1);
}

let convertedSpansWithRecourseAtts = this.resourceManager.addResourceAttrs(convertedSpans, this.userAgent);
const convertedSpansWithRecourseAtts = this.resourceManager.addResourceAttrs(convertedSpans, this.userAgent);
diag.debug('converted spans:', convertedSpansWithRecourseAtts);
return convertedSpansWithRecourseAtts;
} catch (err) {
Expand Down Expand Up @@ -214,6 +214,7 @@ class EpsagonExporter extends CollectorTraceExporter {
return { attributesLength, span, spanAttributes };
}

// eslint-disable-next-line no-unused-vars
send(objects, onSuccess, onError) {
super.send(objects, onSuccess, loggingErrorHandler());
}
Expand Down
74 changes: 44 additions & 30 deletions packages/web/src/web-tracer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,22 @@
import { BatchSpanProcessor } from '@opentelemetry/tracing';
import { WebTracerProvider } from '@opentelemetry/web';
import { ZoneContextManager } from '@opentelemetry/context-zone';
import { diag, DiagConsoleLogger, DiagLogLevel } from "@opentelemetry/api";
import { diag, DiagConsoleLogger, DiagLogLevel } from '@opentelemetry/api';
import { setGlobalErrorHandler, loggingErrorHandler, TraceIdRatioBasedSampler } from '@opentelemetry/core';
import EpsagonFetchInstrumentation from './instrumentation/fetchInstrumentation';
import EpsagonXMLHttpRequestInstrumentation from './instrumentation/xmlHttpInstrumentation';
import EpsagonDocumentLoadInstrumentation from './instrumentation/documentLoadInstrumentation';
import EpsagonExporter from './exporter';
import EpsagonUtils from './utils';
import EpsagonRedirectInstrumentation from './instrumentation/redirectInstrumentation';
import {
setGlobalErrorHandler,
loggingErrorHandler, globalErrorHandler
} from "@opentelemetry/core";
import { DEFAULT_CONFIGURATIONS } from './consts';

const { CompositePropagator, HttpTraceContextPropagator } = require('@opentelemetry/core');
const parser = require('ua-parser-js');
const { registerInstrumentations } = require('@opentelemetry/instrumentation');

let existingTracer;
let epsSpan;
const DEFAULT_APP_NAME = 'Epsagon Application';
const PAGE_LOAD_TIMEOUT = 30000;


class EpsagonSpan {
constructor(tracer) {
Expand All @@ -43,7 +38,7 @@ class EpsagonSpan {
}

get currentSpan() {
if (this._time !== null && this._time + PAGE_LOAD_TIMEOUT >= Date.now()) {
if (this._time !== null && this._time + DEFAULT_CONFIGURATIONS.pageLoadTimeout >= Date.now()) {
return this._currentSpan;
}
this.currentSpan = null;
Expand Down Expand Up @@ -82,42 +77,48 @@ function handleLogLevel(_logLevel) {
switch (_logLevel) {
case 'ALL':
logLevel = DiagLogLevel.ALL;
break
break;
case 'DEBUG':
logLevel = DiagLogLevel.DEBUG;
break
break;
case 'INFO':
logLevel = DiagLogLevel.INFO;
break
break;
case 'WARN':
logLevel = DiagLogLevel.WARN
break
logLevel = DiagLogLevel.WARN;
break;
case 'ERROR':
logLevel = DiagLogLevel.ERROR
break
logLevel = DiagLogLevel.ERROR;
break;
// Default is Open Telemetry default which is DiagLogLevel.INFO
default:
return
return;
}
diag.setLogger(new DiagConsoleLogger(), logLevel)
diag.setLogger(new DiagConsoleLogger(), logLevel);
}

function init(_configData) {
const configData = _configData;

if (configData.logLevel) {
handleLogLevel(configData.logLevel)
handleLogLevel(configData.logLevel);
}

// Epsagon debug overrides configData.logLevel
if (configData.epsagonDebug) {
diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.DEBUG)
diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.DEBUG);
}

diag.info('configData: ', configData);

let samplingRatio = DEFAULT_CONFIGURATIONS.networkSamplingRatio;

if (configData.networkSamplingRatio || configData.networkSamplingRatio === 0) {
samplingRatio = configData.networkSamplingRatio;
}

if (configData.isEpsagonDisabled) {
console.log('epsagon disabled, tracing not running');
console.log('Epsagon disabled, tracing is not running');
return undefined;
}

Expand All @@ -132,10 +133,10 @@ function init(_configData) {
}

if (!configData.collectorURL) {
configData.collectorURL = 'https://opentelemetry.tc.epsagon.com/traces';
configData.collectorURL = DEFAULT_CONFIGURATIONS.collectorURL;
}

const appName = configData.appName || DEFAULT_APP_NAME;
const appName = configData.appName || DEFAULT_CONFIGURATIONS.appName;

const collectorOptions = {
serviceName: appName,
Expand All @@ -147,17 +148,30 @@ function init(_configData) {
metadataOnly: configData.metadataOnly,
};

const maxExportBatchSize = configData.maxBatchSize || DEFAULT_CONFIGURATIONS.maxBatchSize;
const maxQueueSize = configData.maxQueueSize || DEFAULT_CONFIGURATIONS.maxQueueSize;
if (maxExportBatchSize > maxQueueSize) {
diag.error('maxExportBatchSize cannot be bigger than maxQueueSize, could not start Epsagon');
return undefined;
}

const batchProcessorConfig = {
maxExportBatchSize,
maxQueueSize,
scheduledDelayMillis: configData.scheduledDelayMillis || DEFAULT_CONFIGURATIONS.scheduledDelayMillis,
exportTimeoutMillis: configData.exportTimeoutMillis || DEFAULT_CONFIGURATIONS.exportTimeoutMillis,
};

setGlobalErrorHandler(loggingErrorHandler());

const provider = new WebTracerProvider();
const provider = new WebTracerProvider({ sampler: new TraceIdRatioBasedSampler(samplingRatio) });

/* eslint-disable no-undef */
const userAgent = parser(navigator.userAgent);

const exporter = new EpsagonExporter(collectorOptions, userAgent);


provider.addSpanProcessor(new BatchSpanProcessor(exporter));
provider.addSpanProcessor(new BatchSpanProcessor(exporter, batchProcessorConfig));

provider.register({
contextManager: new ZoneContextManager(),
Expand Down Expand Up @@ -185,14 +199,14 @@ function init(_configData) {
}

let blackListedURLs = [];
if(configData.urlPatternsToIgnore) {
if (configData.urlPatternsToIgnore) {
blackListedURLs = configData.urlPatternsToIgnore;
blackListedURLs.forEach(function (item, index, arr){
blackListedURLs.forEach((item, index, arr) => {
// eslint-disable-next-line no-param-reassign
arr[index] = RegExp(item);
});
}

const resetTimer = 3000;
registerInstrumentations({
tracerProvider: provider,
instrumentations: [
Expand All @@ -205,7 +219,7 @@ function init(_configData) {
ignoreUrls: blackListedURLs,
propagateTraceHeaderCorsUrls: whiteListedURLsRegex,
}, epsSpan, { metadataOnly: configData.metadataOnly }),
new EpsagonRedirectInstrumentation(tracer, epsSpan, resetTimer),
new EpsagonRedirectInstrumentation(tracer, epsSpan, DEFAULT_CONFIGURATIONS.redirectTimeout),
],
});

Expand Down
11 changes: 11 additions & 0 deletions packages/web/test/web-tracer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ describe('init tests', () => {
chai.assert.notExists(res, 'res should be false');
done();
});

it('init function produces tracer and epsSpan even if epsagon is not sampled', (done) => {
const res = epsagon.init({
token: 'fasdfsafa', appName, isTest: true, networkSamplingRatio: 0,
});
chai.assert.exists(res.tracer, 'tracer was created');
chai.assert.exists(res.epsSpan, 'epsSpan was created');
chai.assert.equal(res.tracer.instrumentationLibrary.name, appName, 'app name should be passed into tracer');
chai.assert.exists(res.epsSpan.currentSpan, 'current span should have been created');
done();
});
});

describe('logging tests', () => {
Expand Down

0 comments on commit 9208b90

Please sign in to comment.