Beberlei\Metrics\Collector\Prometheus::flush PHP Method

flush() public method

public flush ( )
    public function flush()
    {
        if (!$this->data['gauges'] && !$this->data['counters']) {
            return;
        }
        $tagsValues = array_values($this->tags);
        foreach ($this->data['counters'] as $counterData) {
            $gauge = $this->getOrRegisterGaugeForVariable($counterData['name']);
            if ($counterData['value'] > 0) {
                $gauge->inc($tagsValues);
            } elseif ($counterData['value'] < 0) {
                $gauge->dec($tagsValues);
            }
        }
        foreach ($this->data['gauges'] as $gaugeData) {
            $gauge = $this->getOrRegisterGaugeForVariable($gaugeData['name']);
            $gauge->set($gaugeData['value'], $tagsValues);
        }
        $this->data = array('counters' => array(), 'gauges' => array());
    }

Usage Example

Beispiel #1
0
 /**
  * Method flush must to reset value of field `data`.
  */
 public function testFlushWhenCallsTwiceWithDifferentData()
 {
     $firstExpectedVariableValue = 123;
     $secondExpectedVariableValue = 321;
     $gaugeMock = $this->getMockBuilder('\\Prometheus\\Gauge')->disableOriginalConstructor()->getMock();
     $gaugeMock->expects($this->at(0))->method('set')->with($firstExpectedVariableValue, array());
     $gaugeMock->expects($this->at(1))->method('set')->with($secondExpectedVariableValue, array());
     $this->collectorRegistryMock->expects($this->exactly(2))->method('getGauge')->with(self::TEST_NAMESPACE, self::TEST_VARIABLE_NAME)->willReturn($gaugeMock);
     $this->collector->measure(self::TEST_VARIABLE_NAME, $firstExpectedVariableValue);
     $this->collector->flush();
     $this->collector->measure(self::TEST_VARIABLE_NAME, $secondExpectedVariableValue);
     $this->collector->flush();
 }