diff --git a/src/JaegerExporter.php b/src/JaegerExporter.php index bb995ed..2c14fed 100644 --- a/src/JaegerExporter.php +++ b/src/JaegerExporter.php @@ -58,6 +58,11 @@ class JaegerExporter implements ExporterInterface */ private $spanConverter; + /** + * @var array + */ + private $tags; + /** * Create a new Jaeger Exporter. * @@ -82,11 +87,21 @@ public function __construct($serviceName, array $options = []) $this->host = $options['host']; $this->port = (int) $options['port']; $this->spanConverter = empty($options['spanConverter']) ? new SpanConverter() : $options['spanConverter']; - $this->process = new Process([ - 'serviceName' => $serviceName, - 'tags' => $this->spanConverter->convertTags($options['tags']) - ]); + $this->tags = $this->spanConverter->convertTags($options['tags']); $this->client = $options['client']; + + // if this option is passed, the spans with a particular prefix would be exported + // with that serviceName. + // eg. prefixServiceNameMap => ['PDO' => 'app_db', 'Predis' => 'app_redis']; + + if (array_key_exists('prefixServiceNameMap', $options)){ + $this->prefixServiceNameMap = $options['prefixServiceNameMap']; + } + else{ + $this->prefixServiceNameMap = []; + } + + $this->prefixServiceNameMap += ['_default_' => $serviceName]; } /** @@ -101,13 +116,44 @@ public function export(array $spans) return false; } + // create different span buckets for each prefix + $buckets = []; + foreach (array_keys($this->prefixServiceNameMap) as $prefix){ + $buckets[$prefix] = []; + } + + foreach ($spans as $s){ + $bucketed = false; + foreach (array_keys($buckets) as $prefix){ + // if span name starts with a particular prefix, put the span in that bucket + if (strpos($s->name(), $prefix) === 0){ + $buckets[$prefix][] = $s; + $bucketed = true; + break; + } + } + if (!$bucketed){ + $buckets['_default_'][] = $s; + } + } + $client = $this->client ?: new UDPClient($this->host, $this->port); - $batch = new Batch([ - 'process' => $this->process, - 'spans' => array_map([$this->spanConverter, 'convertSpan'], $spans) - ]); - $client->emitBatch($batch); + foreach ($buckets as $prefix => $spanBucket){ + if (count($spanBucket) != 0){ + $process = new Process([ + 'serviceName' => $this->prefixServiceNameMap[$prefix], + 'tags' => $this->tags + ]); + + $batch = new Batch([ + 'process' => $process, + 'spans' => array_map([$this->spanConverter, 'convertSpan'], $spanBucket) + ]); + $client->emitBatch($batch); + } + } + return true; } }