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

docs: add example config & API calls for network events #165

Merged
merged 3 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
227 changes: 124 additions & 103 deletions examples/custom-with-collector-ts/package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/custom-with-collector-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"license": "ISC",
"dependencies": {
"@honeycombio/opentelemetry-web": "file:../../",
"@opentelemetry/auto-instrumentations-web": "~0.36.0"
"@opentelemetry/auto-instrumentations-web": "^0.40.0"
},
"devDependencies": {
"html-webpack-plugin": "^5.5.4",
Expand Down
5 changes: 5 additions & 0 deletions examples/custom-with-collector-ts/src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
<h1>Example app sending to a collector</h1>
</header>
<button id="button-important" data-cy="button">click me!</button>

<button id="loadDadJoke">Get A Random Dad Joke</button>
<div>
<span id="dadJokeText"></span>
</div>
</section>
<!-- Scripts here. Don't remove ↓ -->
<script type="module" src="dist/index.js"></script>
Expand Down
41 changes: 37 additions & 4 deletions examples/custom-with-collector-ts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import { HoneycombWebSDK } from '@honeycombio/opentelemetry-web';
import { getWebAutoInstrumentations } from '@opentelemetry/auto-instrumentations-web';

const tracing = () => {
const configDefaults = {
ignoreNetworkEvents: true,
};

const sdk = new HoneycombWebSDK({
// To send direct to Honeycomb, set API Key and comment out endpoint
// apiKey: 'api-key',
Expand All @@ -11,7 +15,14 @@ const tracing = () => {
debug: true,
skipOptionsValidation: true,
resourceAttributes: { 'app.environment': 'development' },
instrumentations: [getWebAutoInstrumentations()], // add auto-instrumentation
instrumentations: [
// add auto-instrumentation
getWebAutoInstrumentations({
'@opentelemetry/instrumentation-xml-http-request': configDefaults,
'@opentelemetry/instrumentation-fetch': configDefaults,
'@opentelemetry/instrumentation-document-load': configDefaults,
}),
],
});
sdk.start();
};
Expand All @@ -20,13 +31,14 @@ const trackButton = (onClick: { (): void; (): void }) => {
const button = document.getElementById(
'button-important',
) as HTMLButtonElement;
const tracer = trace.getTracer('click-tracer');

button.onclick = () =>
tracer.startActiveSpan(`clicked the button`, (span) => {
button.onclick = () => {
const tracer = trace.getTracer('click-tracer');
return tracer.startActiveSpan(`clicked the button`, (span) => {
onClick();
span.end();
});
};
};

const onClick = () => {
Expand All @@ -50,8 +62,29 @@ const onClick = () => {
});
};

const setupAPICall = () => {
document.getElementById('loadDadJoke')!.onclick = () => {
fetch('https://icanhazdadjoke.com/', {
headers: {
'content-type': 'application/json',
accept: 'application/json',
},
})
.then((res) => {
return res.json();
})
.then((data) => {
document.getElementById('dadJokeText')!.innerText = data.joke;
})
.catch((e) => {
console.error(e);
});
};
};

const main = () => {
tracing();
trackButton(onClick);
setupAPICall();
};
main();
1 change: 1 addition & 0 deletions examples/custom-with-collector-ts/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@ export default {
compress: true,
host: '0.0.0.0',
},
devtool: 'eval-source-map',
};
5 changes: 5 additions & 0 deletions examples/hello-world-web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
<header class="header">
<h1>👋 Hello World</h1>
</header>

<button id="loadDadJoke">Get A Random Dad Joke</button>
<div>
<span id="dadJokeText"></span>
</div>
</section>
<!-- Scripts here. Don't remove ↓ -->
<script type="module" src="build/bundle.js"></script>
Expand Down
45 changes: 36 additions & 9 deletions examples/hello-world-web/index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,47 @@
import { HoneycombWebSDK } from '@honeycombio/opentelemetry-web';
import { getWebAutoInstrumentations } from '@opentelemetry/auto-instrumentations-web';

const configDefaults = {
ignoreNetworkEvents: true,
};

const main = () => {
// Initialize base OTel WebSDK
// Initialize Honeycomb SDK
const sdk = new HoneycombWebSDK({
// endpoint: 'http://localhost:4318', // send to collector
// To send to collector, comment out API Key
apiKey: 'api-key',
serviceName: 'web-distro',
// defaults to sending to US instance of Honeycomb
// endpoint: "https://api.eu1.honeycomb.io/v1/traces", // uncomment to send to EU instance
apiKey: 'api-key', // Replace with your Honeycomb Ingest API Key
serviceName: 'web-distro', // Replace with your application name. Honeycomb will name your dataset using this variable.
debug: true,
instrumentations: [getWebAutoInstrumentations()], // add auto-instrumentation
// webVitalsInstrumentationConfig: {
// enabled: false,
// },
instrumentations: [
getWebAutoInstrumentations({
// load custom configuration for xml-http-request instrumentation
'@opentelemetry/instrumentation-xml-http-request': configDefaults,
'@opentelemetry/instrumentation-fetch': configDefaults,
'@opentelemetry/instrumentation-document-load': configDefaults,
}),
],
});
sdk.start();

// add event handlers
document.getElementById('loadDadJoke').onclick = () => {
fetch('https://icanhazdadjoke.com/', {
headers: {
'content-type': 'application/json',
accept: 'application/json',
},
})
.then((res) => {
return res.json();
})
.then((data) => {
document.getElementById('dadJokeText').innerText = data.joke;
})
.catch((e) => {
console.error(e);
});
};
};

main();
Loading
Loading