@dash0/sdk-web
Version:
Dash0's Web SDK to collect telemetry from end-users' web browsers
31 lines (30 loc) • 1.37 kB
JavaScript
import { EVENT_NAME, EVENT_NAMES, WEB_EVENT_TITLE, LOG_SEVERITIES } from "../semantic-conventions";
import { sendLog } from "../transport";
import { addCommonAttributes } from "../attributes";
import { addAttribute, toAnyValue } from "../utils/otel";
import { nowNanos, toNanosTs, warn } from "../utils";
/**
* Sends a custom event.
* @param name The event name. Can not be any of the internal event names. See: EVENT_NAMES in src/semantic-conventions.
* @param opts Additional event details.
*/
export function sendEvent(name, opts) {
if (Object.values(EVENT_NAMES).includes(name)) {
warn(`Unable to send custom event ${name}. You are not allowed to use an internal event name while sending a custom event. Dropping event...`);
return;
}
const attributes = [];
addCommonAttributes(attributes);
Object.entries(opts?.attributes ?? {}).forEach(([key, value]) => addAttribute(attributes, key, value));
addAttribute(attributes, EVENT_NAME, name);
if (opts?.title) {
addAttribute(attributes, WEB_EVENT_TITLE, opts.title);
}
sendLog({
timeUnixNano: opts?.timestamp != null ? toNanosTs(opts.timestamp) : nowNanos(),
attributes,
body: toAnyValue(opts?.data),
severityText: opts?.severity,
severityNumber: opts?.severity ? LOG_SEVERITIES[opts.severity] : undefined,
});
}