All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.orientechnologies.agent.services.metrics.OrientDBMetricsCommand Maven / Gradle / Ivy

package com.orientechnologies.agent.services.metrics;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.orientechnologies.agent.profiler.OMetricsRegistry;
import com.orientechnologies.agent.profiler.metrics.OMetric;
import com.orientechnologies.agent.profiler.metrics.OMetricSet;
import com.orientechnologies.orient.server.network.protocol.http.OHttpRequest;
import com.orientechnologies.orient.server.network.protocol.http.OHttpResponse;
import com.orientechnologies.orient.server.network.protocol.http.OHttpUtils;
import com.orientechnologies.orient.server.network.protocol.http.command.OServerCommandAuthenticatedServerAbstract;
import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.exporter.common.TextFormat;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by Enrico Risa on 06/08/2018.
 */
public class OrientDBMetricsCommand extends OServerCommandAuthenticatedServerAbstract {

  private static final String[] NAMES = { "GET|metrics", "GET|metrics/*", "POST|metrics/config", "POST|metrics/config" };

  private OMetricsRegistry       registry;
  private OrientDBMetricsService service;
  private ObjectMapper           mapper = new ObjectMapper();

  public OrientDBMetricsCommand(OMetricsRegistry registry, OrientDBMetricsService settings) {
    super("server.metrics");
    this.registry = registry;
    this.service = settings;
  }

  @Override
  public boolean execute(OHttpRequest iRequest, OHttpResponse iResponse) throws Exception {
    final String[] parts = checkSyntax(iRequest.getUrl(), 1, "Syntax error: metrics");

    if (iRequest.httpMethod.equalsIgnoreCase("GET")) {
      doGet(iResponse, parts);
    } else if (iRequest.httpMethod.equalsIgnoreCase("POST")) {
      doPost(iRequest, iResponse, parts);
    }

    return false;
  }

  private void doGet(OHttpResponse iResponse, String[] parts) throws IOException {
    if (parts.length == 1) {

      ByteArrayOutputStream buffer = new ByteArrayOutputStream();
      registry.toJSON(buffer);
      iResponse.send(OHttpUtils.STATUS_OK_CODE, OHttpUtils.STATUS_OK_DESCRIPTION, OHttpUtils.CONTENT_JSON, buffer.toString(), null);
    } else {
      String command = parts[1];

      OrientDBMetricsSettings settings = service.getSettings();
      switch (command) {
      case "prometheus":
        if (settings.reporters.prometheus.enabled) {
          try (StringWriter writer = new StringWriter()) {
            TextFormat.write004(writer, CollectorRegistry.defaultRegistry.filteredMetricFamilySamples(Collections.emptySet()));
            iResponse
                .send(OHttpUtils.STATUS_OK_CODE, OHttpUtils.STATUS_OK_DESCRIPTION, OHttpUtils.CONTENT_TEXT_PLAIN, writer.toString(),
                    null);
          }
        }
        break;
      case "config":
        String valueAsString = mapper.writeValueAsString(settings);
        iResponse.send(OHttpUtils.STATUS_OK_CODE, OHttpUtils.STATUS_OK_DESCRIPTION, OHttpUtils.CONTENT_JSON, valueAsString, null);
        break;
      case "list":
        Map metrics = getMetricsLists(registry.getMetrics(), "");
        String metricsAsString = mapper
            .writeValueAsString(new MetricList(metrics.entrySet().stream().map(v -> v.getValue()).collect(Collectors.toList())));
        iResponse.send(OHttpUtils.STATUS_OK_CODE, OHttpUtils.STATUS_OK_DESCRIPTION, OHttpUtils.CONTENT_JSON, metricsAsString, null);
        break;
      }

    }
  }

  protected class MetricList {
    List metrics;

    public MetricList(List metrics) {
      this.metrics = metrics;
    }

    public List getMetrics() {
      return metrics;
    }
  }

  protected class MetricInfo {
    String name;
    String description;
    String unitOfMeasure;

    public MetricInfo(String name, String description, String unitOfMeasure) {
      this.name = name;
      this.description = description;
      this.unitOfMeasure = unitOfMeasure;
    }

    public String getName() {
      return name;
    }

    public void setName(String name) {
      this.name = name;
    }

    public String getDescription() {
      return description;
    }

    public void setDescription(String description) {
      this.description = description;
    }

    public String getUnitOfMeasure() {
      return unitOfMeasure;
    }

    public void setUnitOfMeasure(String unitOfMeasure) {
      this.unitOfMeasure = unitOfMeasure;
    }
  }

  private Map getMetricsLists(Map metrics, String prefix) {

    Map infos = new HashMap<>();
    metrics.forEach((k, v) -> {
      if (v instanceof OMetricSet) {
        infos.putAll(getMetricsLists(((OMetricSet) v).getMetrics(), ((OMetricSet) v).prefix()));
      } else {
        if (prefix != null && !prefix.isEmpty()) {
          infos.put(prefix + "." + k, new MetricInfo(prefix + "." + k, v.getDescription(), v.getUnitOfMeasure()));
        } else {
          infos.put(k, new MetricInfo(k, v.getDescription(), v.getUnitOfMeasure()));

        }
      }
    });
    return infos;

  }

  private void doPost(OHttpRequest iRequest, OHttpResponse iResponse, String[] parts) throws IOException {
    OrientDBMetricsSettings settings = mapper.readValue(iRequest.content, OrientDBMetricsSettings.class);
    service.changeSettings(settings);
    iResponse.send(OHttpUtils.STATUS_OK_CODE, OHttpUtils.STATUS_OK_DESCRIPTION, OHttpUtils.CONTENT_JSON, null, null);
  }

  @Override
  public String[] getNames() {
    return NAMES;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy