Skip to content

Commit

Permalink
Add temporary reflection hack to get meters into Prometheus Collector…
Browse files Browse the repository at this point in the history
…Registry (fixes #6)
  • Loading branch information
jkschneider committed May 16, 2017
1 parent c11a324 commit 676ff5a
Showing 1 changed file with 19 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@
*/
package org.springframework.metrics.instrument.prometheus;

import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.*;
import io.prometheus.client.Gauge;
import io.prometheus.client.SimpleCollector;
import io.prometheus.client.Summary;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.metrics.instrument.*;
import org.springframework.metrics.instrument.Counter;
import org.springframework.metrics.instrument.internal.AbstractMeterRegistry;

import java.lang.ref.WeakReference;
import java.lang.reflect.Field;
import java.util.Map;
import java.util.function.ToDoubleFunction;
import java.util.function.UnaryOperator;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -100,6 +101,21 @@ private <B extends SimpleCollector.Builder<B, C>, C extends SimpleCollector<D>,

collector = collectorTransform.apply(collector);

// since we don't yet create Histograms, the full name will be one of the metrics registered
// with the collector registry
// FIXME we should expose a getter on namesToCollectors in CollectorRegistry
try {
Field namesToCollectorsField = registry.getClass().getDeclaredField("namesToCollectors");

This comment has been minimized.

Copy link
@jkschneider

jkschneider May 16, 2017

Author Contributor

@checketts Do you think we could get a public getter on this in simpleclient?

This comment has been minimized.

Copy link
@checketts

checketts May 16, 2017

Contributor

We might get pushback, but I bet we could add a 'contains' method. Would that be sufficient?

I'll prep a PR for that in the client

This comment has been minimized.

Copy link
@jkschneider

jkschneider May 16, 2017

Author Contributor

Perfectly sufficient. You see what I'm angling at here. Just a constant time lookup on name.

This comment has been minimized.

Copy link
@spencergibb

spencergibb May 16, 2017

Collaborator

Spring's ReflectionUtils has some nice methods for dealing with this kind of simple reflection.

This comment has been minimized.

Copy link
@jkschneider

jkschneider May 16, 2017

Author Contributor

Thanks @spencergibb -- I didn't know about that class. Tempting, but so far spring is an optional dep. I'll try to keep it that way until it just isn't possible anymore.

This comment has been minimized.

Copy link
@jkschneider

jkschneider May 16, 2017

Author Contributor

@checketts Never mind the PR to simpleclient. I'm doing a little shuffling that will make it and the hack unnecessary. Should land in the repo shortly.

namesToCollectorsField.setAccessible(true);
@SuppressWarnings("unchecked") Map<String, Collector> namesToCollectors =
(Map<String, Collector>) namesToCollectorsField.get(registry);
if(!namesToCollectors.containsKey(name)) {
registry.register(collector);
}
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new RuntimeException(e);
}

return collector.labels(StreamSupport.stream(tags.spliterator(), false)
.map(Tag::getValue)
.collect(Collectors.toList())
Expand Down

0 comments on commit 676ff5a

Please sign in to comment.