25use Liuggio\StatsdClient\Entity\StatsdData;
26use Liuggio\StatsdClient\Entity\StatsdDataInterface;
27use Liuggio\StatsdClient\Factory\StatsdDataFactory;
58 parent::__construct();
59 $this->prefix = $prefix;
71 public function timing( $key, $time ) {
72 if ( !$this->enabled ) {
75 $this->buffer[] = [ $key, $time, StatsdDataInterface::STATSD_METRIC_TIMING ];
83 public function gauge( $key, $value ) {
84 if ( !$this->enabled ) {
87 $this->buffer[] = [ $key, $value, StatsdDataInterface::STATSD_METRIC_GAUGE ];
95 public function set( $key, $value ) {
96 if ( !$this->enabled ) {
99 $this->buffer[] = [ $key, $value, StatsdDataInterface::STATSD_METRIC_SET ];
108 if ( !$this->enabled ) {
111 $this->buffer[] = [ $key, 1, StatsdDataInterface::STATSD_METRIC_COUNT ];
120 if ( !$this->enabled ) {
123 $this->buffer[] = [ $key, -1, StatsdDataInterface::STATSD_METRIC_COUNT ];
132 if ( !$this->enabled ) {
135 $this->buffer[] = [ $key, $delta, StatsdDataInterface::STATSD_METRIC_COUNT ];
154 private static function normalizeMetricKey( $key ) {
155 $key = strtr( $key, [
'::' =>
'.' ] );
156 $key = preg_replace(
'/[^a-zA-Z0-9.]+/',
'_', $key );
157 $key = trim( $key,
'_.' );
158 return strtr( $key, [
'..' =>
'.' ] );
162 $key, $value = 1, $metric = StatsdDataInterface::STATSD_METRIC_COUNT
164 $entity = $this->produceStatsdDataEntity();
165 if ( $key !==
null ) {
166 $key = self::normalizeMetricKey(
"{$this->prefix}.{$key}" );
167 $entity->setKey( $key );
169 if ( $value !==
null ) {
170 $entity->setValue( $value );
172 if ( $metric !==
null ) {
173 $entity->setMetric( $metric );
192 foreach ( $this->buffer as [ $key, $val, $metric ] ) {
194 if ( $metric === StatsdDataInterface::STATSD_METRIC_COUNT && !$val ) {
211 return count( $this->buffer );
220class_alias( BufferingStatsdDataFactory::class,
'BufferingStatsdDataFactory' );