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

Commit

Permalink
fix(logs): add logs (#49)
Browse files Browse the repository at this point in the history
* fix(logs): add logs

* fix(logs): fix logs

* fix(logs): fix logs
  • Loading branch information
haddasbronfman authored Sep 2, 2021
1 parent 3de10bf commit 24e2bd5
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 61 deletions.
18 changes: 17 additions & 1 deletion packages/web/src/exporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class EpsagonExporter extends CollectorTraceExporter {

Object.keys(spansList).forEach((spanIndex) => {
const spanSubList = spansList[spanIndex].spans;
diag.debug('Handle spans for index ', spanIndex);

/* eslint-disable no-restricted-syntax */
for (const spanSubIndex in spanSubList) {
Expand All @@ -77,6 +78,7 @@ class EpsagonExporter extends CollectorTraceExporter {
let attributesLength = spanAttributes.length;

if (span.name === rootType.EPS) {
diag.debug('Handle epsagon_init span', span);
rootSpan.eps.position = spanIndex;
rootSpan.eps.subPosition = spanSubIndex;
rootSpan.eps.spanId = span.spanId;
Expand All @@ -101,17 +103,21 @@ class EpsagonExporter extends CollectorTraceExporter {
const reactUpdates = spanAttributes.filter((attr) => attr.key === spanAttributeNames.REACT_COMPONENT_NAME);

if (httpHost.length > 0) {
diag.debug('httpHost:', httpHost);
const formattedHttpRequestSpan = this.formatter.formatHttpRequestSpan(span, httpHost, spanAttributes, attributesLength);
spanAttributes = formattedHttpRequestSpan.spanAttributes;
attributesLength = formattedHttpRequestSpan.attributesLength;
span = formattedHttpRequestSpan.span;
} else if (userInteraction.length > 0) {
diag.debug('userInteraction:', userInteraction);
const formattedSpan = EpsagonFormatter.formatUserInteractionSpan(spanAttributes, attributesLength);
spanAttributes[attributesLength] = formattedSpan.userInteraction;
attributesLength += 1;
spanAttributes[attributesLength] = formattedSpan.operation;
attributesLength += 1;
} else if (documentLoad.length > 0 || reactUpdates.length > 0) {
diag.debug('document load:', documentLoad);
diag.debug('react updates:', reactUpdates);
rootSpan.doc.position = spanIndex;

// replace root span with document load
Expand All @@ -120,6 +126,7 @@ class EpsagonExporter extends CollectorTraceExporter {
rootSpan.eps.remove = true;
rootSpan.doc.subPosition = spanSubIndex;
rootSpan.doc.parent = span.parentSpanId;
diag.debug('replace root span with document load:', rootSpan);
}

const formattedSpan = EpsagonFormatter.formatDocumentLoadSpan();
Expand All @@ -129,6 +136,7 @@ class EpsagonExporter extends CollectorTraceExporter {
spanAttributes[attributesLength] = formattedSpan.operation;
attributesLength += 1;
} else if (span.name === spanAttributeNames.ROUTE_CHANGE) {
diag.debug('span name is route_change');
rootSpan.rootType = rootType.REDIR;

const formattedSpan = EpsagonFormatter.formatRouteChangeSpan(this.userAgent);
Expand All @@ -150,25 +158,31 @@ class EpsagonExporter extends CollectorTraceExporter {

if (errorSpans && errorSpans.length > 0) {
spansList = EpsagonExporter.handleErrors(errorSpans, spansList, rootSpan);
diag.debug('Error spans:', spansList);
}

if (rootSpan.eps.remove && rootSpan.doc.parent === rootSpan.eps.spanId) {
diag.debug('Remove document load span');
const docLoad = spansList[rootSpan.doc.position].spans[0];
docLoad.parentSpanId = undefined;
docLoad.spanId = rootSpan.eps.spanId;
spansList.splice(rootSpan.eps.position, 1);
}

return this.resourceManager.addResourceAttrs(convertedSpans, this.userAgent);
let convertedSpansWithRecourseAtts = this.resourceManager.addResourceAttrs(convertedSpans, this.userAgent);
diag.debug('converted spans:', convertedSpansWithRecourseAtts);
return convertedSpansWithRecourseAtts;
} catch (err) {
diag.warn('error converting and exporting', err);
return null;
}
}

static handleErrors(errorSpans, _spansList, rootSpan) {
diag.debug('handle errors:', errorSpans);
const spansList = _spansList;
if (rootSpan.rootType === rootType.REDIR || rootSpan.rootType === rootType.DOC) {
diag.debug('rootType:', rootType);
const type = rootSpan.rootType === rootType.REDIR ? rootType.REDIR : rootType.ROOT_TYPE_DOC;
const rootSubList = spansList[rootSpan[type].position].spans;
const rootSubPos = rootSpan[type].subPosition;
Expand All @@ -183,6 +197,7 @@ class EpsagonExporter extends CollectorTraceExporter {
rootSubList[rootSpan[type].subPosition].status.code = 2;
spansList[rootSpan.doc.position].spans = spansList[rootSpan.doc.position].spans.filter((span) => span.name !== rootType.ERROR);
} else {
diag.debug('remove duplicate events and add attrs.');
/// remove duplicate events and add attrs
const finalSpans = [];
spansList[rootSpan.doc.position].spans.forEach((span) => {
Expand Down Expand Up @@ -222,6 +237,7 @@ class EpsagonExporter extends CollectorTraceExporter {
}
return attr.key !== spanAttributeNames.RESPONSE_CONTENT_LENGTH_EPS;
});
diag.debug('FinalGenericSpanAttrs:', spanAttributes);
return { attributesLength, span, spanAttributes };
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { ROOT_CONTEXT } from '@opentelemetry/api';
import { DocumentLoadInstrumentation } from '@opentelemetry/instrumentation-document-load';
import EpsagonUtils from '../utils';
import { diag } from "@opentelemetry/api";

const api = require('@opentelemetry/api');

Expand All @@ -26,15 +27,17 @@ class EpsagonDocumentLoadInstrumentation extends DocumentLoadInstrumentation {
}

_startSpan(spanName, performanceName, entries) {
diag.debug('start span with name: ', spanName);
// drop document fetch events
if (spanName === 'documentFetch') {
return undefined;
}
const initialSpan = super._startSpan(spanName, performanceName, entries, this.epsParentSpan.currentSpan);
const initialSpan = super._startSpan(spanName, performanceName, entries, this.epsParentSpan.currentSpan);
if (initialSpan && !this.epsParentSpan.currentSpan) {
this.epsParentSpan.currentSpan = initialSpan;
}
EpsagonUtils.addEpsSpanAttrs(initialSpan, this.epsParentSpan);
diag.debug('initialSpan: ', initialSpan);
return initialSpan;
}

Expand All @@ -55,6 +58,7 @@ class EpsagonDocumentLoadInstrumentation extends DocumentLoadInstrumentation {
}

reportError(event) {
diag.debug('DocumentLoadInstrumentation: reportError');
const error = event.error ? event.error : event.reason;
const failedToExportError = (this._includes(error.message, 'Failed to export with XHR (status: 502)')) || this._includes(error, 'Failed to export with XHR (status: 502)');
if (error && failedToExportError) {
Expand All @@ -73,7 +77,8 @@ class EpsagonDocumentLoadInstrumentation extends DocumentLoadInstrumentation {
};
EpsagonUtils.addEpsSpanAttrs(span, this.epsParentSpan);
span.setStatus({ code: 2 });
span.end();
diag.debug('error span: ', span);
span.end();
}

/* eslint-disable no-undef */
Expand Down
12 changes: 7 additions & 5 deletions packages/web/src/instrumentation/fetchInstrumentation.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,18 @@ class EpsagonFetchInstrumentation extends FetchInstrumentation {
return (original) => {
const plugin = this;
return function patchConstructor(input, init) {
diag.debug(`input: ${input}, init: ${init} `);
diag.debug('input: ', input, ' init: ', init);
const url = input instanceof Request ? input.url : input;
const options = input instanceof Request ? input : init || {};
diag.debug(`url: ${url}, options: ${options} `);
diag.debug('url: ', url, 'options: ', options);
if (options.eps) {
// if epsagon request, ignore and dont send through eps param
diag.debug("epsagon request. ignore and don't send through eps param");
return original.apply(this, [url, {}]);
}
const createdSpan = plugin._createSpan(url, options);
if (!createdSpan) {
diag.debug('span was not created. apply the original function');
return original.apply(this, [url, options]);
}
const spanData = plugin._prepareSpanData(url);
Expand Down Expand Up @@ -106,9 +108,9 @@ class EpsagonFetchInstrumentation extends FetchInstrumentation {
}
}
return new Promise((resolve, reject) => api.context.with(api.trace.setSpan(api.context.active(), createdSpan), () => {
diag.debug(`Before add headers: url: ${url}, options: ${options} `);
diag.debug('Before add headers: url: ', url, 'options: ', options);
plugin._addHeaders(options, url);
diag.debug(`After add headers: url: ${url}, options: ${options} `);
diag.debug('After add headers: url: ', url , 'options: ', options);
plugin._tasksCount += 1;
return original
.apply(this, [url, options])
Expand All @@ -130,7 +132,7 @@ class EpsagonFetchInstrumentation extends FetchInstrumentation {
}
const method = (options.method || 'GET').toUpperCase();
const spanName = `HTTP ${method}`;
diag.debug(`create span: url: ${url}, options: ${options} `);
diag.debug('create span: url: ', url , 'options: ', options);

let span;
if (this.globalOptions.metadataOnly) {
Expand Down
2 changes: 2 additions & 0 deletions packages/web/src/instrumentation/redirectInstrumentation.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import EpsagonUtils from '../utils';
import { diag } from "@opentelemetry/api";

/* eslint-disable no-undef */
class EpsagonRedirectInstrumentation {
Expand Down Expand Up @@ -31,6 +32,7 @@ class EpsagonRedirectInstrumentation {
}
EpsagonUtils.addEpsSpanAttrs(span, this.parentSpan);
span.setStatus({ code: 0 });
diag.debug('create span for redirect', span);
span.end();
}
}
Expand Down
7 changes: 6 additions & 1 deletion packages/web/src/instrumentation/xmlHttpInstrumentation.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { XMLHttpRequestInstrumentation } from '@opentelemetry/instrumentation-xm
const api = require('@opentelemetry/api');
const core1 = require('@opentelemetry/core');
const semanticConventions1 = require('@opentelemetry/semantic-conventions');
const diag = api.diag;

class EpsagonXMLHttpRequestInstrumentation extends XMLHttpRequestInstrumentation {
constructor(config, parentSpan, options) {
Expand All @@ -15,8 +16,9 @@ class EpsagonXMLHttpRequestInstrumentation extends XMLHttpRequestInstrumentation

// create span copied over so parent span can be added at creation
_createSpan(xhr, url, method) {
diag.debug('xmlHttpInstrumentation: create span for url: ', url, 'method: ', method);
if (core1.isUrlIgnored(url, this._getConfig().ignoreUrls)) {
api.diag.debug('ignoring span as url matches ignored url');
diag.debug('ignoring span as url matches ignored url');
return undefined;
}
const spanName = `HTTP ${method.toUpperCase()}`;
Expand All @@ -33,10 +35,12 @@ class EpsagonXMLHttpRequestInstrumentation extends XMLHttpRequestInstrumentation
span: currentSpan,
spanUrl: url,
});
diag.debug('xmlHttpInstrumentation: created span: ', currentSpan);
return currentSpan;
}

_addFinalSpanAttributes(span, xhrMem, spanUrl) {
diag.debug('xmlHttpInstrumentation: before add final attributes: ', span);
super._addFinalSpanAttributes(span, xhrMem, spanUrl);
let responseBody = xhrMem.xhrInstance.response;

Expand All @@ -56,6 +60,7 @@ class EpsagonXMLHttpRequestInstrumentation extends XMLHttpRequestInstrumentation
}
span.setAttribute('http.response_content_length', xhrMem.xhrInstance.getResponseHeader('content-length'));
span.setAttribute('http.request.body', xhrMem.xhrInstance.__zone_symbol__xhrTask.data.args[0]);
diag.debug('xmlHttpInstrumentation: after add final attributes: ', span);
}
}

Expand Down
52 changes: 0 additions & 52 deletions packages/web/src/span-manager.js

This file was deleted.

2 changes: 2 additions & 0 deletions packages/web/src/web-tracer.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ function init(_configData) {
diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.DEBUG)
}

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

if (configData.isEpsagonDisabled) {
console.log('epsagon disabled, tracing not running');
return undefined;
Expand Down

0 comments on commit 24e2bd5

Please sign in to comment.