Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port change for #1618 to 2.0 #1686

Merged
merged 1 commit into from
May 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,17 @@ public GrpcMetrics description(String description) {
return new GrpcMetrics(metricRule.description(description));
}

/**
* Set the display name to apply to the metric.
*
* @param displayName the display name to apply to the metric
* @return a {@link io.helidon.grpc.metrics.GrpcMetrics} interceptor
* @see org.eclipse.microprofile.metrics.Metadata
*/
public GrpcMetrics displayName(String displayName) {
return new GrpcMetrics(metricRule.displayName(displayName));
}

/**
* Set the units to apply to the metric.
*
Expand All @@ -137,6 +148,16 @@ public GrpcMetrics units(String units) {
return new GrpcMetrics(metricRule.units(units));
}

/**
* Set the reusability of the metric.
* @param reusable {@code true} if this metric may be reused
* @return a {@link io.helidon.grpc.metrics.GrpcMetrics} interceptor
* @see org.eclipse.microprofile.metrics.Metadata
*/
public GrpcMetrics reusable(boolean reusable) {
return new GrpcMetrics(metricRule.reusable(reusable));
}

/**
* Obtain the {@link org.eclipse.microprofile.metrics.MetricType}.
*
Expand Down Expand Up @@ -441,6 +462,13 @@ static class MetricsRules {
*/
private Optional<String> description = Optional.empty();

/**
* The display name of the metric.
*
* @see org.eclipse.microprofile.metrics.Metadata
*/
private String displayName;

/**
* The unit of the metric.
*
Expand All @@ -449,6 +477,12 @@ static class MetricsRules {
*/
private Optional<String> units = Optional.empty();

/**
* The reusability status of this metric.
* @see org.eclipse.microprofile.metrics.Metadata
*/
private boolean reusable;

/**
* The function to use to obtain the metric name.
*/
Expand All @@ -462,8 +496,10 @@ private MetricsRules(MetricsRules copy) {
this.type = copy.type;
this.tags = copy.tags;
this.description = copy.description;
this.displayName = copy.displayName;
this.units = copy.units;
this.nameFunction = copy.nameFunction;
this.reusable = copy.reusable;
}

/**
Expand All @@ -489,6 +525,11 @@ io.helidon.common.metrics.InternalBridge.Metadata metadata(ServiceDescriptor ser
this.description.ifPresent(builder::withDescription);
this.units.ifPresent(builder::withUnit);

String displayName = this.displayName;
builder.withDisplayName(displayName == null ? name : displayName);

builder = this.reusable ? builder.reusable() : builder.notReusable();

return builder.build();
}

Expand All @@ -508,6 +549,12 @@ private MetricsRules description(String description) {
return rules;
}

private MetricsRules displayName(String displayName) {
MetricsRules rules = new MetricsRules(this);
rules.displayName = displayName;
return rules;
}

private MetricsRules nameFunction(NamingFunction function) {
MetricsRules rules = new MetricsRules(this);
rules.nameFunction = Optional.of(function);
Expand All @@ -520,6 +567,12 @@ private MetricsRules units(String units) {
return rules;
}

private MetricsRules reusable(boolean reusable) {
MetricsRules rules = new MetricsRules(this);
rules.reusable = reusable;
return rules;
}

private Map<String, String> toTags() {
return tags.isPresent() ? tags.get() : Collections.emptyMap();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

import org.eclipse.microprofile.metrics.MetricType;
import org.eclipse.microprofile.metrics.annotation.Counted;
import org.eclipse.microprofile.metrics.annotation.Gauge;
import org.eclipse.microprofile.metrics.annotation.Metered;
import org.eclipse.microprofile.metrics.annotation.Timed;

Expand Down Expand Up @@ -118,6 +119,36 @@ private void addMetric(ServiceDescriptor.Builder builder,

MetricUtil.LookupResult<? extends Annotation> lookupResult
= MetricUtil.lookupAnnotation(method, annotation.annotationType(), annotatedClass);

if (annotation instanceof Metered) {
Metered metered = (Metered) annotation;
String displayName = metered.displayName().trim();
interceptor = interceptor.description(metered.description());
interceptor = interceptor.displayName(displayName.isEmpty() ? metricName : displayName);
interceptor = interceptor.reusable(metered.reusable());
interceptor = interceptor.units(metered.unit());
} else if (annotation instanceof Gauge) {
Gauge gauge = (Gauge) annotation;
String displayName = gauge.displayName().trim();
interceptor = interceptor.description(gauge.description());
interceptor = interceptor.displayName(displayName.isEmpty() ? metricName : displayName);
interceptor = interceptor.units(gauge.unit());
} else if (annotation instanceof Timed) {
Timed timed = (Timed) annotation;
String displayName = timed.displayName().trim();
interceptor = interceptor.description(timed.description());
interceptor = interceptor.displayName(displayName.isEmpty() ? metricName : displayName);
interceptor = interceptor.reusable(timed.reusable());
interceptor = interceptor.units(timed.unit());
} else if (annotation instanceof Counted) {
Counted counted = (Counted) annotation;
String displayName = counted.displayName().trim();
interceptor = interceptor.description(counted.description());
interceptor = interceptor.displayName(displayName.isEmpty() ? metricName : displayName);
interceptor = interceptor.reusable(counted.reusable());
interceptor = interceptor.units(counted.unit());
}

MetricsCdiExtension.registerMetric(method, annotatedClass, lookupResult);
builder.intercept(grpcMethodName, interceptor.nameFunction(new ConstantNamingFunction(metricName)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,15 @@
import java.util.List;
import java.util.logging.Logger;

import org.eclipse.microprofile.metrics.Metadata;
import org.eclipse.microprofile.metrics.MetricID;
import org.eclipse.microprofile.metrics.MetricRegistry;
import org.eclipse.microprofile.metrics.MetricType;
import org.eclipse.microprofile.metrics.Tag;
import org.eclipse.microprofile.metrics.annotation.ConcurrentGauge;
import org.eclipse.microprofile.metrics.annotation.Counted;
import org.eclipse.microprofile.metrics.annotation.Metered;
import org.eclipse.microprofile.metrics.annotation.Timed;

/**
* Class MetricUtil.
Expand Down Expand Up @@ -100,28 +107,113 @@ String getMetricName(E element, Class<?> clazz, MatchingType matchingType, Strin
methods = Arrays.asList(declaringClass.getDeclaredMethods());
}
}
result = declaringClass.getName() + "." + result;
result = declaringClass.getName() + '.' + result;
}
} else if (matchingType == MatchingType.CLASS) {
if (explicitName == null || explicitName.isEmpty()) {
result = getElementName(element, clazz);
if (!absolute) {
result = clazz.getName() + "." + result;
result = clazz.getName() + '.' + result;
}
} else {
// Absolute must be false at class level, issue warning here
if (absolute) {
LOGGER.warning(() -> "Attribute 'absolute=true' in metric annotation ignored at class level");
}
result = clazz.getPackage().getName() + "." + explicitName
+ "." + getElementName(element, clazz);
result = clazz.getPackage().getName() + '.' + explicitName
+ '.' + getElementName(element, clazz);
}
} else {
throw new InternalError("Unknown matching type");
}
return result;
}

/**
* Register a metric.
*
* @param element the annotated element
* @param clazz the annotated class
* @param lookupResult the annotation lookup result
* @param <E> the annotated element type
*/
public static <E extends Member & AnnotatedElement>
void registerMetric(E element, Class<?> clazz, LookupResult<? extends Annotation> lookupResult) {
registerMetric(element, clazz, lookupResult.getAnnotation(), lookupResult.getType());
}

/**
* Register a metric.
*
* @param element the annotated element
* @param clazz the annotated class
* @param annotation the annotation to register
* @param type the {@link MatchingType} indicating the type of annotated element
* @param <E> the annotated element type
*/
public static <E extends Member & AnnotatedElement>
void registerMetric(E element, Class<?> clazz, Annotation annotation, MatchingType type) {
MetricRegistry registry = getMetricRegistry();

if (annotation instanceof Counted) {
Counted counted = (Counted) annotation;
String metricName = getMetricName(element, clazz, type, counted.name().trim(), counted.absolute());
String displayName = counted.displayName().trim();
Metadata meta = Metadata.builder()
.withName(metricName)
.withDisplayName(displayName.isEmpty() ? metricName : displayName)
.withDescription(counted.description().trim())
.withType(MetricType.COUNTER)
.withUnit(counted.unit().trim())
.reusable(counted.reusable()).build();
registry.counter(meta);
LOGGER.fine(() -> "### Registered counter " + metricName);
} else if (annotation instanceof Metered) {
Metered metered = (Metered) annotation;
String metricName = getMetricName(element, clazz, type, metered.name().trim(), metered.absolute());
String displayName = metered.displayName().trim();
Metadata meta = Metadata.builder()
.withName(metricName)
.withDisplayName(displayName.isEmpty() ? metricName : displayName)
.withDescription(metered.description().trim())
.withType(MetricType.METERED)
.withUnit(metered.unit().trim())
.reusable(metered.reusable()).build();
registry.meter(meta);
LOGGER.fine(() -> "### Registered meter " + metricName);
} else if (annotation instanceof ConcurrentGauge) {
ConcurrentGauge concurrentGauge = (ConcurrentGauge) annotation;
String metricName = getMetricName(element, clazz, type, concurrentGauge.name().trim(),
concurrentGauge.absolute());
String displayName = concurrentGauge.displayName().trim();
Metadata meta = Metadata.builder()
.withName(metricName)
.withDisplayName(displayName.isEmpty() ? metricName : displayName)
.withDescription(concurrentGauge.description().trim())
.withType(MetricType.METERED)
.withUnit(concurrentGauge.unit().trim()).build();
registry.concurrentGauge(meta);
LOGGER.fine(() -> "### Registered ConcurrentGauge " + metricName);
} else if (annotation instanceof Timed) {
Timed timed = (Timed) annotation;
String metricName = getMetricName(element, clazz, type, timed.name().trim(), timed.absolute());
String displayName = timed.displayName().trim();
Metadata meta = Metadata.builder()
.withName(metricName)
.withDisplayName(displayName.isEmpty() ? metricName : displayName)
.withDescription(timed.description().trim())
.withType(MetricType.TIMER)
.withUnit(timed.unit().trim())
.reusable(timed.reusable()).build();
registry.timer(meta);
LOGGER.fine(() -> "### Registered timer " + metricName);
}
}

private static MetricRegistry getMetricRegistry() {
return RegistryProducer.getDefaultRegistry();
}

static <E extends Member & AnnotatedElement>
String getElementName(E element, Class<?> clazz) {
return element instanceof Constructor ? clazz.getSimpleName() : element.getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,46 +121,53 @@ void registerMetric(E element, Class<?> clazz, LookupResult<? extends Annotation

if (annotation instanceof Counted) {
Counted counted = (Counted) annotation;
String metricName = getMetricName(element, clazz, lookupResult.getType(), counted.name(), counted.absolute());
String metricName = getMetricName(element, clazz, lookupResult.getType(), counted.name().trim(),
counted.absolute());
String displayName = counted.displayName().trim();
Metadata meta = new HelidonMetadata(metricName,
counted.displayName(),
counted.description(),
displayName.isEmpty() ? metricName : displayName,
counted.description().trim(),
MetricType.COUNTER,
counted.unit(),
counted.unit().trim(),
counted.reusable());
registry.counter(meta, tags(counted.tags()));
LOGGER.log(Level.FINE, () -> "Registered counter " + metricName);
} else if (annotation instanceof Metered) {
Metered metered = (Metered) annotation;
String metricName = getMetricName(element, clazz, lookupResult.getType(), metered.name(), metered.absolute());
String metricName = getMetricName(element, clazz, lookupResult.getType(), metered.name().trim(),
metered.absolute());
String displayName = metered.displayName().trim();
Metadata meta = new HelidonMetadata(metricName,
metered.displayName(),
metered.description(),
displayName.isEmpty() ? metricName : displayName,
metered.description().trim(),
MetricType.METERED,
metered.unit(),
metered.unit().trim(),
metered.reusable());
registry.meter(meta, tags(metered.tags()));
LOGGER.log(Level.FINE, () -> "Registered meter " + metricName);
} else if (annotation instanceof Timed) {
Timed timed = (Timed) annotation;
String metricName = getMetricName(element, clazz, lookupResult.getType(), timed.name(), timed.absolute());
String metricName = getMetricName(element, clazz, lookupResult.getType(), timed.name().trim(),
timed.absolute());
String displayName = timed.displayName().trim();
Metadata meta = new HelidonMetadata(metricName,
timed.displayName(),
timed.description(),
displayName.isEmpty() ? metricName : displayName,
timed.description().trim(),
MetricType.TIMER,
timed.unit(),
timed.unit().trim(),
timed.reusable());
registry.timer(meta, tags(timed.tags()));
LOGGER.log(Level.FINE, () -> "Registered timer " + metricName);
} else if (annotation instanceof ConcurrentGauge) {
ConcurrentGauge concurrentGauge = (ConcurrentGauge) annotation;
String metricName = getMetricName(element, clazz, lookupResult.getType(), concurrentGauge.name(),
concurrentGauge.absolute());
String metricName = getMetricName(element, clazz, lookupResult.getType(), concurrentGauge.name().trim(),
concurrentGauge.absolute());
String displayName = concurrentGauge.displayName().trim();
Metadata meta = new HelidonMetadata(metricName,
concurrentGauge.displayName(),
concurrentGauge.description(),
displayName.isEmpty() ? metricName : displayName,
concurrentGauge.description().trim(),
MetricType.CONCURRENT_GAUGE,
concurrentGauge.unit(),
concurrentGauge.unit().trim(),
concurrentGauge.reusable());
registry.concurrentGauge(meta, tags(concurrentGauge.tags()));
LOGGER.log(Level.FINE, () -> "Registered concurrent gauge " + metricName);
Expand Down