diff --git a/composer.json b/composer.json index c0814d9bf2..b11ab618a8 100644 --- a/composer.json +++ b/composer.json @@ -42,10 +42,7 @@ "autoload": { "psr-4": { "DDTrace\\": "./src/api/" - }, - "files": [ - "./src/api/ComposerBootstrap.php" - ] + } }, "autoload-dev": { "psr-4": { diff --git a/src/api/ComposerBootstrap.php b/src/api/ComposerBootstrap.php deleted file mode 100644 index 7482191d5f..0000000000 --- a/src/api/ComposerBootstrap.php +++ /dev/null @@ -1,9 +0,0 @@ -boolValue('trace.enabled', true); - } - - /** - * Whether or not debug mode is enabled. - * - * @return bool - */ - public function isDebugModeEnabled() - { - return $this->boolValue('trace.debug', false); - } - - /** - * Whether or not distributed tracing is enabled globally. - * - * @return bool - */ - public function isDistributedTracingEnabled() - { - return $this->boolValue('distributed.tracing', true); - } - - /** - * Whether or not automatic trace analytics configuration is enabled. - * - * @return bool - */ - public function isAnalyticsEnabled() - { - return $this->boolValue('trace.analytics.enabled', false); - } - - /** - * Whether or not priority sampling is enabled globally. - * - * @return bool - */ - public function isPrioritySamplingEnabled() - { - return $this->isDistributedTracingEnabled() - && $this->boolValue('priority.sampling', true); - } - - /** - * Whether or not also unfinished spans should be finished (and thus sent) when tracer is flushed. - * Motivation: We had users reporting that in some cases they have manual end-points that `echo` some content and - * then just `exit(0)` at the end of action's method. While the shutdown hook that flushes traces would still be - * called, many spans would be unfinished and thus discarded. With this option enabled spans are automatically - * finished (if not finished yet) when the tracer is flushed. - * - * @return bool - */ - public function isAutofinishSpansEnabled() - { - return $this->boolValue('autofinish.spans', false); - } - - /** - * Returns the sampling rate provided by the user. Default: 1.0 (keep all). - * - * @return float - */ - public function getSamplingRate() - { - $value = $this->floatValue('trace.sample.rate', 1.0); - if ($value < 0) { - $value = 1; - } else { - $value = min(1, max(0, $value)); - } - return $value; - } - - /** - * Returns the sampling rules defined for the current service. - * Results are cached so it is perfectly fine to call this method multiple times. - * The expected format for sampling rule env variable is: - * - example: DD_TRACE_SAMPLING_RULES=[] - * --> sample rate is 100% - * - example: DD_TRACE_SAMPLING_RULES=[{"sample_rate": 0.2}] - * --> sample rate is 20% - * - example: DD_TRACE_SAMPLING_RULES=[{"service": "a.*", "name": "b", "sample_rate": 0.1}, {"sample_rate": 0.2}] - * --> sample rate is 20% except for spans of service starting with 'a' and with name 'b' where rate is 10% - * - * Note that 'service' and 'name' is optional when when omitted the '*' pattern is assumed. - * - * @return array - */ - public function getSamplingRules() - { - if (null !== $this->samplingRulesCache) { - return $this->samplingRulesCache; - } - - $this->samplingRulesCache = []; - - $parsed = \json_decode($this->stringValue('trace.sampling.rules'), true); - if (false === $parsed) { - $parsed = []; - } - - // We do a proper parsing here to make sure that once the sampling rules leave this method - // they are always properly defined. - if (is_array($parsed)) { - foreach ($parsed as $rule) { - if (!is_array($rule) || !isset($rule['sample_rate'])) { - continue; - } - $service = isset($rule['service']) ? strval($rule['service']) : '.*'; - $name = isset($rule['name']) ? strval($rule['name']) : '.*'; - $rate = isset($rule['sample_rate']) ? floatval($rule['sample_rate']) : 1.0; - $this->samplingRulesCache[] = [ - 'service' => $service, - 'name' => $name, - 'sample_rate' => $rate, - ]; - } - } - - return $this->samplingRulesCache; - } - - /** - * Whether or not a specific integration is enabled. - * - * @param string $name - * @return bool - */ - public function isIntegrationEnabled($name) - { - if (function_exists('ddtrace_config_integration_enabled')) { - return \ddtrace_config_integration_enabled($name); - } - return $this->isEnabled() && !$this->inArray('integrations.disabled', $name); - } - - /** - * Returns the global tags to be set on all spans. - */ - public function getGlobalTags() - { - return $this->associativeStringArrayValue('tags') ?: $this->associativeStringArrayValue('trace.global.tags'); - } - - /** - * Returns the service mapping. - */ - public function getServiceMapping() - { - // We use the format 'service.mapping' instead of 'trace.service.mapping' for consistency - // with java naming pattern for this very same config: DD_SERVICE_MAPPING - return $this->associativeStringArrayValue('service.mapping'); - } - - /** - * Append hostname as a root span tag - * - * @return bool - */ - public function isHostnameReportingEnabled() - { - return $this->boolValue('trace.report.hostname', false); - } - - /** - * Use normalized URL as resource name - * - * @return bool - */ - public function isURLAsResourceNameEnabled() - { - return $this->boolValue('trace.url.as.resource.names.enabled', true); - } - - /** - * Set URL hostname as service name - * - * @return bool - */ - public function isHttpClientSplitByDomain() - { - return $this->boolValue('trace.http.client.split.by.domain', false); - } - - /** - * The name of the application. - * - * @param string $default - * @return string - */ - public function appName($default = '') - { - $service = $this->stringValue('service', null); - $service = $service ?: $this->stringValue('service.name', null); - $service = $service ?: $default; - return $service; - } - - /** - * The version of the application. - * A return value of null indicates the version is not set. - * - * @return string|null - */ - public function getServiceVersion() - { - // Using the env `DD_VERSION` for consistency with other tracers. - return $this->stringValue('version', null); - } - - /** - * The environment for the application. - * A return value of null indicates the env is not set. - * - * @return string|null - */ - public function getEnv() - { - // Using the env `DD_ENV` for consistency with other tracers. - return $this->stringValue('env', null); - } -} diff --git a/src/api/Configuration/AbstractConfiguration.php b/src/api/Configuration/AbstractConfiguration.php deleted file mode 100644 index 0c4a85410b..0000000000 --- a/src/api/Configuration/AbstractConfiguration.php +++ /dev/null @@ -1,104 +0,0 @@ -registry = new EnvVariableRegistry(); - } - - /** - * Returns the singleton configuration instance. - * - * @return static - */ - public static function get() - { - if (self::$instance === null) { - self::$instance = new static(); - } - - return self::$instance; - } - - /** - * {@inheritdoc} - */ - public function stringValue($key, $default = '') - { - return $this->registry->stringValue($key, $default); - } - - /** - * {@inheritdoc} - * - * Allows users to access configuration properties by name instead of calling explicit methods. - */ - public function boolValue($key, $default) - { - return $this->registry->boolValue($key, $default); - } - - /** - * {@inheritdoc} - * - * Returns a floating point value from the registry. - */ - public function floatValue($key, $default, $min = null, $max = null) - { - return $this->registry->floatValue($key, $default, $min, $max); - } - - /** - * {@inheritdoc} - * - * Reads a configuration property into an associative string => string array. - */ - public function associativeStringArrayValue($key) - { - return $this->registry->associativeStringArrayValue($key); - } - - /** - * {@inheritdoc} - * - * Allows users to access configuration properties by name instead of calling explicit methods. - */ - public function inArray($key, $name) - { - return $this->registry->inArray($key, $name); - } - - /** - * Replaces the singleton instance. - * - * @param AbstractConfiguration $configuration - */ - public static function replace(AbstractConfiguration $configuration) - { - self::$instance = $configuration; - } - - /** - * Clear the singleton instance. - */ - public static function clear() - { - self::$instance = null; - } -} diff --git a/src/api/Configuration/EnvVariableRegistry.php b/src/api/Configuration/EnvVariableRegistry.php deleted file mode 100644 index 9bcb6d7604..0000000000 --- a/src/api/Configuration/EnvVariableRegistry.php +++ /dev/null @@ -1,204 +0,0 @@ -prefix = $prefix; - $this->registry = []; - } - - private function readEnvOrIni($name) - { - $ini_name = strtolower(strtr($name, [ - "DD_TRACE_" => "datadog.trace.", - "DD_" => "datadog.", - ])); - $ini = ini_get($ini_name); - if ($ini !== false) { - return $ini; - } - return \getenv($name); - } - - /** - * Return an env variable that starts with "DD_". - * - * @param string $key - * @return string|null - */ - protected function get($key) - { - $value = $this->readEnvOrIni($this->convertKeyToEnvVariableName($key)); - if (false === $value) { - return null; - } - return trim($value); - } - - /** - * {@inheritdoc} - */ - public function stringValue($key, $default) - { - if (isset($this->registry[$key])) { - return $this->registry[$key]; - } - $value = $this->get($key); - if (null !== $value) { - return $this->registry[$key] = $value; - } else { - return $this->registry[$key] = $default; - } - return $this->registry[$key]; - } - - /** - * {@inheritdoc} - */ - public function boolValue($key, $default) - { - if (isset($this->registry[$key])) { - return $this->registry[$key]; - } - - $value = $this->get($key); - if (null === $value) { - return $default; - } - - $value = strtolower($value); - if ($value === '1' || $value === 'true') { - $this->registry[$key] = true; - } elseif ($value === '0' || $value === 'false') { - $this->registry[$key] = false; - } else { - $this->registry[$key] = $default; - } - - return $this->registry[$key]; - } - - /** - * {@inheritdoc} - */ - public function floatValue($key, $default, $min = null, $max = null) - { - if (!isset($this->registry[$key])) { - $value = $this->get($key); - $value = $value === null ? "" : strtolower($value); - if (is_numeric($value)) { - $floatValue = (float)$value; - } else { - $floatValue = (float)$default; - } - - if (null !== $min && $floatValue < $min) { - $floatValue = $min; - } - - if (null !== $max && $floatValue > $max) { - $floatValue = $max; - } - - $this->registry[$key] = $floatValue; - } - - return $this->registry[$key]; - } - - /** - * Given a string like 'key1:value1,key2:value2', it returns an associative array - * ['key1'=> 'value1', 'key2'=> 'value2'] - * - * @param string $key - * @return string[] - */ - public function associativeStringArrayValue($key) - { - if (isset($this->registry[$key])) { - return $this->registry[$key]; - } - - $default = []; - $value = $this->get($key); - - if (null === $value) { - return $default; - } - - // For now we provide no escaping - $this->registry[$key] = []; - $elements = explode(',', $value); - foreach ($elements as $element) { - $keyAndValue = explode(':', $element); - - if (count($keyAndValue) !== 2) { - continue; - } - - $keyFragment = trim($keyAndValue[0]); - $valueFragment = trim($keyAndValue[1]); - - if (empty($keyFragment)) { - continue; - } - - $this->registry[$key][$keyFragment] = $valueFragment; - } - - return $this->registry[$key]; - } - - /** - * {@inheritdoc} - */ - public function inArray($key, $name) - { - if (!isset($this->registry[$key])) { - $value = $this->get($key); - if (null !== $value) { - $disabledIntegrations = explode(',', $value); - $this->registry[$key] = array_map(function ($entry) { - return strtolower(trim($entry)); - }, $disabledIntegrations); - } else { - $this->registry[$key] = []; - } - } - - return in_array(strtolower($name), $this->registry[$key], true); - } - - /** - * Given a dot separated key, it converts it to an expected variable name. - * - * e.g.: 'distributed_tracing' -> 'DD_DISTRIBUTED_TRACING' - * - * @param string $key - * @return string - */ - private function convertKeyToEnvVariableName($key) - { - return $this->prefix . strtoupper(str_replace('.', '_', trim($key))); - } -} diff --git a/src/api/Configuration/Registry.php b/src/api/Configuration/Registry.php deleted file mode 100644 index eafa80d9f8..0000000000 --- a/src/api/Configuration/Registry.php +++ /dev/null @@ -1,56 +0,0 @@ - 'value1', 'key2'=> 'value2'] - * - * @param string $key - * @return string[] - */ - public function associativeStringArrayValue($key); - - /** - * Returns whether or not a given case-insensitive name is contained in a configuration property. - * - * @param string $key - * @param string $name - * @return bool - */ - public function inArray($key, $name); -} diff --git a/src/api/Data/Span.php b/src/api/Data/Span.php index abbeb90515..0b3835effa 100644 --- a/src/api/Data/Span.php +++ b/src/api/Data/Span.php @@ -2,7 +2,6 @@ namespace DDTrace\Data; -use DDTrace\Time; use DDTrace\Data\SpanContext as SpanContextData; use DDTrace\Contracts\Span as SpanInterface; diff --git a/tests/Composer/app/index.php b/tests/Composer/app/index.php index e7e4ec22de..2f747e0d4c 100644 --- a/tests/Composer/app/index.php +++ b/tests/Composer/app/index.php @@ -38,11 +38,6 @@ $span->setTag(Tag::SPAN_TYPE, Type::MEMCACHED); $scope->close(); - /* - * Using Configuration class from 'api' (which no longer exists in the 'src') - */ - Configuration::get()->appName('default'); - /* * Using Logger class which is defined in both 'src' AND 'api' */ diff --git a/tests/OpenTracer1Unit/TracerTest.php b/tests/OpenTracer1Unit/TracerTest.php index c3195f8198..c192e6077d 100644 --- a/tests/OpenTracer1Unit/TracerTest.php +++ b/tests/OpenTracer1Unit/TracerTest.php @@ -9,7 +9,6 @@ use DDTrace\Tag; use DDTrace\Tests\DebugTransport; use DDTrace\Tests\Common\BaseTestCase; -use DDTrace\Time; use DDTrace\Transport\Noop as NoopTransport; use OpenTracing\GlobalTracer; use OpenTracing\Formats; @@ -85,7 +84,7 @@ public function testCreateSpanWithExpectedValues() { $tracer = Tracer::make(new NoopTransport()); $rootSpan = $tracer->startSpan("rootSpan"); - $startTime = Time::now(); + $startTime = 12345; $span = $tracer ->startSpan(self::OPERATION_NAME, [ 'tags' => [ diff --git a/tests/OpenTracerUnit/SpanTest.php b/tests/OpenTracerUnit/SpanTest.php index afbfec02f7..83ce510036 100644 --- a/tests/OpenTracerUnit/SpanTest.php +++ b/tests/OpenTracerUnit/SpanTest.php @@ -4,11 +4,9 @@ use DDTrace\OpenTracer\Span; use DDTrace\Span as DDSpan; -use DDTrace\SpanData; use DDTrace\SpanContext as DDSpanContext; use DDTrace\Tag; use DDTrace\Tests\Common\BaseTestCase; -use DDTrace\Time; use Exception; final class SpanTest extends BaseTestCase diff --git a/tests/OpenTracerUnit/TracerTest.php b/tests/OpenTracerUnit/TracerTest.php index c221a6d15e..803baa1018 100644 --- a/tests/OpenTracerUnit/TracerTest.php +++ b/tests/OpenTracerUnit/TracerTest.php @@ -9,7 +9,6 @@ use DDTrace\Tag; use DDTrace\Tests\DebugTransport; use DDTrace\Tests\Common\BaseTestCase; -use DDTrace\Time; use DDTrace\Transport\Noop as NoopTransport; use OpenTracing\GlobalTracer; use OpenTracing\Formats; @@ -91,7 +90,7 @@ public function testCreateSpanWithEnvAndVersionPrecedence() public function testCreateSpanWithExpectedValues() { $tracer = Tracer::make(new NoopTransport()); - $startTime = Time::now(); + $startTime = 12345; $span = $tracer ->startSpan(self::OPERATION_NAME, [ 'tags' => [ diff --git a/tests/Unit/TimeTest.php b/tests/Unit/TimeTest.php deleted file mode 100644 index 87e6140c1e..0000000000 --- a/tests/Unit/TimeTest.php +++ /dev/null @@ -1,36 +0,0 @@ -assertEquals(16, strlen((string) $now)); - } - - /** - * @dataProvider microtimeProvider - */ - public function testIsValidHasTheExpectedOutput($microtime, $isValid) - { - $this->assertEquals($isValid, Time::isValid($microtime)); - } - - public function microtimeProvider() - { - return [ - [Time::now(), true], - [1234567890123456, true], - [123456789012345, false], - ['1234567890123456', false], - ['123456789012345', false], - ['123456789012345a', false], - ['abcdefgh12345678', false] - ]; - } -} diff --git a/tests/Unit/TracerTest.php b/tests/Unit/TracerTest.php index fb18059982..3494550853 100644 --- a/tests/Unit/TracerTest.php +++ b/tests/Unit/TracerTest.php @@ -7,7 +7,6 @@ use DDTrace\SpanContext; use DDTrace\Tag; use DDTrace\Tests\DebugTransport; -use DDTrace\Time; use DDTrace\Tracer; use DDTrace\Transport\Noop as NoopTransport; use DDTrace\Tests\Common\BaseTestCase; @@ -52,7 +51,7 @@ public function testCreateSpanSuccessWithExpectedValues() { $tracer = new Tracer(new NoopTransport()); $tracer->startRootSpan('foo'); // setting start_time not allowed on internal root span - $startTime = Time::now(); + $startTime = 12345; $span = $tracer->startSpan(self::OPERATION_NAME, [ 'tags' => [ self::TAG_KEY => self::TAG_VALUE diff --git a/tests/api/Unit/Configuration/EnvVariableRegistryTest.php b/tests/api/Unit/Configuration/EnvVariableRegistryTest.php deleted file mode 100644 index ef21eebe79..0000000000 --- a/tests/api/Unit/Configuration/EnvVariableRegistryTest.php +++ /dev/null @@ -1,196 +0,0 @@ -assertSame('bar', $registry->stringValue('some.test.parameter', 'foo')); - } - - public function testPrefixCanBeCustomized() - { - self::putenv('DD_CUSTOM_PREFIX_SOME_TEST_PARAMETER=bar'); - $registry = new EnvVariableRegistry('DD_CUSTOM_PREFIX_'); - $this->assertSame('bar', $registry->stringValue('some.test.parameter', 'foo')); - } - - public function testStringFromEnv() - { - self::putenv('DD_SOME_TEST_PARAMETER=bar'); - $registry = new EnvVariableRegistry(); - $this->assertSame('bar', $registry->stringValue('some.test.parameter', 'foo')); - } - - public function testStringWillFallbackToDefault() - { - $registry = new EnvVariableRegistry(); - $this->assertSame('foo', $registry->stringValue('some.test.parameter', 'foo')); - } - - public function testTrueValueWhenEnvNotSet() - { - $registry = new EnvVariableRegistry(); - $this->assertTrue($registry->boolValue('some.test.parameter', true)); - } - - public function testFalseValueWhenEnvNotSet() - { - $registry = new EnvVariableRegistry(); - $this->assertFalse($registry->boolValue('some.test.parameter', false)); - } - - public function testBoolValueTrueEnvSetWord() - { - $registry = new EnvVariableRegistry(); - self::putenv('DD_SOME_TEST_PARAMETER=tRuE '); - $this->assertTrue($registry->boolValue('some.test.parameter', false)); - } - - public function testBoolValueTrueEnvSetNumber() - { - $registry = new EnvVariableRegistry(); - self::putenv('DD_SOME_TEST_PARAMETER=1 '); - $this->assertTrue($registry->boolValue('some.test.parameter', false)); - } - - public function testBoolValueFalseEnvSetWord() - { - $registry = new EnvVariableRegistry(); - self::putenv('DD_SOME_TEST_PARAMETER=fAlSe '); - $this->assertFalse($registry->boolValue('some.test.parameter', true)); - } - - public function testBoolValueFalseEnvSetNumber() - { - $registry = new EnvVariableRegistry(); - self::putenv('DD_SOME_TEST_PARAMETER=0 '); - $this->assertFalse($registry->boolValue('some.test.parameter', true)); - } - - public function testFloatValueProvided() - { - $registry = new EnvVariableRegistry(); - self::putenv('DD_SOME_TEST_PARAMETER=0.7 '); - $this->assertSame(0.7, $registry->floatValue('some.test.parameter', 1)); - } - - public function testFloatValueNot() - { - $registry = new EnvVariableRegistry(); - $this->assertSame(1.0, $registry->floatValue('some.test.parameter', 1)); - } - - public function testFloatValueAlwaysConvertedToFloat() - { - $registry = new EnvVariableRegistry(); - self::putenv('DD_SOME_TEST_PARAMETER=1 '); - $this->assertEquals(1.0, $registry->floatValue('some.test.parameter', 1)); - } - - public function testFloatValueOverMax() - { - $registry = new EnvVariableRegistry(); - self::putenv('DD_SOME_TEST_PARAMETER=10000 '); - $this->assertEquals(1.0, $registry->floatValue('some.test.parameter', 1, 0, 1)); - } - - public function testFloatValueBelowMin() - { - $registry = new EnvVariableRegistry(); - self::putenv('DD_SOME_TEST_PARAMETER=0 '); - $this->assertEquals(1.0, $registry->floatValue('some.test.parameter', 1, 1, 2)); - } - - public function testInArrayNotSet() - { - $registry = new EnvVariableRegistry(); - $this->assertFalse($registry->inArray('some.test.parameter', 'name')); - } - - public function testInArraySet() - { - $registry = new EnvVariableRegistry(); - self::putenv('DD_SOME_TEST_PARAMETER=value1,value2'); - $this->assertTrue($registry->inArray('some.test.parameter', 'value1')); - $this->assertTrue($registry->inArray('some.test.parameter', 'value2')); - $this->assertFalse($registry->inArray('some.test.parameter', 'value3')); - } - - public function testInArrayCaseInsensitive() - { - $registry = new EnvVariableRegistry(); - self::putenv('DD_SOME_TEST_PARAMETER=vAlUe1,VaLuE2'); - $this->assertTrue($registry->inArray('some.test.parameter', 'value1')); - $this->assertTrue($registry->inArray('some.test.parameter', 'value2')); - $this->assertFalse($registry->inArray('some.test.parameter', 'value3')); - } - - public function testInArrayWhiteSpaceBetweenDefinitions() - { - $registry = new EnvVariableRegistry(); - self::putenv('DD_SOME_TEST_PARAMETER= value1 , value2 '); - $this->assertTrue($registry->inArray('some.test.parameter', 'value1')); - $this->assertTrue($registry->inArray('some.test.parameter', 'value2')); - $this->assertFalse($registry->inArray('some.test.parameter', 'value3')); - } - - public function testAssociativeStringArrayNotPersistingDefaultValue() - { - $registry = new EnvVariableRegistry(); - $this->assertSame([], $registry->associativeStringArrayValue('some.test.parameter')); - - self::putenv('DD_SOME_TEST_PARAMETER=key:value'); - - $this->assertSame(['key' => 'value'], $registry->associativeStringArrayValue('some.test.parameter')); - } - - /** - * @dataProvider associativeStringArrayDataProvider - * @param string|null $env - * @param string $expected - */ - public function testAssociativeStringArray($env, $expected) - { - if (null !== $env) { - self::putenv('DD_SOME_TEST_PARAMETER=' . $env); - } - $registry = new EnvVariableRegistry(); - $this->assertSame($expected, $registry->associativeStringArrayValue('some.test.parameter')); - } - - public function associativeStringArrayDataProvider() - { - return [ - - 'env not set' => [null, []], - - 'empty string' => ['', []], - - 'only key' => ['only_key', []], - - 'empty key' => [':value', []], - - 'one value' => [' key: value ', ['key' => 'value']], - - 'multiple values' => [' key1: value1 , key2 :value2 ', ['key1' => 'value1', 'key2' => 'value2']], - - 'preserves case' => [' kEy1: vAlUe1 ', ['kEy1' => 'vAlUe1']], - - 'tolerant to errors' => [' kEy1: vAlUe1 : wrong ', []], - ]; - } -} diff --git a/tests/api/Unit/ConfigurationTest.php b/tests/api/Unit/ConfigurationTest.php deleted file mode 100644 index 8fb5ae03d5..0000000000 --- a/tests/api/Unit/ConfigurationTest.php +++ /dev/null @@ -1,385 +0,0 @@ -cleanUpEnvs(); - } - - protected function ddTearDown() - { - $this->cleanUpEnvs(); - Configuration::clear(); - parent::ddTearDown(); - } - - private function cleanUpEnvs() - { - self::putenv('DD_DISTRIBUTED_TRACING'); - self::putenv('DD_ENV'); - self::putenv('DD_SERVICE_MAPPING'); - self::putenv('DD_SERVICE'); - self::putenv('DD_TAGS'); - self::putenv('DD_TRACE_ANALYTICS_ENABLED'); - self::putenv('DD_TRACE_DEBUG'); - self::putenv('DD_TRACE_ENABLED'); - self::putenv('DD_TRACE_SAMPLE_RATE'); - self::putenv('DD_TRACE_SAMPLING_RULES'); - self::putenv('DD_TRACE_SLIM_ENABLED'); - self::putenv('DD_TRACE_PDO_ENABLED'); - self::putenv('DD_VERSION'); - } - - public function testTracerEnabledByDefault() - { - $this->assertTrue(Configuration::get()->isEnabled()); - } - - public function testTracerDisabled() - { - $this->putEnvAndReloadConfig(['DD_TRACE_ENABLED=false']); - $this->assertFalse(Configuration::get()->isEnabled()); - } - - public function testDebugModeDisabledByDefault() - { - $this->assertFalse(Configuration::get()->isDebugModeEnabled()); - } - - public function testDebugModeCanBeEnabled() - { - $this->putEnvAndReloadConfig(['DD_TRACE_DEBUG=true']); - $this->assertTrue(Configuration::get()->isDebugModeEnabled()); - } - - public function testDistributedTracingEnabledByDefault() - { - $this->assertTrue(Configuration::get()->isDistributedTracingEnabled()); - } - - public function testDistributedTracingDisabled() - { - $this->putEnvAndReloadConfig(['DD_DISTRIBUTED_TRACING=false']); - $this->assertFalse(Configuration::get()->isDistributedTracingEnabled()); - } - - public function testAllIntegrationsEnabledByDefault() - { - $this->assertTrue(Configuration::get()->isIntegrationEnabled('pdo')); - } - - public function testIntegrationsDisabled() - { - $this->putEnvAndReloadConfig(['DD_TRACE_PDO_ENABLED=false', 'DD_TRACE_SLIM_ENABLED=false']); - $this->assertFalse(Configuration::get()->isIntegrationEnabled('pdo')); - $this->assertFalse(Configuration::get()->isIntegrationEnabled('slim')); - $this->assertTrue(Configuration::get()->isIntegrationEnabled('mysqli')); - } - - public function testIntegrationsDisabledIfGlobalDisabled() - { - $this->putEnvAndReloadConfig(['DD_TRACE_PDO_ENABLED=false', 'DD_TRACE_ENABLED=false']); - $this->assertFalse(Configuration::get()->isIntegrationEnabled('pdo')); - $this->assertFalse(Configuration::get()->isIntegrationEnabled('mysqli')); - } - - public function testAllIntegrationsEnabledToggleConfig() - { - $integrations = self::getIntegrationsUpper(); - foreach ($integrations as $integration) { - $this->putEnvAndReloadConfig(["DD_TRACE_{$integration}_ENABLED=false"]); - - $lower = strtolower($integration); - $error = "'{$lower}' was expected to be disabled." . self::INTEGRATION_ERROR; - self::assertFalse(Configuration::get()->isIntegrationEnabled($lower), $error); - - // Reset - self::putenv("DD_TRACE_{$integration}_ENABLED"); - } - - // Make sure we're not testing the default fallback - self::assertTrue(Configuration::get()->isIntegrationEnabled('foo_invalid')); - } - - private static function getIntegrationsUpper() - { - $dirs = glob(__DIR__ . '/../../src/DDTrace/Integrations/*', GLOB_ONLYDIR); - return array_map(function ($entry) { - return strtoupper(substr($entry, strrpos($entry, '/') + 1)); - }, $dirs); - } - - public function testServiceName() - { - $this->putEnvAndReloadConfig(['DD_SERVICE']); - - $this->assertSame('__default__', Configuration::get()->appName('__default__')); - - $this->putEnvAndReloadConfig(['DD_SERVICE=my_app']); - Configuration::clear(); - $this->assertSame('my_app', Configuration::get()->appName('__default__')); - } - - public function testAnalyticsDisabledByDefault() - { - $this->assertFalse(Configuration::get()->isAnalyticsEnabled()); - } - - public function testAnalyticsCanBeGloballyEnabled() - { - $this->putEnvAndReloadConfig(['DD_TRACE_ANALYTICS_ENABLED=true']); - $this->assertTrue(Configuration::get()->isAnalyticsEnabled()); - } - - /** - * @dataProvider dataProviderTestTraceSamplingRules - * @param mixed $rules - * @param array $expected - */ - public function testTraceSamplingRules($rules, $expected) - { - if (false !== $rules) { - $this->putEnvAndReloadConfig(['DD_TRACE_SAMPLING_RULES=' . $rules]); - } - - $this->assertSame($expected, Configuration::get()->getSamplingRules()); - } - - public function dataProviderTestTraceSamplingRules() - { - return [ - 'DD_TRACE_SAMPLING_RULES not defined' => [ - false, - [], - ], - 'DD_TRACE_SAMPLING_RULES empty string' => [ - '', - [], - ], - 'DD_TRACE_SAMPLING_RULES not a valid json' => [ - '[a!}', - [], - ], - 'DD_TRACE_SAMPLING_RULES empty array' => [ - '[]', - [], - ], - 'DD_TRACE_SAMPLING_RULES empty object' => [ - '[{}]', - [], - ], - 'DD_TRACE_SAMPLING_RULES only rate' => [ - '[{"sample_rate": 0.3}]', - [ - [ - 'service' => '.*', - 'name' => '.*', - 'sample_rate' => 0.3, - ], - ], - ], - 'DD_TRACE_SAMPLING_RULES service defined' => [ - '[{"service": "my_service", "sample_rate": 0.3}]', - [ - [ - 'service' => 'my_service', - 'name' => '.*', - 'sample_rate' => 0.3, - ], - ], - ], - 'DD_TRACE_SAMPLING_RULES named defined' => [ - '[{"name": "my_name", "sample_rate": 0.3}]', - [ - [ - 'service' => '.*', - 'name' => 'my_name', - 'sample_rate' => 0.3, - ], - ], - ], - 'DD_TRACE_SAMPLING_RULES multiple values keeps order' => [ - '[{"name": "my_name", "sample_rate": 0.3}, {"service": "my_service", "sample_rate": 0.7}]', - [ - [ - 'service' => '.*', - 'name' => 'my_name', - 'sample_rate' => 0.3, - ], - [ - 'service' => 'my_service', - 'name' => '.*', - 'sample_rate' => 0.7, - ], - ], - ], - 'DD_TRACE_SAMPLING_RULES values converted to proper type' => [ - '[{"name": 1, "sample_rate": "0.3"}]', - [ - [ - 'service' => '.*', - 'name' => '1', - 'sample_rate' => 0.3, - ], - ], - ], - 'DD_TRACE_SAMPLING_RULES regex can be provided' => [ - '[{"name": "^a.*b$", "sample_rate": 0.3}]', - [ - [ - 'service' => '.*', - 'name' => '^a.*b$', - 'sample_rate' => 0.3, - ], - ], - ], - ]; - } - - /** - * @dataProvider dataProviderTestTraceSampleRate - * @param mixed $envs - * @param float $expected - */ - public function testTraceSampleRate($envs, $expected) - { - foreach ($envs as $env) { - $this->putEnvAndReloadConfig([$env]); - } - - $this->assertEquals($expected, Configuration::get()->getSamplingRate()); - } - - public function dataProviderTestTraceSampleRate() - { - return [ - 'defaults to 1.0 when nothing is set' => [ - [], - 1.0, - ], - 'DD_TRACE_SAMPLE_RATE can be set' => [ - [ - 'DD_TRACE_SAMPLE_RATE=0.7', - ], - 0.7, - ], - // i.e. default sample rate: will be 1 then - 'DD_TRACE_SAMPLE_RATE has a minimum of 0.0' => [ - [ - 'DD_TRACE_SAMPLE_RATE=-0.1', - ], - 1, - ], - 'DD_TRACE_SAMPLE_RATE has a maximum of 1.0' => [ - [ - 'DD_TRACE_SAMPLE_RATE=1.1', - ], - 1.0, - ], - ]; - } - - /** - * @dataProvider dataProviderTestServiceMapping - * @param mixed $envs - * @param array $expected - */ - public function testTraceServiceMapping($env, $expected) - { - if (false !== $env) { - $this->putEnvAndReloadConfig(["DD_SERVICE_MAPPING=$env"]); - } - - $this->assertSame($expected, Configuration::get()->getServiceMapping()); - } - - public function dataProviderTestServiceMapping() - { - return [ - 'not set' => [ - false, - [], - ], - 'empty' => [ - false, - [], - ], - 'one service mapping' => [ - 'service1:service2', - [ 'service1' => 'service2' ], - ], - 'multiple service mappings' => [ - 'service1:service2,service3:service4', - [ 'service1' => 'service2', 'service3' => 'service4' ], - ], - 'tolerant to extra whitespace' => [ - 'service1 : service2 , service3 : service4 ', - [ 'service1' => 'service2', 'service3' => 'service4' ], - ], - ]; - } - - public function testEnv() - { - $this->putEnvAndReloadConfig(['DD_ENV=my-env']); - $this->assertSame('my-env', Configuration::get()->getEnv()); - } - - public function testEnvNotSet() - { - $this->putEnvAndReloadConfig(['DD_ENV']); - $this->assertEmpty(Configuration::get()->getEnv()); - } - - public function testVersion() - { - $this->putEnvAndReloadConfig(['DD_VERSION=1.2.3']); - $this->assertSame('1.2.3', Configuration::get()->getServiceVersion()); - } - - public function testVersionNotSet() - { - $this->putEnvAndReloadConfig(['DD_VERSION']); - $this->assertEmpty(Configuration::get()->getServiceVersion()); - } - - public function testUriAsResourceNameEnabledDefault() - { - $this->assertTrue(Configuration::get()->isURLAsResourceNameEnabled()); - } - - public function testUriAsResourceNameCanBeDisabled() - { - $this->putEnvAndReloadConfig(['DD_TRACE_URL_AS_RESOURCE_NAMES_ENABLED=false']); - $this->assertFalse(Configuration::get()->isURLAsResourceNameEnabled()); - } - - public function testGlobalTags() - { - $this->putEnvAndReloadConfig(['DD_TAGS=key1:value1,key2:value2']); - $this->assertEquals(['key1' => 'value1', 'key2' => 'value2'], Configuration::get()->getGlobalTags()); - $this->assertEquals(['key1' => 'value1', 'key2' => 'value2'], \dd_trace_env_config("DD_TAGS")); - } - - public function testGlobalTagsWrongValueJustResultsInNoTags() - { - $this->putEnvAndReloadConfig(['DD_TAGS=wrong_key_value']); - $this->assertEquals([], Configuration::get()->getGlobalTags()); - } -}