1818namespace OpenCensus \Trace ;
1919
2020/**
21- * This plain PHP class represents a
22- * [TraceSpan resource](https://cloud.google.com/trace/docs/reference/v1/rest/v1/projects.traces#TraceSpan)
23- * A span represents a single timed event within a Trace. Spans can be nested
24- * and form a trace tree. Often, a trace contains a root span that describes
25- * the end-to-end latency of an operation and, optionally, one or more subspans
21+ * This plain PHP class represents a single timed event within a Trace. Spans can
22+ * be nested and form a trace tree. Often, a trace contains a root span that
23+ * describes the end-to-end latency of an operation and, optionally, one or more subspans
2624 * for its suboperations. Spans do not need to be contiguous. There may be
2725 * gaps between spans in a trace.
2826 */
29- class TraceSpan implements \JsonSerializable
27+ class TraceSpan
3028{
31- const SPAN_KIND_UNSPECIFIED = 'SPAN_KIND_UNSPECIFIED ' ;
32- const SPAN_KIND_RPC_SERVER = 'RPC_SERVER ' ;
33- const SPAN_KIND_RPC_CLIENT = 'RPC_CLIENT ' ;
34-
3529 /**
36- * @var array Associative array containing all the fields representing this TraceSpan .
30+ * @var array Associative array containing all the fields representing this Span .
3731 */
3832 private $ info = [];
3933
4034 /**
41- * Instantiate a new TraceSpan instance.
35+ * Instantiate a new Span instance.
4236 *
4337 * @param array $options [optional] {
4438 * Configuration options.
4539 *
46- * @type string $spanId The ID of the span. If not provided,
40+ * @type int $spanId The ID of the span. If not provided,
4741 * one will be generated automatically for you.
48- * @type string $kind Distinguishes between spans generated
49- * in a particular context. **Defaults to**
50- * SPAN_KIND_UNSPECIFIED.
5142 * @type string $name The name of the span.
5243 * @type \DateTimeInterface|int|float|string $startTime Start time of the span in nanoseconds.
5344 * If provided as a string, it must be in "Zulu" format. If provided as an int or float, it is
5445 * expected to be a Unix timestamp.
5546 * @type \DateTimeInterface|int|float|string $endTime End time of the span in nanoseconds.
5647 * If provided as a string, it must be in "Zulu" format. If provided as an int or float, it is
5748 * expected to be a Unix timestamp.
58- * @type string $parentSpanId ID of the parent span if any.
49+ * @type int $parentSpanId ID of the parent span if any.
5950 * @type array $labels Associative array of $label => $value
6051 * to attach to this span.
6152 * }
6253 */
6354 public function __construct ($ options = [])
6455 {
6556 if (array_key_exists ('startTime ' , $ options )) {
66- $ this ->setStart ($ options ['startTime ' ]);
57+ $ this ->setStartTime ($ options ['startTime ' ]);
58+ unset($ options ['startTime ' ]);
6759 }
6860 if (array_key_exists ('endTime ' , $ options )) {
69- $ this ->setEnd ($ options ['endTime ' ]);
61+ $ this ->setEndTime ($ options ['endTime ' ]);
62+ unset($ options ['endTime ' ]);
7063 }
7164
7265 if (array_key_exists ('labels ' , $ options )) {
7366 $ this ->addLabels ($ options ['labels ' ]);
67+ unset($ options ['labels ' ]);
7468 }
7569
7670 if (array_key_exists ('spanId ' , $ options )) {
7771 $ this ->info ['spanId ' ] = $ options ['spanId ' ];
72+ unset($ options ['spanId ' ]);
7873 } else {
7974 $ this ->info ['spanId ' ] = $ this ->generateSpanId ();
8075 }
8176
82- if (array_key_exists ('kind ' , $ options )) {
83- $ this ->info ['kind ' ] = $ options ['kind ' ];
84- } else {
85- $ this ->info ['kind ' ] = self ::SPAN_KIND_UNSPECIFIED ;
86- }
87-
8877 if (array_key_exists ('name ' , $ options )) {
8978 $ this ->info ['name ' ] = $ options ['name ' ];
79+ unset($ options ['name ' ]);
9080 } else {
9181 $ this ->info ['name ' ] = $ this ->generateSpanName ();
9282 }
9383
9484 if (array_key_exists ('parentSpanId ' , $ options )) {
9585 $ this ->info ['parentSpanId ' ] = $ options ['parentSpanId ' ];
86+ unset($ options ['parentSpanId ' ]);
9687 }
88+
89+ $ this ->info ['metadata ' ] = $ options ;
90+ }
91+
92+ /**
93+ * Retrieve the start time for this span.
94+ *
95+ * @return \DateTimeInterface
96+ */
97+ public function startTime ()
98+ {
99+ return $ this ->info ['startTime ' ];
97100 }
98101
99102 /**
@@ -103,27 +106,37 @@ public function __construct($options = [])
103106 * **Defaults to** now. If provided as a string, it must be in "Zulu" format.
104107 * If provided as an int or float, it is expected to be a Unix timestamp.
105108 */
106- public function setStart ($ when = null )
109+ public function setStartTime ($ when = null )
107110 {
108111 $ this ->info ['startTime ' ] = $ this ->formatDate ($ when );
109112 }
110113
114+ /**
115+ * Retrieve the end time for this span.
116+ *
117+ * @return \DateTimeInterface
118+ */
119+ public function endTime ()
120+ {
121+ return $ this ->info ['endTime ' ];
122+ }
123+
111124 /**
112125 * Set the end time for this span.
113126 *
114127 * @param \DateTimeInterface|int|float|string $when [optional] The end time of this span.
115128 * **Defaults to** now. If provided as a string, it must be in "Zulu" format.
116129 * If provided as an int or float, it is expected to be a Unix timestamp.
117130 */
118- public function setEnd ($ when = null )
131+ public function setEndTime ($ when = null )
119132 {
120133 $ this ->info ['endTime ' ] = $ this ->formatDate ($ when );
121134 }
122135
123136 /**
124137 * Retrieve the ID of this span.
125138 *
126- * @return string
139+ * @return int
127140 */
128141 public function spanId ()
129142 {
@@ -133,7 +146,7 @@ public function spanId()
133146 /**
134147 * Retrieve the ID of this span's parent if it exists.
135148 *
136- * @return string
149+ * @return int
137150 */
138151 public function parentSpanId ()
139152 {
@@ -162,16 +175,6 @@ public function info()
162175 return $ this ->info ;
163176 }
164177
165- /**
166- * Returns the info array for serialization.
167- *
168- * @return array
169- */
170- public function jsonSerialize ()
171- {
172- return $ this ->info ;
173- }
174-
175178 /**
176179 * Attach labels to this span.
177180 *
@@ -201,37 +204,38 @@ public function addLabel($label, $value)
201204 /**
202205 * Returns a "Zulu" formatted string representing the provided \DateTime.
203206 *
204- * @param \DateTimeInterface|int|float|string $when [optional] The end time of this span.
207+ * @param \DateTimeInterface|int|float $when [optional] The end time of this span.
205208 * **Defaults to** now. If provided as a string, it must be in "Zulu" format.
206209 * If provided as an int or float, it is expected to be a Unix timestamp.
207- * @return string
210+ * @return \DateTimeInterface
208211 */
209212 private function formatDate ($ when = null )
210213 {
211- if (is_string ($ when )) {
212- return $ when ;
213- } elseif (!$ when ) {
214+ if (!$ when ) {
215+ // now
214216 list ($ usec , $ sec ) = explode (' ' , microtime ());
215217 $ micro = sprintf ("%06d " , $ usec * 1000000 );
216218 $ when = new \DateTime (date ('Y-m-d H:i:s. ' . $ micro ));
217219 } elseif (is_numeric ($ when )) {
218220 // Expect that this is a timestamp
219221 $ micro = sprintf ("%06d " , ($ when - floor ($ when )) * 1000000 );
220222 $ when = new \DateTime (date ('Y-m-d H:i:s. ' . $ micro , (int ) $ when ));
223+ } elseif (!$ when instanceof \DateTimeInterface) {
224+ throw new \InvalidArgumentException ('Invalid date format. Must be a \DateTimeInterface or numeric. ' );
221225 }
222226 $ when ->setTimezone (new \DateTimeZone ('UTC ' ));
223- return $ when-> format ( ' Y-m-d\TH:i:s.u000\Z ' ) ;
227+ return $ when ;
224228 }
225229
226230 /**
227231 * Generate a random ID for this span. Must be unique per trace,
228232 * but does not need to be globally unique.
229233 *
230- * @return string
234+ * @return int
231235 */
232236 private function generateSpanId ()
233237 {
234- return '' . mt_rand ();
238+ return mt_rand ();
235239 }
236240
237241 /**
0 commit comments