Page Menu
Home
WickedGov Phorge
Search
Configure Global Search
Log In
Files
F1426221
Tracer.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
2 KB
Referenced Files
None
Subscribers
None
Tracer.php
View Options
<?php
namespace
Wikimedia\Telemetry
;
use
Wikimedia\Assert\Assert
;
/**
* @since 1.43
* @internal
*/
class
Tracer
implements
TracerInterface
{
/**
* The length of a trace ID in bytes, as specified by the OTEL specification.
*/
private
const
TRACE_ID_BYTES_LENGTH
=
16
;
/**
* The length of a span ID in bytes, as specified by the OTEL specification.
*/
private
const
SPAN_ID_BYTES_LENGTH
=
8
;
private
Clock
$clock
;
private
SamplerInterface
$sampler
;
private
ExporterInterface
$exporter
;
private
TracerState
$tracerState
;
/**
* Whether tracing has been explicitly ended by calling shutdown() on this instance.
* @var bool
*/
private
bool
$wasShutdown
=
false
;
public
function
__construct
(
Clock
$clock
,
SamplerInterface
$sampler
,
ExporterInterface
$exporter
,
TracerState
$tracerState
)
{
$this
->
clock
=
$clock
;
$this
->
sampler
=
$sampler
;
$this
->
exporter
=
$exporter
;
$this
->
tracerState
=
$tracerState
;
}
/** @inheritDoc */
public
function
createSpan
(
string
$spanName
):
SpanInterface
{
$activeSpanContext
=
$this
->
tracerState
->
getActiveSpanContext
();
// Gracefully handle attempts to instrument code after shutdown() was called.
if
(
!
$this
->
wasShutdown
)
{
Assert
::
precondition
(
$activeSpanContext
!==
null
,
'Attempted to create a span with the currently active span as the implicit parent, '
.
'but no span was active. Use createRootSpan() to create a span with no parent (i.e. a root span).'
);
}
return
$this
->
newSpan
(
$spanName
,
$activeSpanContext
);
}
/** @inheritDoc */
public
function
createRootSpan
(
string
$spanName
):
SpanInterface
{
return
$this
->
newSpan
(
$spanName
,
null
);
}
/** @inheritDoc */
public
function
createSpanWithParent
(
string
$spanName
,
SpanContext
$parentSpanContext
):
SpanInterface
{
return
$this
->
newSpan
(
$spanName
,
$parentSpanContext
);
}
private
function
newSpan
(
string
$spanName
,
?
SpanContext
$parentSpanContext
):
SpanInterface
{
$traceId
=
$parentSpanContext
!==
null
?
$parentSpanContext
->
getTraceId
()
:
$this
->
generateId
(
self
::
TRACE_ID_BYTES_LENGTH
);
$spanId
=
$this
->
generateId
(
self
::
SPAN_ID_BYTES_LENGTH
);
$sampled
=
$this
->
sampler
->
shouldSample
(
$parentSpanContext
);
$spanContext
=
new
SpanContext
(
$traceId
,
$spanId
,
$parentSpanContext
!==
null
?
$parentSpanContext
->
getSpanId
()
:
null
,
$spanName
,
$sampled
);
if
(
$this
->
wasShutdown
||
!
$sampled
)
{
return
new
NoopSpan
(
$spanContext
);
}
return
new
Span
(
$this
->
clock
,
$this
->
tracerState
,
$spanContext
);
}
/** @inheritDoc */
public
function
shutdown
():
void
{
$this
->
wasShutdown
=
true
;
$this
->
exporter
->
export
(
$this
->
tracerState
);
}
/**
* Generate a valid hexadecimal string for use as a trace or span ID, with the given length in bytes.
*
* @param int $bytesLength The byte length of the ID
* @return string The ID as a hexadecimal string
*/
private
function
generateId
(
int
$bytesLength
):
string
{
return
bin2hex
(
random_bytes
(
$bytesLength
)
);
}
}
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Sat, May 16, 12:50 (1 d, 5 h)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
68/3f/2ea6c79e81891a10b74eeead7f47
Default Alt Text
Tracer.php (2 KB)
Attached To
Mode
rMWPROD MediaWiki Production
Attached
Detach File
Event Timeline
Log In to Comment