io.prometheus.client.SimpleCollector Java Examples

The following examples show how to use io.prometheus.client.SimpleCollector. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PrometheusMetricTest.java    From athenz with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateOrGetCollector() throws NoSuchMethodException, SecurityException, IllegalAccessException,
        IllegalArgumentException, InvocationTargetException {
    CollectorRegistry registry = new CollectorRegistry();
    ConcurrentHashMap<String, Collector> namesToCollectors = new ConcurrentHashMap<>();
    PrometheusMetric metric = new PrometheusMetric(registry, namesToCollectors, null, "");
    Method createOrGetCollector = metric.getClass().getDeclaredMethod("createOrGetCollector", String.class, SimpleCollector.Builder.class);
    createOrGetCollector.setAccessible(true);

    // test create
    String metricName = "metric_test";
    Counter.Builder builder = Counter.build();
    double countValue = 110.110d;
    Counter counter = (Counter) createOrGetCollector.invoke(metric, metricName, builder);
    counter.labels("", "").inc(countValue);
    // assertions
    Assert.assertSame(counter, namesToCollectors.get(metricName));
    Assert.assertEquals(registry.getSampleValue(metricName, this.labelNames, new String[]{"", ""}), countValue);

    // test get
    Counter counter_2 = (Counter) createOrGetCollector.invoke(metric, metricName, builder);
    // assertions
    Assert.assertSame(counter_2, namesToCollectors.get(metricName));
    Assert.assertSame(counter_2, counter);
}
 
Example #2
Source File: PrometheusMetrics.java    From garmadon with Apache License 2.0 5 votes vote down vote up
private static <CHILD> void registerPartitionCollector(SimpleCollector<CHILD> collector, Integer partition, String... labels) {
    REGISTERED_COLLECTORS.computeIfAbsent(partition, ignored -> new HashMap<>());
    REGISTERED_COLLECTORS.computeIfPresent(partition, (part, parentCollectorToChildLabels) -> {
        parentCollectorToChildLabels.computeIfAbsent(collector, ignored -> new HashSet<>());
        parentCollectorToChildLabels.computeIfPresent(collector, (ignored, childLabels) -> {
            childLabels.add(Arrays.asList(mergeWithDefault(labels)));
            return childLabels;
        });
        return parentCollectorToChildLabels;
    });
}
 
Example #3
Source File: PromagentCollectorRegistry.java    From promagent with Apache License 2.0 5 votes vote down vote up
@Override
public void register(Collector metric) {
    super.register(metric);
    try {
        ManagementFactory.getPlatformMBeanServer().registerMBean(new Metric(metric), makeObjectName((SimpleCollector) metric));
    } catch (Exception e) {
        throw new RuntimeException("Failed to register Prometheus metric: " + e.getMessage(), e);
    }
}
 
Example #4
Source File: PromagentCollectorRegistry.java    From promagent with Apache License 2.0 5 votes vote down vote up
private static String getFullName(SimpleCollector metric) {
    // Unfortunately, there is no public API to get the 'fullname' of a metric. We use reflection to get it anyway.
    try {
        Field field = SimpleCollector.class.getDeclaredField("fullname");
        field.setAccessible(true);
        return (String) field.get(metric);
    } catch (IllegalAccessException | NoSuchFieldException e) {
        throw new RuntimeException("Failed to access " + metric.getClass().getName() + ".fullname. " +
                "This is probably because the internal implementation of the Prometheus client library has changed. " +
                "You should adapt the Promagent accordingly.", e);
    }
}
 
Example #5
Source File: PrometheusMetric.java    From athenz with Apache License 2.0 5 votes vote down vote up
/**
 * Create collector and register it to the registry.
 * This is needed since Athenz metric names are defined on runtime and we need the same collector object to record the data.
 * @param metricName Name of the metric
 * @param builder Prometheus Collector Builder
 */
private Collector createOrGetCollector(String metricName, SimpleCollector.Builder<?, ?> builder) {
    String key = metricName;
    ConcurrentMap<String, Collector> map = this.namesToCollectors;
    Collector collector = map.get(key);

    // double checked locking
    if (collector == null) {
        synchronized (map) {
            if (!map.containsKey(key)) {
                // create
                builder = builder
                    .namespace(this.namespace)
                    .name(metricName)
                    .help(metricName)
                    .labelNames(REQUEST_DOMAIN_LABEL_NAME, PRINCIPAL_DOMAIN_LABEL_NAME);
                collector = builder.register(this.registry);
                // put
                map.put(key, collector);
            } else {
                // get
                collector = map.get(key);
            }
        }
    };

    return collector;
}
 
Example #6
Source File: FreshnessMetrics.java    From kafka-helmsman with MIT License 4 votes vote down vote up
public void register() {
  for (SimpleCollector collector :
      newArrayList(elapsed, missing, kafkaRead, failed, invalid, error, freshness, kafkaQueryLatency)) {
    collector.register();
  }
}
 
Example #7
Source File: PrometheusMetrics.java    From garmadon with Apache License 2.0 4 votes vote down vote up
private static <CHILD> CHILD buildChild(SimpleCollector<CHILD> collector, String... labels) {
    return collector.labels(mergeWithDefault(labels));
}
 
Example #8
Source File: PrometheusMetrics.java    From garmadon with Apache License 2.0 4 votes vote down vote up
private static <CHILD> CHILD buildChild(SimpleCollector<CHILD> collector, Integer partition, String... labels) {
    registerPartitionCollector(collector, partition, labels);
    return buildChild(collector, labels);
}
 
Example #9
Source File: PrometheusMetrics.java    From garmadon with Apache License 2.0 4 votes vote down vote up
public static void clearPartitionCollectors(int partition) {
    Map<SimpleCollector<?>, Set<List<String>>> collectorToChildLabels = REGISTERED_COLLECTORS.get(partition);
    if (collectorToChildLabels != null) {
        collectorToChildLabels.forEach((collector, childLabels) -> childLabels.forEach(labels -> collector.remove(labels.toArray(new String[0]))));
    }
}
 
Example #10
Source File: PrometheusMetrics.java    From garmadon with Apache License 2.0 4 votes vote down vote up
public static Map<Integer, Map<SimpleCollector<?>, Set<List<String>>>> getRegisteredCollectors() {
    return Collections.unmodifiableMap(REGISTERED_COLLECTORS);
}
 
Example #11
Source File: PromagentCollectorRegistry.java    From promagent with Apache License 2.0 4 votes vote down vote up
private static ObjectName makeObjectName(SimpleCollector metric) throws MalformedObjectNameException {
    return makeObjectName(getFullName(metric));
}
 
Example #12
Source File: ClientMetrics.java    From java-grpc-prometheus with Apache License 2.0 4 votes vote down vote up
private <T> T addLabels(SimpleCollector<T> collector, String... labels) {
  List<String> allLabels = new ArrayList<>();
  Collections.addAll(allLabels, method.type(), method.serviceName(), method.methodName());
  Collections.addAll(allLabels, labels);
  return collector.labels(allLabels.toArray(new String[0]));
}
 
Example #13
Source File: ServerMetrics.java    From java-grpc-prometheus with Apache License 2.0 4 votes vote down vote up
private <T> T addLabels(SimpleCollector<T> collector, String... labels) {
  List<String> allLabels = new ArrayList<>();
  Collections.addAll(allLabels, method.type(), method.serviceName(), method.methodName());
  Collections.addAll(allLabels, labels);
  return collector.labels(allLabels.toArray(new String[0]));
}