• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

openzipkin / zipkin-php / #597

pending completion
#597

push

php-coveralls

web-flow
Convert carrier key to string. (#226)

1 of 1 new or added line in 1 file covered. (100.0%)

811 of 997 relevant lines covered (81.34%)

10.91 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

75.0
/src/Zipkin/Reporters/Http.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Zipkin\Reporters;
6

7
use Zipkin\Reporters\SpanSerializer;
8
use Zipkin\Reporters\JsonV2Serializer;
9
use Zipkin\Reporters\Http\CurlFactory;
10
use Zipkin\Reporters\Http\ClientFactory;
11
use Zipkin\Reporter;
12
use Zipkin\Recording\ReadbackSpan;
13
use RuntimeException;
14
use Psr\Log\NullLogger;
15
use Psr\Log\LoggerInterface;
16

17
final class Http implements Reporter
18
{
19
    public const DEFAULT_OPTIONS = [
20
        'endpoint_url' => 'http://localhost:9411/api/v2/spans',
21
    ];
22

23
    private ClientFactory $clientFactory;
24

25
    private array $options;
26

27
    /**
28
     * logger is only meant to be used for development purposes. Enabling
29
     * an actual logger in production could cause a massive amount of data
30
     * that will flood the logs on failure.
31
     */
32
    private LoggerInterface $logger;
33

34
    private SpanSerializer $serializer;
35

36
    /**
37
     * @param array $options the options for HTTP call:
38
     *
39
     * <code>
40
     * $options = [
41
     *   'endpoint_url' => 'http://myzipkin:9411/api/v2/spans', // the reporting url for zipkin server
42
     *   'headers'      => ['X-API-Key' => 'abc123'] // the additional headers to be included in the request
43
     *   'timeout'      => 10, // the timeout for the request in seconds
44
     * ];
45
     * </code>
46
     *
47
     * @param ClientFactory $requesterFactory the factory for the client
48
     * that will do the HTTP call
49
     * @param LoggerInterface $logger the logger for output
50
     * @param SpanSerializer $serializer
51
     */
52
    public function __construct(
53
        array $options = [],
54
        ClientFactory $requesterFactory = null,
55
        LoggerInterface $logger = null,
56
        SpanSerializer $serializer = null
57
    ) {
58
        $this->options = \array_merge(self::DEFAULT_OPTIONS, $options);
3✔
59
        $this->clientFactory = $requesterFactory ?? CurlFactory::create();
3✔
60
        $this->logger = $logger ?? new NullLogger();
3✔
61
        $this->serializer = $serializer ?? new JsonV2Serializer();
3✔
62
    }
63

64
    /**
65
     * @param ReadbackSpan[] $spans
66
     * @return void
67
     */
68
    public function report(array $spans): void
69
    {
70
        if (\count($spans) === 0) {
3✔
71
            return;
1✔
72
        }
73

74
        $payload = $this->serializer->serialize($spans);
2✔
75
        if ($payload === false) {
2✔
76
            $this->logger->error(
×
77
                \sprintf('failed to encode spans with code %d', \json_last_error())
×
78
            );
×
79
            return;
×
80
        }
81

82
        $client = $this->clientFactory->build($this->options);
2✔
83
        try {
84
            $client($payload);
2✔
85
        } catch (RuntimeException $e) {
1✔
86
            $this->logger->error(\sprintf('failed to report spans: %s', $e->getMessage()));
1✔
87
        }
88
    }
89
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc