census-instrumentation / opencensus-web

A stats collection and distributed tracing framework

Home Page:https://opencensus.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Attaching user interaction spans and initial load span

rikonor opened this issue · comments

Is your feature request related to a problem? Please describe.
I'm encountering a use-case where it'd be handy if I could view both initial load data as well as custom spans together. Right now, any custom spans appear separately from the initial load data.

Describe the solution you'd like
I'd like to have some global span that gets started automatically via the initial load, but then requires manual closing. The initial page load span can live under that global span, and any user interaction or custom spans will live under that global span as well.

Describe alternatives you've considered
I've been digging through the code to see if it should already be possible to do this, but it was not able to find a solution.

May have figured a way to make this happen:

import { tracing } from "@opencensus/web-core";
import { OCAgentExporter } from "@opencensus/web-exporter-ocagent";
import {
  getPerfEntries,
  getInitialLoadRootSpan
} from "@opencensus/web-instrumentation-perf";

const TRACE_ENDPOINT = "/v1/trace";

tracing.registerExporter(
  new OCAgentExporter({
    agentEndpoint: `${window.ocAgent}${TRACE_ENDPOINT}`,
    serviceName: window.ocServiceName
  })
);

let rootSpan = getInitialLoadRootSpan(tracing.tracer, getPerfEntries());

setTimeout(() => {
  rootSpan.end();
}, 5000);

setTimeout(() => {
  let childSpan = tracing.tracer.startChildSpan({
    name: "child-1",
    childOf: rootSpan
  });

  setTimeout(() => {
    childSpan.end();
  }, 500);
}, 1000);

Seems to allow exporting initial page load as well as custom events under the same root.