11use Liuggio\StatsdClient\Entity\StatsdData;
12use Liuggio\StatsdClient\Entity\StatsdDataInterface;
13use Liuggio\StatsdClient\Factory\StatsdDataFactory;
45 parent::__construct();
46 $this->prefix = $prefix;
58 public function timing( $key, $time ) {
59 if ( !$this->enabled ) {
62 $this->buffer[] = [ $key, $time, StatsdDataInterface::STATSD_METRIC_TIMING ];
70 public function gauge( $key, $value ) {
71 if ( !$this->enabled ) {
74 $this->buffer[] = [ $key, $value, StatsdDataInterface::STATSD_METRIC_GAUGE ];
82 public function set( $key, $value ) {
83 if ( !$this->enabled ) {
86 $this->buffer[] = [ $key, $value, StatsdDataInterface::STATSD_METRIC_SET ];
95 if ( !$this->enabled ) {
98 $this->buffer[] = [ $key, 1, StatsdDataInterface::STATSD_METRIC_COUNT ];
107 if ( !$this->enabled ) {
110 $this->buffer[] = [ $key, -1, StatsdDataInterface::STATSD_METRIC_COUNT ];
119 if ( !$this->enabled ) {
122 $this->buffer[] = [ $key, $delta, StatsdDataInterface::STATSD_METRIC_COUNT ];
141 private static function normalizeMetricKey( $key ) {
142 $key = strtr( $key, [
'::' =>
'.' ] );
143 $key = preg_replace(
'/[^a-zA-Z0-9.]+/',
'_', $key );
144 $key = trim( $key,
'_.' );
145 return strtr( $key, [
'..' =>
'.' ] );
150 $key, $value = 1, $metric = StatsdDataInterface::STATSD_METRIC_COUNT
152 $entity = $this->produceStatsdDataEntity();
153 if ( $key !==
null ) {
154 $key = self::normalizeMetricKey(
"{$this->prefix}.{$key}" );
155 $entity->setKey( $key );
157 if ( $value !==
null ) {
158 $entity->setValue( $value );
160 if ( $metric !==
null ) {
161 $entity->setMetric( $metric );
181 foreach ( $this->buffer as [ $key, $val, $metric ] ) {
183 if ( $metric === StatsdDataInterface::STATSD_METRIC_COUNT && !$val ) {
202 return count( $this->buffer );
212class_alias( BufferingStatsdDataFactory::class,
'BufferingStatsdDataFactory' );