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

feat: Add page view instrumentation plugin #2386

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 12 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
3 changes: 2 additions & 1 deletion .release-please-manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,6 @@
"plugins/web/opentelemetry-instrumentation-user-interaction": "0.39.0",
"plugins/web/opentelemetry-plugin-react-load": "0.30.1",
"propagators/opentelemetry-propagator-instana": "0.3.2",
"propagators/opentelemetry-propagator-ot-trace": "0.27.2"
"propagators/opentelemetry-propagator-ot-trace": "0.27.2",
"plugins/web/opentelemetry-instrumentation-page-view": "0.39.0"
}
8 changes: 8 additions & 0 deletions examples/web/examples/document-load/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@
<script type="text/javascript" src="document-load.js"></script>
<br/>
<button id="button1">Test WebTracer with ZoneContextManager - async</button>
<nav>
<a href="document-load/route1" data-link>Route 1</a>
<a href="document-load/route2" data-link>Route 2</a>
</nav>

<div id="content">
<!-- Content will be loaded here -->
</div>

</body>

Expand Down
1 change: 1 addition & 0 deletions examples/web/examples/document-load/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,4 @@ const prepareClickEvent = () => {
};

window.addEventListener('load', prepareClickEvent);

38 changes: 38 additions & 0 deletions examples/web/examples/page-view/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8">
<title>Page View 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="page-view.js"></script>
<br/>
<nav>
<a href="page-view/route1" data-link>Route 1</a>
<a href="page-view/route2" data-link>Route 2</a>
</nav>

<div id="content">
<!-- Content will be loaded here -->
</div>

</body>

</html>
62 changes: 62 additions & 0 deletions examples/web/examples/page-view/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { context, trace } from '@opentelemetry/api';
import { events } from '@opentelemetry/api-events';
import { EventLoggerProvider } from '@opentelemetry/sdk-events';
import { LoggerProvider,SimpleLogRecordProcessor,ConsoleLogRecordExporter } from '@opentelemetry/sdk-logs';
import { OTLPLogExporter } from '@opentelemetry/exporter-logs-otlp-http'
import { registerInstrumentations } from '@opentelemetry/instrumentation';
import { Resource } from '@opentelemetry/resources';
import { SEMRESATTRS_SERVICE_NAME } from '@opentelemetry/semantic-conventions';
import { PageViewInstrumentation } from "@opentelemetry/instrumentation-page-view";

const loggerProvider = new LoggerProvider({resource: new Resource({[SEMRESATTRS_SERVICE_NAME]: 'web-service-dl'})});

loggerProvider.addLogRecordProcessor(new SimpleLogRecordProcessor(new ConsoleLogRecordExporter()));
loggerProvider.addLogRecordProcessor(new SimpleLogRecordProcessor(new OTLPLogExporter()));
const eventLoggerProvider = new EventLoggerProvider(loggerProvider);
events.setGlobalEventLoggerProvider(eventLoggerProvider);

registerInstrumentations({
instrumentations: [
new PageViewInstrumentation({enabled:false})
],
});

// Define routes and their content
const routes = {
'/page-view/route1': '<h2>Welcome to Route 1</h2><p>This is the content for Route 1.</p>',
'/page-view/route2': '<h2>Welcome to Route 2</h2><p>This is the content for Route 2.</p>'
};

// Function to navigate to a route
function navigateTo(url) {
console.log('Navigating to', url);
history.pushState(null, null, url);
handleRouteChange();
}

// Function to handle the route change
function handleRouteChange() {
const path = window.location.pathname; // Get current path
const routeContent = routes[path] || `<h2>Not on Route </h2>`;
document.getElementById('content').innerHTML = routeContent;
}

// Attach event listeners to navigation links

const attachEventListenersToLinks = () => {
document.querySelectorAll('a[data-link]').forEach(link => {
console.log('attach event listener to link', link.href)
link.addEventListener('click', (event) => {
console.log('Link clicked', event.target.href)
event.preventDefault();
navigateTo(event.target.href);
});
});
}

window.addEventListener('popstate', handleRouteChange);
const loadTimeSetup = () => {
handleRouteChange();
attachEventListenersToLinks();
}
window.addEventListener('load', loadTimeSetup);
Loading