static Metadata gcTimeMeta(String poolName) { return new Metadata("gc." + poolName + ".time", "Garbage Collection Time", "Displays the approximate accumulated collection elapsed time in milliseconds. " + "This attribute displays -1 if the collection elapsed time is undefined for this " + "collector. The Java virtual machine implementation may use a high resolution " + "timer to measure the elapsed time. This attribute may display the same value " + "even if the collection count has been incremented if the collection elapsed " + "time is very short.", MetricType.GAUGE, MetricUnits.MILLISECONDS); }
MetricImpl(String registryType, Metadata metadata) { super(metadata.getName(), metadata.getDisplayName(), metadata.getDescription(), metadata.getTypeRaw(), metadata.getUnit(), tagsToSimpleString(metadata)); this.registryType = registryType; }
private static String tagsToSimpleString(Metadata metadata) { // add tags HashMap<String, String> tags = metadata.getTags(); if (tags.isEmpty()) { return ""; } else { StringBuilder sb = new StringBuilder(); for (Map.Entry<String, String> entry : tags.entrySet()) { sb.append(entry.getKey()).append("=").append(entry.getValue()); sb.append(","); } sb.delete(sb.length() - 1, sb.length()); return sb.toString(); } }
private <T extends Metric, I extends MetricImpl> T getMetric(Metadata metadata, Class<T> type, Function<String, I> newInstanceCreator) { MetricImpl metric = allMetrics.get(metadata.getName()); if (metric != null) { if (metric.isReusable() != metadata.isReusable()) { throw new IllegalArgumentException("Metadata not re-usable for metric " + metadata.getName()); } } else { metric = newInstanceCreator.apply(metadata.getName()); metric.setReusable(metadata.isReusable()); allMetrics.put(metadata.getName(), metric); } if (!(type.isAssignableFrom(metric.getClass()))) { throw new IllegalArgumentException("Attempting to get " + metadata.getType() + ", but metric registered under this name is " + metric); } return type.cast(metric); }
private void registerAnnotatedGauges(@Observes AfterDeploymentValidation adv, BeanManager bm) { LOGGER.log(Level.FINE, () -> "### registerGauges"); MetricRegistry registry = getMetricRegistry(); annotatedGaugeSites.entrySet().forEach(gaugeSite -> { LOGGER.log(Level.FINE, () -> "### gaugeSite " + gaugeSite.toString()); String gaugeName = gaugeSite.getKey(); AnnotatedMethodConfigurator<?> site = gaugeSite.getValue(); DelegatingGauge<?> dg = buildDelegatingGauge(gaugeName, site, bm); Gauge gaugeAnnotation = site.getAnnotated().getAnnotation(Gauge.class); Metadata md = new Metadata(gaugeName, gaugeAnnotation.displayName(), gaugeAnnotation.description(), MetricType.GAUGE, gaugeAnnotation.unit(), toTags(gaugeAnnotation.tags())); LOGGER.log(Level.FINE, () -> String.format("### Registering gauge with metadata %s", md.toString())); registry.register(md, dg); }); annotatedGaugeSites.clear(); }
private Holder(final T metric, final Metadata metadata) { this.metric = metric; this.metadata = new Metadata(metadata.getName(), metadata.getDisplayName(), metadata.getDescription(), metadata.getTypeRaw(), metadata.getUnit()); this.metadata.setReusable(metadata.isReusable()); this.metadata.setTags(metadata.getTags()); } }
@Override @Deprecated public <T extends Metric> T register(String name, T t, Metadata metadata) throws IllegalArgumentException { Metadata metadataCopy = new Metadata(name, metadata.getDisplayName(), metadata.getDescription(), metadata.getTypeRaw(), metadata.getUnit(), metadata.getTagsAsString()); metadataCopy.setReusable(metadata.isReusable()); metadataCopy.setName(name); return register(metadataCopy, t); }
private StringBuilder toPrometheus(final String registryKey, final String keyBase, final String keyUnit, final Snapshot snapshot, final Metadata metadata) { final Function<Map<String, String>, Metadata> metaFactory = tag -> new Metadata(metadata.getName(), metadata.getDisplayName(), metadata.getDescription(), metadata.getTypeRaw(), metadata.getUnit(), Stream.concat(metadata.getTags().entrySet().stream(), tag.entrySet().stream()) .map(e -> e.getKey() + '=' + e.getValue()) .collect(joining(","))); final String completeKey = keyBase + keyUnit; return new StringBuilder() .append(value(registryKey, keyBase + "_min" + keyUnit, snapshot.getMin(), metadata)) .append(value(registryKey, keyBase + "_max" + keyUnit, snapshot.getMax(), metadata)) .append(value(registryKey, keyBase + "_mean" + keyUnit, snapshot.getMean(), metadata)) .append(value(registryKey, keyBase + "_stddev" + keyUnit, snapshot.getStdDev(), metadata)) .append(value(registryKey, completeKey, snapshot.getMedian(), metaFactory.apply(singletonMap("quantile", "0.5")))) .append(value(registryKey, completeKey, snapshot.get75thPercentile(), metaFactory.apply(singletonMap("quantile", "0.75")))) .append(value(registryKey, completeKey, snapshot.get95thPercentile(), metaFactory.apply(singletonMap("quantile", "0.95")))) .append(value(registryKey, completeKey, snapshot.get98thPercentile(), metaFactory.apply(singletonMap("quantile", "0.98")))) .append(value(registryKey, completeKey, snapshot.get99thPercentile(), metaFactory.apply(singletonMap("quantile", "0.99")))) .append(value(registryKey, completeKey, snapshot.get999thPercentile(), metaFactory.apply(singletonMap("quantile", "0.999")))); }
static Metadata metadataOf(String name, MetricType metricType) { Metadata res = new Metadata(name, metricType); res.setReusable(true); return res; }
@Override public Meter meter(final Metadata metadata) { Holder<? extends Metric> holder = metrics.get(metadata.getName()); if (holder == null) { holder = new Holder<>(new MeterImpl(metadata.getUnit()), metadata); final Holder<? extends Metric> existing = metrics.putIfAbsent(metadata.getName(), holder); if (existing != null) { holder = existing; } } else if (!metadata.isReusable()) { throw new IllegalArgumentException("Metric " + metadata.getName() + " already exists and is not set as reusable"); } else if (!holder.metadata.isReusable()) { throw new IllegalArgumentException("Metric " + metadata.getName() + " already exists and was not set as reusable"); } if (!Meter.class.isInstance(holder.metric)) { throw new IllegalArgumentException(holder.metric + " is not a meter"); } return Meter.class.cast(holder.metric); }
@Override public void serialize(Metadata metadata, JsonGenerator json, SerializerProvider provider) throws IOException { json.writeStartObject(); json.writeStringField("unit", metadata.getUnit()); json.writeStringField("type", metadata.getType()); json.writeStringField("description", metadata.getDescription()); json.writeStringField("displayName", metadata.getDisplayName()); json.writeStringField("tags", getTagsAsStringNoQuotes(metadata.getTags())); json.writeEndObject(); }
@Override public Counter counter(Metadata metadata) { return counter(metadata.getName()); }
Metadata metadata = new Metadata(finalName, type); List<String> missingEqualSign = Arrays.stream(tags) .filter(tag -> tag != null && !tag.isEmpty() && !tag.contains("=")) metadata.setDisplayName(displayName); metadata.setDescription(description); metadata.setUnit(unit); metadata.setReusable(reusable);
@Override public <T extends Metric> T register(Metadata metadata, T t) throws IllegalArgumentException { // add default tags ServiceConfigInfo configInfo = ServiceConfigInfo.getInstance(); if(configInfo.shouldAddToTags()) { metadata.addTag("environment=" + configInfo.getEnvironment()); metadata.addTag("serviceName=" + configInfo.getServiceName()); metadata.addTag("serviceVersion=" + configInfo.getServiceVersion()); metadata.addTag("instanceId=" + configInfo.getInstanceId()); } Optional<String> tagsFromConfig = ConfigurationUtil.getInstance().get("MP_METRICS_TAGS"); tagsFromConfig.ifPresent(metadata::addTags); try { metricRegistry.register(metadata.getName(), new MetricAdapter(t, metadata)); } catch (IllegalArgumentException e) { com.codahale.metrics.Metric m = metricRegistry.getMetrics().get(metadata.getName()); if (m instanceof MetricAdapter) { Metadata existingMetadata = ((MetricAdapter) m).getMetadata(); if (existingMetadata.isReusable() && metadata.isReusable() && existingMetadata.getTypeRaw().equals(metadata.getTypeRaw())) { return t; } else { throw e; } } } return t; }
/** * Constructs a Metadata object from a map with the following keys * <ul> * <li>{@code name} - The name of the metric</li> * <li>{@code displayName} - The display (friendly) name of the metric</li> * <li>{@code description} - The description of the metric</li> * <li>{@code type} - The type of the metric</li> * <li>{@code unit} - The units of the metric</li> * <li>{@code tags} - The tags of the metric - cannot be null</li> * <li>{@code reusable} - If <tt>true</tt>, this metric name is permitted to be used at multiple registration points. If * <tt>false</tt>, this metric name is only permitted to be used at one registration point per MetricRegistry.</li> * </ul> * * @param in a map of key/value pairs representing Metadata */ public Metadata(Map<String, String> in) { this(); this.name = (String) in.get("name"); this.description = (String) in.get("description"); this.displayName = (String) in.get("displayName"); this.setType((String) in.get("type")); this.setUnit((String) in.get("unit")); if (in.keySet().contains("tags")) { String tagString = (String) in.get("tags"); addTags(tagString); } this.setReusable(Boolean.parseBoolean(in.get("reusable"))); }
if (metricMetaData.getDescription() == null || metricMetaData.getDescription().trim().isEmpty()) { description = ""; } else { description = metricMetaData.getDescription().trim(); String tags = metricMetaData.getTagsAsString(); String unit = metricMetaData.getUnit();
@Test public void addTagsTest() { metadata.addTags("colour=green,size=medium"); metadata.addTag("number=5"); Assert.assertNotNull(metadata); Assert.assertTrue(metadata.getTags().containsKey("size")); Assert.assertTrue(metadata.getTags().containsValue("green")); Assert.assertFalse(metadata.getTags().containsValue("blue")); Assert.assertTrue(metadata.getTags().containsKey("number")); }
public String getTypeRaw() { return value.getTypeRaw().name(); }
private StringBuilder value(final String registryKey, final String key, final double value, final Metadata metadata) { final String builtKey = registryKey + ':' + key; return new StringBuilder() .append(type(registryKey, key, metadata)) .append(keyMapping.getOrDefault(builtKey, builtKey)) .append(of(metadata.getTags()) .filter(t -> !t.isEmpty()) .map(t -> t.entrySet().stream() .map(e -> e.getKey() + "=\"" + e.getValue() + "\"") .collect(joining(",", "{", "}"))) .orElse("")) .append(' ').append(toPrometheusValue(metadata.getUnit(), value)).append("\n"); }
void afterDeploymentValidation(@Observes final AfterDeploymentValidation afterDeploymentValidation, final BeanManager beanManager) { registrations.values().stream().filter(m -> m.getTypeRaw() == MetricType.GAUGE) .forEach(registration -> { final Gauge<?> gauge = gaugeFactories.get(registration.getName()).apply(beanManager); applicationRegistry.register(registration, gauge); }); producersRegistrations.forEach(Runnable::run); producersRegistrations.clear(); gaugeFactories.clear(); registrations.clear(); // mainly for tck, to drop if we add real vendor metrics vendorRegistry.counter("startTime").inc(System.currentTimeMillis()); if (!Boolean.getBoolean("geronimo.metrics.base.skip")) { new BaseMetrics(baseRegistry).register(); } }