-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: (not working) experiment with otel-web
- Loading branch information
Nicolas Burtey
committed
Feb 10, 2024
1 parent
d3be4fd
commit 326d7a2
Showing
11 changed files
with
263 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Importing necessary modules from OpenTelemetry packages | ||
import { context, trace } from "@opentelemetry/api" | ||
import { ConsoleSpanExporter, SimpleSpanProcessor } from "@opentelemetry/sdk-trace-base" | ||
import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-http" | ||
import { WebTracerProvider } from "@opentelemetry/sdk-trace-web" | ||
import { FetchInstrumentation } from "@opentelemetry/instrumentation-fetch" | ||
import { ZoneContextManager } from "@opentelemetry/context-zone" | ||
import { B3Propagator } from "@opentelemetry/propagator-b3" | ||
import { registerInstrumentations } from "@opentelemetry/instrumentation" | ||
|
||
// Define the initialization function | ||
export default function initializeTelemetry() { | ||
const provider = new WebTracerProvider() | ||
|
||
provider.addSpanProcessor(new SimpleSpanProcessor(new OTLPTraceExporter())) | ||
provider.register({ | ||
contextManager: new ZoneContextManager(), | ||
propagator: new B3Propagator(), | ||
}) | ||
|
||
registerInstrumentations({ | ||
instrumentations: [ | ||
new FetchInstrumentation({ | ||
ignoreUrls: [/localhost:8090\/sockjs-node/], | ||
propagateTraceHeaderCorsUrls: [ | ||
"https://cors-test.appspot.com/test", | ||
"https://httpbin.org/get", | ||
], | ||
clearTimingResources: true, | ||
}), | ||
], | ||
}) | ||
|
||
// Example of how you might use the provider to create a tracer | ||
// This example function doesn't do anything by itself but is here to show how you might use it | ||
const webTracerWithZone = provider.getTracer("example-tracer-web") | ||
|
||
// Placeholder for any setup logic you might have | ||
console.log("OpenTelemetry initialized") | ||
} | ||
|
||
// Note: This file does not include the event listener setup from the original otel.js example, | ||
// as those are typically handled within the components or pages themselves in a React application. |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
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,72 @@ | ||
const { context, trace } = require( '@opentelemetry/api'); | ||
const { ConsoleSpanExporter, SimpleSpanProcessor } = require( '@opentelemetry/sdk-trace-base'); | ||
const { OTLPTraceExporter } = require( '@opentelemetry/exporter-trace-otlp-http'); | ||
const { WebTracerProvider } = require( '@opentelemetry/sdk-trace-web'); | ||
const { FetchInstrumentation } = require( '@opentelemetry/instrumentation-fetch'); | ||
const { ZoneContextManager } = require( '@opentelemetry/context-zone'); | ||
const { B3Propagator } = require( '@opentelemetry/propagator-b3'); | ||
const { registerInstrumentations } = require( '@opentelemetry/instrumentation'); | ||
|
||
const provider = new WebTracerProvider(); | ||
|
||
// Note: For production consider using the "BatchSpanProcessor" to reduce the number of requests | ||
// to your exporter. Using the SimpleSpanProcessor here as it sends the spans immediately to the | ||
// exporter without delay | ||
// provider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter())); | ||
provider.addSpanProcessor(new SimpleSpanProcessor(new OTLPTraceExporter())); | ||
provider.register({ | ||
contextManager: new ZoneContextManager(), | ||
propagator: new B3Propagator(), | ||
}); | ||
|
||
registerInstrumentations({ | ||
instrumentations: [ | ||
new FetchInstrumentation({ | ||
ignoreUrls: [/localhost:8090\/sockjs-node/], | ||
propagateTraceHeaderCorsUrls: [ | ||
'https://cors-test.appspot.com/test', | ||
'https://httpbin.org/get', | ||
], | ||
clearTimingResources: true, | ||
}), | ||
], | ||
}); | ||
|
||
const webTracerWithZone = provider.getTracer('example-tracer-web'); | ||
|
||
const getData = (url) => fetch(url, { | ||
method: 'GET', | ||
headers: { | ||
Accept: 'application/json', | ||
'Content-Type': 'application/json', | ||
}, | ||
}); | ||
|
||
// example of keeping track of context between async operations | ||
const prepareClickEvent = () => { | ||
const url = 'https://httpbin.org/get'; | ||
|
||
const element = document.getElementById('button1'); | ||
|
||
const onClick = () => { | ||
const singleSpan = webTracerWithZone.startSpan('files-series-info'); | ||
context.with(trace.setSpan(context.active(), singleSpan), () => { | ||
getData(url).then((_data) => { | ||
trace.getSpan(context.active()).addEvent('fetching-single-span-completed'); | ||
singleSpan.end(); | ||
}); | ||
}); | ||
for (let i = 0, j = 5; i < j; i += 1) { | ||
const span = webTracerWithZone.startSpan(`files-series-info-${i}`); | ||
context.with(trace.setSpan(context.active(), span), () => { | ||
getData(url).then((_data) => { | ||
trace.getSpan(context.active()).addEvent(`fetching-span-${i}-completed`); | ||
span.end(); | ||
}); | ||
}); | ||
} | ||
}; | ||
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
Oops, something went wrong.