forked from open-telemetry/opentelemetry-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Document Load Instrumentation (open-telemetry#331)
* chore: document load instrumentation * chore: document load instrumentation * chore: adding example for document load, fixing context * chore: lint fix * chore: updating documentation * chore: reviews
- Loading branch information
Showing
24 changed files
with
288 additions
and
154 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
|
||
<head> | ||
<meta charset="utf-8"> | ||
<title>Document Load Plugin Example</title> | ||
<base href="/"> | ||
|
||
<!-- | ||
https://www.w3.org/TR/trace-context/ | ||
Set the `traceparent` in the server's HTML template code. It should be | ||
dynamically generated server side to have the server's request trace Id, | ||
a parent span Id that was set on the server's request span, and the trace | ||
flags to indicate the server's sampling decision | ||
(01 = sampled, 00 = notsampled). | ||
'{version}-{traceId}-{spanId}-{sampleDecision}' | ||
--> | ||
<meta name="traceparent" content="00-ab42124a3c573678d4d8b21ba52df3bf-d21f7bc17caa5aba-01"> | ||
|
||
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
</head> | ||
|
||
<body> | ||
Example of using Web Tracer with document load plugin with console exporter and collector exporter | ||
<script type="text/javascript" src="document-load.js"></script> | ||
<br/> | ||
<button id="button1">Test WebTracer with ZoneContextManager - async</button> | ||
|
||
</body> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import { context, getSpan, setSpan } from '@opentelemetry/api'; | ||
import { ConsoleSpanExporter, SimpleSpanProcessor } from '@opentelemetry/tracing'; | ||
import { WebTracerProvider } from '@opentelemetry/web'; | ||
import { DocumentLoadInstrumentation } from '@opentelemetry/instrumentation-document-load'; | ||
import { XMLHttpRequestInstrumentation } from '@opentelemetry/instrumentation-xml-http-request'; | ||
import { ZoneContextManager } from '@opentelemetry/context-zone'; | ||
import { CollectorTraceExporter } from '@opentelemetry/exporter-collector'; | ||
import { B3Propagator } from '@opentelemetry/propagator-b3'; | ||
import { CompositePropagator, HttpTraceContext } from '@opentelemetry/core'; | ||
import { registerInstrumentations } from '@opentelemetry/instrumentation'; | ||
|
||
const provider = new WebTracerProvider(); | ||
provider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter())); | ||
provider.addSpanProcessor(new SimpleSpanProcessor(new CollectorTraceExporter())); | ||
|
||
provider.register({ | ||
contextManager: new ZoneContextManager(), | ||
propagator: new CompositePropagator({ | ||
propagators: [ | ||
new B3Propagator(), | ||
new HttpTraceContext(), | ||
], | ||
}), | ||
}); | ||
registerInstrumentations({ | ||
instrumentations: [ | ||
new DocumentLoadInstrumentation(), | ||
new XMLHttpRequestInstrumentation({ | ||
ignoreUrls: [/localhost/], | ||
propagateTraceHeaderCorsUrls: [ | ||
'http://localhost:8090', | ||
], | ||
}), | ||
], | ||
tracerProvider: provider, | ||
}); | ||
|
||
const tracer = provider.getTracer('example-document-load'); | ||
|
||
const getData = (url) => new Promise((resolve, reject) => { | ||
// eslint-disable-next-line no-undef | ||
const req = new XMLHttpRequest(); | ||
req.open('GET', url, true); | ||
req.send(); | ||
req.onload = () => { | ||
let json; | ||
try { | ||
json = JSON.parse(req.responseText); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
resolve(json); | ||
}; | ||
}); | ||
|
||
// example of keeping track of context between async operations | ||
const prepareClickEvent = () => { | ||
const url1 = 'https://raw.githubusercontent.com/open-telemetry/opentelemetry-js/master/package.json'; | ||
const url2 = 'https://raw.githubusercontent.com/open-telemetry/opentelemetry-js/master/packages/opentelemetry-web/package.json'; | ||
|
||
const element = document.getElementById('button1'); | ||
|
||
const onClick = () => { | ||
let count = 0; | ||
|
||
function finish() { | ||
count++; | ||
if (count === 2) { | ||
mainSpan.end(); | ||
} | ||
} | ||
|
||
const mainSpan = tracer.startSpan('click button'); | ||
context.with(setSpan(context.active(), mainSpan), () => { | ||
const span1 = tracer.startSpan('files-series-info-1'); | ||
|
||
const span2 = tracer.startSpan('files-series-info-2'); | ||
|
||
context.with(setSpan(context.active(), span1), () => { | ||
getData(url1).then((data) => { | ||
const curSpan = getSpan(context.active()); | ||
console.log('current span is span1', curSpan === span1); | ||
console.log('info from package.json', data.description, data.version); | ||
curSpan.addEvent('fetching-span1-completed'); | ||
span1.end(); | ||
finish(); | ||
}); | ||
}); | ||
|
||
context.with(setSpan(context.active(), span2), () => { | ||
getData(url2).then((data) => { | ||
setTimeout(() => { | ||
const curSpan = getSpan(context.active()); | ||
console.log('current span is span2', curSpan === span2); | ||
console.log('info from package.json', data.description, data.version); | ||
curSpan.addEvent('fetching-span2-completed'); | ||
span2.end(); | ||
finish(); | ||
}, 100); | ||
}); | ||
}); | ||
}); | ||
}; | ||
element.addEventListener('click', onClick); | ||
}; | ||
|
||
window.addEventListener('load', prepareClickEvent); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
Oops, something went wrong.