|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace JSHayes\LaravelDataDogStatsd; |
| 4 | + |
| 5 | +use DataDog\DogStatsd; |
| 6 | + |
| 7 | +class StatBuilder |
| 8 | +{ |
| 9 | + private $statsd; |
| 10 | + private $stat; |
| 11 | + private $sampleRate = 1.0; |
| 12 | + private $tags = null; |
| 13 | + |
| 14 | + /** |
| 15 | + * Create a new metric builder |
| 16 | + * |
| 17 | + * @param \DataDog\DogStatsd $statsd |
| 18 | + * @param string $stat |
| 19 | + */ |
| 20 | + public function __construct(DogStatsd $statsd, string $stat) |
| 21 | + { |
| 22 | + $this->statsd = $statsd; |
| 23 | + $this->stat = $stat; |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Apply the given tags to this stat |
| 28 | + * |
| 29 | + * @param array|string $tags |
| 30 | + * |
| 31 | + * @return self |
| 32 | + */ |
| 33 | + public function withTags($tags): self |
| 34 | + { |
| 35 | + $this->tags = $tags; |
| 36 | + return $this; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Use the given sample rate for this stat |
| 41 | + * |
| 42 | + * @param array|string $tags |
| 43 | + * |
| 44 | + * @return self |
| 45 | + */ |
| 46 | + public function withSampleRate(float $sampleRate): self |
| 47 | + { |
| 48 | + $this->sampleRate = $sampleRate; |
| 49 | + return $this; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Log timing information |
| 54 | + * |
| 55 | + * @param float $time |
| 56 | + * |
| 57 | + * @return void |
| 58 | + */ |
| 59 | + public function timing(float $time): void |
| 60 | + { |
| 61 | + $this->statsd->timing($this->stat, $time, $this->sampleRate, $this->tags); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * A convenient alias for the timing function when used with micro-timing |
| 66 | + * |
| 67 | + * @param float $time |
| 68 | + * |
| 69 | + * @return void |
| 70 | + */ |
| 71 | + public function microtiming(float $time): void |
| 72 | + { |
| 73 | + $this->statsd->microtiming($this->stat, $time, $this->sampleRate, $this->tags); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Gauge |
| 78 | + * |
| 79 | + * @param float $value |
| 80 | + * |
| 81 | + * @return void |
| 82 | + */ |
| 83 | + public function gauge(float $value): void |
| 84 | + { |
| 85 | + $this->statsd->gauge($this->stat, $value, $this->sampleRate, $this->tags); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Histogram |
| 90 | + * |
| 91 | + * @param float $value |
| 92 | + * |
| 93 | + * @return void |
| 94 | + */ |
| 95 | + public function histogram(float $value): void |
| 96 | + { |
| 97 | + $this->statsd->histogram($this->stat, $value, $this->sampleRate, $this->tags); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Distribution |
| 102 | + * |
| 103 | + * @param float $value |
| 104 | + * |
| 105 | + * @return void |
| 106 | + */ |
| 107 | + public function distribution(float $value): void |
| 108 | + { |
| 109 | + $this->statsd->distribution($this->stat, $value, $this->sampleRate, $this->tags); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Set |
| 114 | + * |
| 115 | + * @param float $value |
| 116 | + * |
| 117 | + * @return void |
| 118 | + */ |
| 119 | + public function set(float $value): void |
| 120 | + { |
| 121 | + $this->statsd->set($this->stat, $value, $this->sampleRate, $this->tags); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Increment a stat counter |
| 126 | + * |
| 127 | + * @param int $value |
| 128 | + * |
| 129 | + * @return void |
| 130 | + */ |
| 131 | + public function increment(int $value = 1): void |
| 132 | + { |
| 133 | + $this->statsd->increment($this->stat, $this->sampleRate, $this->tags, $value); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Decrement a stat counter |
| 138 | + * |
| 139 | + * @param int $value |
| 140 | + * |
| 141 | + * @return void |
| 142 | + */ |
| 143 | + public function decrement(int $value = -1): void |
| 144 | + { |
| 145 | + $this->statsd->decrement($this->stat, $this->sampleRate, $this->tags, $value); |
| 146 | + } |
| 147 | +} |
0 commit comments