Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add example of how to create a new trace in same process #650

Merged
merged 1 commit into from
Apr 2, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions examples/CreatingANewTraceInTheSameProcess.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);
require __DIR__ . '/../vendor/autoload.php';

use OpenTelemetry\SDK\Trace\SpanExporter\ConsoleSpanExporter;
use OpenTelemetry\SDK\Trace\SpanProcessor\SimpleSpanProcessor;
use OpenTelemetry\SDK\Trace\TracerProvider;

// Boilerplate setup to create a new tracer with console output
$tracer = (new TracerProvider(
new SimpleSpanProcessor(
new ConsoleSpanExporter()
)
))->getTracer();

// This creates a span and sets it as the current parent (and root) span
$rootSpan = $tracer->spanBuilder('foo')->startSpan();
$rootScope = $rootSpan->activate();

// This creates (and closes) a child span
$childSpan = $tracer->spanBuilder('bar')->startSpan();
$childSpan->end();

// This closes the root/parent span and detaches its scope/context
$rootSpan->end();
$rootScope->detach();

// This creates a new span as a parent/root, however regardless of calling "activate" on it, it will have a new TraceId
$span = $tracer->spanBuilder('baz')->startSpan();
$span->activate();

$span->end();