Skip to content

Commit

Permalink
Issue #735, remove MetricType class
Browse files Browse the repository at this point in the history
Signed-off-by: Jonathan Gallimore <jgallimore@tomitribe.com>
  • Loading branch information
jgallimore committed Sep 8, 2022
1 parent 08b6286 commit 549d409
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 215 deletions.
Expand Up @@ -47,13 +47,6 @@ public class DefaultMetadata implements Metadata {
*/
private final String description;

/**
* Type of the metric.
* <p>
* A required field which holds the type of the metric object.
* </p>
*/
private final MetricType type;
/**
* Unit of the metric.
* <p>
Expand All @@ -62,11 +55,9 @@ public class DefaultMetadata implements Metadata {
*/
private final String unit;

protected DefaultMetadata(String name, String description,
MetricType type, String unit) {
protected DefaultMetadata(String name, String description, String unit) {
this.name = name;
this.description = description;
this.type = type;
this.unit = unit;
}

Expand All @@ -85,16 +76,6 @@ public Optional<String> description() {
return Optional.ofNullable(description);
}

@Override
public String getType() {
return Optional.ofNullable(type).orElse(MetricType.INVALID).toString();
}

@Override
public MetricType getTypeRaw() {
return Optional.ofNullable(type).orElse(MetricType.INVALID);
}

@Override
public String getUnit() {
return unit().orElse(MetricUnits.NONE);
Expand All @@ -118,20 +99,18 @@ public boolean equals(Object o) {
// Use getters to compare the effective values
return Objects.equals(name, that.getName()) &&
Objects.equals(this.getDescription(), that.getDescription()) &&
Objects.equals(this.getUnit(), that.getUnit()) &&
Objects.equals(this.getTypeRaw(), that.getTypeRaw());
Objects.equals(this.getUnit(), that.getUnit());
}

@Override
public int hashCode() {
return Objects.hash(name, description, unit, type);
return Objects.hash(name, description, unit);
}

@Override
public String toString() {
final StringBuilder sb = new StringBuilder("DefaultMetadata{");
sb.append("name='").append(name).append('\'');
sb.append(", type=").append(type);
sb.append(", unit='").append(unit).append('\'');
if (description != null) {
sb.append(", description='").append(description).append('\'');
Expand Down
16 changes: 0 additions & 16 deletions api/src/main/java/org/eclipse/microprofile/metrics/Metadata.java
Expand Up @@ -33,7 +33,6 @@
* <ul>
* <li>{@code Name}: (Required) The name of the metric.</li>
* <li>{@code Description}: (Optional) A human readable description of the metric.</li>
* <li>{@code Type}: (Required) The type of the metric. See {@link MetricType}.</li>
* <li>{@code Unit}: (Optional) The unit of the metric. The unit may be any unit specified as a String or one specified
* in {@link MetricUnits}.</li>
* </ul>
Expand All @@ -58,21 +57,6 @@ public interface Metadata {

Optional<String> description();

/**
* Returns the String representation of the {@link MetricType}.
*
* @return the MetricType as a String
* @see MetricType
*/
String getType();

/**
* Returns the {@link MetricType} of the metric if set, otherwise it returns {@link MetricType#INVALID}
*
* @return the {@link MetricType}
*/
MetricType getTypeRaw();

/**
* Returns the unit of this metric if set, otherwise this method returns {@link MetricUnits#NONE}
*
Expand Down
Expand Up @@ -36,13 +36,10 @@ public class MetadataBuilder {

private String description;

private MetricType type;

private String unit;

MetadataBuilder(Metadata metadata) {
this.name = metadata.getName();
this.type = metadata.getTypeRaw();
metadata.description().ifPresent(this::withDescription);
metadata.unit().ifPresent(this::withUnit);
}
Expand Down Expand Up @@ -82,18 +79,6 @@ public MetadataBuilder withDescription(String description) {
return this;
}

/**
* Sets the type.
*
* @param type
* the type, {@link MetricType#INVALID} is considered as "not present" (null)
* @return the builder instance
*/
public MetadataBuilder withType(MetricType type) {
this.type = MetricType.INVALID == type ? null : type;
return this;
}

/**
* Sets the unit.
*
Expand All @@ -116,6 +101,6 @@ public Metadata build() {
throw new IllegalStateException("Name is required");
}

return new DefaultMetadata(name, description, type, unit);
return new DefaultMetadata(name, description, unit);
}
}
128 changes: 0 additions & 128 deletions api/src/main/java/org/eclipse/microprofile/metrics/MetricType.java

This file was deleted.

Expand Up @@ -28,7 +28,6 @@
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.Metric;
import org.eclipse.microprofile.metrics.annotation.RegistryScope;
Expand Down Expand Up @@ -97,7 +96,7 @@ public void useExistingMetaDataTest() {

// first to register a "complex" metadata
metrics.counter(Metadata.builder().withName(metricName)
.withType(MetricType.COUNTER).build(), orangeTag);
.build(), orangeTag);

// creates with a simple/template metadata or uses an existing one.
metrics.counter(metricName, purpleTag);
Expand All @@ -119,29 +118,4 @@ public void testMetricRegistryScope() {
private void assertExists(Class<? extends org.eclipse.microprofile.metrics.Metric> expected, MetricID metricID) {
assertNotNull("Metric expected to exist but was undefined: " + metricID, metrics.getMetric(metricID, expected));
}

/**
* The implementation has to sanitize Metadata passed to registration methods if the application does not supply the
* metric type explicitly, but the type is implied by the used method.
*/
@Test
@InSequence(6)
public void sanitizeMetadataTest() {
Metadata metadata = Metadata.builder().withName("metric1").build();
metrics.counter(metadata);
Metadata actualMetadata = metrics.getMetadata("metric1");
Assert.assertEquals(MetricType.COUNTER, actualMetadata.getTypeRaw());
}

/**
* if there is a mismatch because the type specified in the `Metadata` is different than the one implied by the
* method name, an exception must be thrown
*/
@Test(expected = Exception.class)
@InSequence(7)
public void conflictingMetadataTest() {
Metadata metadata = Metadata.builder().withName("metric1").withType(MetricType.COUNTER).build();
metrics.timer(metadata);
}

}
Expand Up @@ -30,7 +30,6 @@
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.MetricUnits;
import org.eclipse.microprofile.metrics.Timer;
import org.eclipse.microprofile.metrics.annotation.Counted;
Expand Down Expand Up @@ -183,7 +182,7 @@ public void gaugeMe() {
};

Metadata metadata = Metadata.builder().withName("metricTest.test1.gauge")
.withType(MetricType.GAUGE).withUnit(MetricUnits.GIGABYTES).build();
.withUnit(MetricUnits.GIGABYTES).build();
metrics.gauge(metadata, gaugeSupp);
}

Expand All @@ -202,7 +201,7 @@ public long gaugeMeB() {
public void histogramMe() {

Metadata metadata = Metadata.builder().withName("metricTest.test1.histogram")
.withType(MetricType.HISTOGRAM).withUnit(MetricUnits.BYTES).build();
.withUnit(MetricUnits.BYTES).build();
Histogram histogram = metrics.histogram(metadata);

// Go both ways to minimize error due to decay
Expand All @@ -212,7 +211,7 @@ public void histogramMe() {
}

Metadata metadata2 = Metadata.builder().withName("metricTest.test1.histogram2")
.withType(MetricType.HISTOGRAM).withUnit(MetricUnits.NONE).build();
.withUnit(MetricUnits.NONE).build();
Histogram histogram2 = metrics.histogram(metadata2);
histogram2.update(1);
}
Expand Down

0 comments on commit 549d409

Please sign in to comment.