npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@sophonz/span-processors

v0.0.6

Published

Readme

@sophonz/span-processors

Span and log record processors for the Sophonz browser agents.

한국어

Two things the OpenTelemetry defaults leave to you in a browser: getting a batch flushed before the page goes away, and stamping every span with attributes that are global to the session rather than to one call site. These processors do both.

Part of the Sophonz OpenTelemetry suite.

Install

bun add @sophonz/span-processors
# or
pnpm add @sophonz/span-processors
# or
npm install @sophonz/span-processors

@sophonz/browser-sdk and @sophonz/otel-web already install these. Reach for the package directly only when you assemble the SDK yourself.

Usage

import {
  SophonzBatchSpanProcessor,
  SophonzSpanAttributesProcessor,
  SophonzLogAttributesProcessor,
} from '@sophonz/span-processors';

const globalAttributes = { 'user.id': 'u-123' };

provider.addSpanProcessor(new SophonzSpanAttributesProcessor(globalAttributes));
provider.addSpanProcessor(new SophonzBatchSpanProcessor(exporter));

API

SophonzBatchSpanProcessor

Extends OpenTelemetry's BatchSpanProcessor.

new SophonzBatchSpanProcessor(exporter, config?, meter?)
  • exporter (SpanExporter) — where batches go.
  • config (BatchSpanProcessorBrowserConfig, optional) — the upstream batch options.
  • meter (Meter, optional) — when given, every span also records its duration in milliseconds to a durationInMs gauge, tagged with the span's attributes plus span.id and trace.id.

It flushes on visibilitychange (when the document becomes hidden) and on pagehide, which is the Safari fallback since visibility change is unreliable there. Upstream does the same but lets a rejected forceFlush escape as an unhandled rejection; this one routes it to globalErrorHandler. Set disableAutoFlushOnDocumentHide: true in the config to turn the listeners off.

SophonzSpanAttributesProcessor

Implements SpanProcessor. On every span start it sets location.href, applies the current global attributes, and adds the resource attributes from @sophonz/meta.

new SophonzSpanAttributesProcessor(globalAttributes)
  • setGlobalAttributes(attributes?) — merges into the current set. Called with no argument, it clears them.
  • getGlobalAttributes() — returns the live object.

Attributes are applied at onStart, so a change takes effect for spans started afterwards and leaves spans already in flight alone.

SophonzLogAttributesProcessor

Implements LogRecordProcessor with the same setGlobalAttributes / getGlobalAttributes pair, so logs carry the same global attributes as spans.

SophonzSpanAttributeScrubbingProcessor

Implements SpanProcessor. Applies the customer's attributeScrubbers rules — per-key redaction from @sophonz/redaction — to every span before it is exported.

new SophonzSpanAttributeScrubbingProcessor(scrubbers?)

Constructed with no rules (or an empty array) it is a no-op: spans are not iterated, not copied and not touched. Rules are documented in the @sophonz/redaction README; the SDK exposes them as the attributeScrubbers option.

The work happens in onEnd, not onStart. Almost every attribute an instrumentation sets — http.response.status_code, exception.message, the console instrumentation's object spread — is written during the span's life, so a scrubber running at start would see an essentially empty bag. onEnd is the last synchronous point at which every attribute exists and the span has not yet been queued for export.

It mutates span.attributes directly, which is what the SDK actually permits here. onEnd receives a ReadableSpan, whose attributes is a readonly property holding an ordinary mutable object — the reference cannot be reassigned, the contents can. span.setAttribute() is not an alternative even where the type allows it: SpanImpl.setAttribute returns early once the span has ended, and cannot express deletion. One documented cost: SpanImpl keeps a private _attributesCount that cannot be updated from outside, so removing an attribute leaves it one too high. It is only read when adding an attribute against attributeCountLimit, and the span has already ended.

Span event attributes are scrubbed too, which is where exception.message and exception.stacktrace live.

Place it after the attribute-writing processors and before any exporting processor. MultiSpanProcessor calls onEnd in array order, so array position is the ordering.

A customer's scrubber never throws into the page; failures are reported through diag.warn (visible only under debug: true, matching the rest of this SDK) and the attribute fails closed. See the @sophonz/redaction README for the exact semantics.

Dependencies

  • @opentelemetry/api, @opentelemetry/core, @opentelemetry/sdk-trace-base, @opentelemetry/sdk-logs
  • @sophonz/meta — resource attributes applied on span start
  • @sophonz/redaction — the compiled attribute scrubber
  • @sophonz/semantic-conventions

License

See LICENSE in this package.


Part of sophonz-js.