|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2017 OpenCensus Authors |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +namespace OpenCensus\Tests\Unit\Trace\Tracer; |
| 19 | + |
| 20 | +use OpenCensus\Trace\Span; |
| 21 | +use OpenCensus\Trace\SpanContext; |
| 22 | + |
| 23 | +/** |
| 24 | + * @group trace |
| 25 | + */ |
| 26 | +abstract class AbstractTracerTest extends \PHPUnit_Framework_TestCase |
| 27 | +{ |
| 28 | + abstract protected function getTracerClass(); |
| 29 | + |
| 30 | + public function testMaintainsContext() |
| 31 | + { |
| 32 | + $class = $this->getTracerClass(); |
| 33 | + $parentSpanId = 12345; |
| 34 | + $initialContext = new SpanContext('traceid', $parentSpanId); |
| 35 | + $tracer = new $class($initialContext); |
| 36 | + $context = $tracer->spanContext(); |
| 37 | + |
| 38 | + $this->assertEquals('traceid', $context->traceId()); |
| 39 | + $this->assertEquals($parentSpanId, $context->spanId()); |
| 40 | + |
| 41 | + $tracer->inSpan(['name' => 'test'], function () use ($parentSpanId, $tracer) { |
| 42 | + $context = $tracer->spanContext(); |
| 43 | + $this->assertNotEquals($parentSpanId, $context->spanId()); |
| 44 | + }); |
| 45 | + |
| 46 | + $spans = $tracer->spans(); |
| 47 | + $this->assertCount(1, $spans); |
| 48 | + $span = $spans[0]; |
| 49 | + $this->assertEquals('test', $span->name()); |
| 50 | + $this->assertEquals($parentSpanId, $span->parentSpanId()); |
| 51 | + } |
| 52 | + |
| 53 | + public function testAddsAttributesToCurrentSpan() |
| 54 | + { |
| 55 | + $class = $this->getTracerClass(); |
| 56 | + $tracer = new $class(); |
| 57 | + $tracer->inSpan(['name' => 'root'], function () use ($tracer) { |
| 58 | + $tracer->inSpan(['name' => 'inner'], function () use ($tracer) { |
| 59 | + $tracer->addAttribute('foo', 'bar'); |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + $spans = $tracer->spans(); |
| 64 | + $this->assertCount(2, $spans); |
| 65 | + $span = $spans[1]; |
| 66 | + $this->assertEquals('inner', $span->name()); |
| 67 | + $attributes = $span->attributes(); |
| 68 | + $this->assertArrayHasKey('foo', $attributes); |
| 69 | + $this->assertEquals('bar', $attributes['foo']); |
| 70 | + } |
| 71 | + |
| 72 | + public function testAddsAttributesToRootSpan() |
| 73 | + { |
| 74 | + $class = $this->getTracerClass(); |
| 75 | + $tracer = new $class(); |
| 76 | + $tracer->inSpan(['name' => 'root'], function () use ($tracer) { |
| 77 | + $tracer->inSpan(['name' => 'inner'], function () use ($tracer) { |
| 78 | + $tracer->addRootAttribute('foo', 'bar'); |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | + $spans = $tracer->spans(); |
| 83 | + $this->assertCount(2, $spans); |
| 84 | + $span = $spans[0]; |
| 85 | + $this->assertEquals('root', $span->name()); |
| 86 | + $attributes = $span->attributes(); |
| 87 | + $this->assertArrayHasKey('foo', $attributes); |
| 88 | + $this->assertEquals('bar', $attributes['foo']); |
| 89 | + } |
| 90 | + |
| 91 | + public function testPersistsBacktrace() |
| 92 | + { |
| 93 | + $class = $this->getTracerClass(); |
| 94 | + $tracer = new $class(); |
| 95 | + $tracer->inSpan(['name' => 'test'], function () {}); |
| 96 | + $span = $tracer->spans()[0]; |
| 97 | + $stackframe = $span->stackTrace()[0]; |
| 98 | + $this->assertEquals('testPersistsBacktrace', $stackframe['function']); |
| 99 | + $this->assertEquals(self::class, $stackframe['class']); |
| 100 | + } |
| 101 | + |
| 102 | + public function testWithSpan() |
| 103 | + { |
| 104 | + $span = new Span(['name' => 'foo']); |
| 105 | + $class = $this->getTracerClass(); |
| 106 | + $tracer = new $class(); |
| 107 | + |
| 108 | + $this->assertNull($tracer->spanContext()->spanId()); |
| 109 | + $scope = $tracer->withSpan($span); |
| 110 | + $this->assertEquals($span->spanId(), $tracer->spanContext()->spanId()); |
| 111 | + $scope->close(); |
| 112 | + $this->assertNull($tracer->spanContext()->spanId()); |
| 113 | + } |
| 114 | + |
| 115 | + public function testSetStartTime() |
| 116 | + { |
| 117 | + $time = microtime(true) - 10; |
| 118 | + $span = new Span(['name' => 'foo', 'startTime' => $time]); |
| 119 | + $class = $this->getTracerClass(); |
| 120 | + $tracer = new $class(); |
| 121 | + $scope = $tracer->withSpan($span); |
| 122 | + usleep(100); |
| 123 | + $scope->close(); |
| 124 | + |
| 125 | + $this->assertEquivalentTimestamps($span->startTime(), $tracer->spans()[0]->startTime()); |
| 126 | + } |
| 127 | + |
| 128 | + private function assertEquivalentTimestamps($expected, $value) |
| 129 | + { |
| 130 | + $this->assertEquals((float)($expected->format('U.u')), (float)($expected->format('U.u')), '', 0.000001); |
| 131 | + } |
| 132 | +} |
0 commit comments