Skip to content

Commit

Permalink
docs(user-interaction): update docs to include examples of config opt…
Browse files Browse the repository at this point in the history
…ions (open-telemetry#1840)

* docs(user-interaction): update docs to include examples of config options

* add example to prevent span creation

* fix lint

* address PR comments

* update link to list of events

* syntax updates

Co-authored-by: Martin Kuba <[email protected]>

---------

Co-authored-by: Martin Kuba <[email protected]>
  • Loading branch information
pkanal and martinkuba authored Dec 4, 2023
1 parent fb80783 commit 1c24cfd
Showing 1 changed file with 63 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ npm install --save @opentelemetry/instrumentation-user-interaction

## Usage

### Initialize

```js
import { ConsoleSpanExporter, SimpleSpanProcessor } from '@opentelemetry/sdk-trace-base';
import { WebTracerProvider } from '@opentelemetry/sdk-trace-web';
Expand Down Expand Up @@ -82,6 +84,67 @@ function getData(url) {

```

### Send spans for different events

By default, only `click` events are automatically instrumented. To automatically instrument other events, specify the events that should be captured for telemetry. Most [HTMLElement interface events](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement#events) are supported.

```js
import { UserInteractionInstrumentation } from '@opentelemetry/instrumentation-user-interaction';
import { registerInstrumentations } from '@opentelemetry/instrumentation';

// ...general opentelemetry configuration

registerInstrumentations({
instrumentations: [
new UserInteractionInstrumentation({
eventNames: ['submit', 'click', 'keypress'],
}),
],
});
```

### Prevent spans from recording

```js
import { UserInteractionInstrumentation } from '@opentelemetryinstrumentation-user-interaction';
import { registerInstrumentations } from '@opentelemetry/instrumentation';


// ...general opentelemetry configuration

registerInstrumentations({
instrumentations: [
new UserInteractionInstrumentation({
shouldPreventSpanCreation: () => {
return true;
},
}),
],
});
```

### Add extra attributes to spans

To attach extra attributes to user interaction spans, provide a callback function to the `shouldPreventSpanCreation` option:

```js
import { UserInteractionInstrumentation } from '@opentelemetryinstrumentation-user-interaction';
import { registerInstrumentations } from '@opentelemetry/instrumentation';

// ...general opentelemetry configuration

registerInstrumentations({
instrumentations: [
new UserInteractionInstrumentation({
shouldPreventSpanCreation: (event, element, span) => {
span.setAttribute('target.id', element.id);
// etc..
}
}),
],
});
```

## Example Screenshots

![Screenshot of the running example](images/main.jpg)
Expand Down

0 comments on commit 1c24cfd

Please sign in to comment.