Commit c20a548e authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3321935 by alexpott: Add the ability to use the metric ingestion API

parent 65b749df
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
base_url: ''
host: ''
api_keys:
  metrics: ''
+3 −0
Original line number Diff line number Diff line
@@ -6,6 +6,9 @@ dynatrace.settings:
    base_url:
      type: uri
      label: 'Base URL'
    host:
      type: string
      label: 'Host'
    api_keys:
      type: mapping
      label: 'API keys'
+6 −0
Original line number Diff line number Diff line
@@ -2,3 +2,9 @@ services:
  dynatrace.one_agent:
    class: Drupal\dynatrace\OneAgent
    arguments: []
  dynatrace.ingestMetric:
    class: Drupal\dynatrace\Api\IngestMetric
    arguments: ['@http_client', '@config.factory', '@logger.channel.dynatrace']
  logger.channel.dynatrace:
    parent: logger.channel_base
    arguments: ['dynatrace']

src/Api/Count.php

0 → 100644
+44 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\dynatrace\Api;

/**
 * Data points of the count type are deltas between the previous and current
 * data points.
 *
 * For example, if the initial data point has the value of 500 and the second
 * data point has the value of 1,000, the actual stored value at the timestamp
 * of the second data point is 1,500.
 *
 * @see https://www.dynatrace.com/support/help/extend-dynatrace/extend-metrics/reference/metric-ingestion-protocol
 */
class Count extends MetricBase {

  /**
   * The delta to send to Dynatrace.
   *
   * @var int
   */
  private int $delta = 1;

  /**
   * Sets the count delta.
   *
   * @param int $delta
   *   A numeric value.
   *
   * @return $this
   */
  public function setDelta(int $delta): static {
    $this->delta = $delta;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  protected function payloadToString(): string {
    return 'count,delta=' . $this->delta;
  }

}

src/Api/Gauge.php

0 → 100644
+54 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\dynatrace\Api;

/**
 * @see https://www.dynatrace.com/support/help/extend-dynatrace/extend-metrics/reference/metric-ingestion-protocol
 */
class Gauge extends MetricBase {

  /**
   * An array of numeric values.
   *
   * @var int[]|float[]
   */
  private $values = [];

  /**
   * Adds a value to gauge.
   *
   * @param int|float $value
   *   A numeric value.
   *
   * @return $this
   */
  public function addValue(int|float $value): static {
    $this->values[] = $value;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  protected function payloadToString(): string {
    $output = 'gauge,';
    $count = count($this->values);
    if ($count < 2) {
      $output .= $this->values[0] ?? 1;
    }
    else {
      foreach ($this->values as $value) {
        $min = isset($min) ? min($value, $min) : $value;
        $max = isset($max) ? max($value, $max) : $value;
        $sum = isset($sum) ? $sum + $value : $value;
      }
      // These variable will always be set be stops PHPStan, PHPCS and IDEs from
      // complaining.
      if (isset($min) && isset($max) && isset($sum)) {
        $output .= "min=$min,max=$max,sum=$sum,count=$count";
      }
    }
    return $output;
  }

}
Loading