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); }
public boolean isReusable() { return value.isReusable(); }
@Override public <T extends Metric> T register(final Metadata metadata, final T metric) throws IllegalArgumentException { final Holder<? extends Metric> holder = metrics.putIfAbsent(metadata.getName(), new Holder<>(metric, metadata)); if (holder != null && !metadata.isReusable() && !holder.metadata.isReusable()) { throw new IllegalArgumentException("'" + metadata.getName() + "' metric already exists and is not reusable"); } return metric; }
@Override public Histogram histogram(final Metadata metadata) { Holder<? extends Metric> holder = metrics.get(metadata.getName()); if (holder == null) { holder = new Holder<>(new HistogramImpl(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 (!Histogram.class.isInstance(holder.metric)) { throw new IllegalArgumentException(holder.metric + " is not a histogram"); } return Histogram.class.cast(holder.metric); }
@Override public Counter counter(final Metadata metadata) { Holder<? extends Metric> holder = metrics.get(metadata.getName()); if (holder == null) { holder = new Holder<>(new CounterImpl(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 (!Counter.class.isInstance(holder.metric)) { throw new IllegalArgumentException(holder.metric + " is not a counter"); } return Counter.class.cast(holder.metric); }
@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 Timer timer(final Metadata metadata) { Holder<? extends Metric> holder = metrics.get(metadata.getName()); if (holder == null) { holder = new Holder<>(new TimerImpl(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 (!Timer.class.isInstance(holder.metric)) { throw new IllegalArgumentException(holder.metric + " is not a timer"); } return Timer.class.cast(holder.metric); }
@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 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 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; }