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

add copy() method to Builders #4104

Closed
wants to merge 2 commits into from
Closed
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 @@ -18,6 +18,7 @@
import io.micrometer.common.lang.Nullable;
import io.micrometer.core.instrument.distribution.DistributionStatisticConfig;
import io.micrometer.core.instrument.distribution.pause.PauseDetector;
import io.micrometer.core.instrument.internal.Copyable;

import java.time.Duration;
import java.util.Arrays;
Expand All @@ -30,11 +31,11 @@
* @since 1.6.0
*/
@SuppressWarnings("unchecked")
public abstract class AbstractTimerBuilder<B extends AbstractTimerBuilder<B>> {
public abstract class AbstractTimerBuilder<B extends AbstractTimerBuilder<B>> implements Copyable<B> {

protected final String name;

protected Tags tags = Tags.empty();
protected Tags tags;

protected final DistributionStatisticConfig.Builder distributionConfigBuilder;

Expand All @@ -46,11 +47,20 @@ public abstract class AbstractTimerBuilder<B extends AbstractTimerBuilder<B>> {

protected AbstractTimerBuilder(String name) {
this.name = name;
this.distributionConfigBuilder = new DistributionStatisticConfig.Builder();
this.tags = Tags.empty();
this.distributionConfigBuilder = DistributionStatisticConfig.builder();
minimumExpectedValue(Duration.ofMillis(1));
maximumExpectedValue(Duration.ofSeconds(30));
}

protected AbstractTimerBuilder(B builder) {
this.name = builder.name;
this.tags = builder.tags.copy();
this.distributionConfigBuilder = builder.distributionConfigBuilder.copy();
this.description = builder.description;
this.pauseDetector = builder.pauseDetector;
}

/**
* @param tags Must be an even number of arguments representing key/value pairs of
* tags.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package io.micrometer.core.instrument;

import io.micrometer.common.lang.Nullable;
import io.micrometer.core.instrument.internal.Copyable;

import java.util.Collections;

Expand Down Expand Up @@ -57,20 +58,33 @@ default Iterable<Measurement> measure() {
/**
* Fluent builder for counters.
*/
class Builder {
class Builder implements Copyable<Builder> {

private final String name;

private Tags tags = Tags.empty();
private Tags tags;

@Nullable
private String description;

@Nullable
private String baseUnit;

private Builder(String name) {
protected Builder(String name) {
this.name = name;
this.tags = Tags.empty();
}

protected Builder(Builder builder) {
this.name = builder.name;
this.tags = builder.tags.copy();
this.description = builder.description;
this.baseUnit = builder.baseUnit;
}

@Override
public Builder copy() {
return new Builder(this);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import io.micrometer.core.instrument.distribution.DistributionStatisticConfig;
import io.micrometer.core.instrument.distribution.HistogramSupport;
import io.micrometer.core.instrument.distribution.ValueAtPercentile;
import io.micrometer.core.instrument.internal.Copyable;

import java.time.Duration;
import java.util.Arrays;
Expand Down Expand Up @@ -112,13 +113,13 @@ default Iterable<Measurement> measure() {
/**
* Fluent builder for distribution summaries.
*/
class Builder {
class Builder implements Copyable<Builder> {

private final String name;

private Tags tags = Tags.empty();
private Tags tags;

private DistributionStatisticConfig.Builder distributionConfigBuilder = DistributionStatisticConfig.builder();
private final DistributionStatisticConfig.Builder distributionConfigBuilder;

@Nullable
private String description;
Expand All @@ -128,8 +129,24 @@ class Builder {

private double scale = 1.0;

private Builder(String name) {
protected Builder(String name) {
this.name = name;
this.tags = Tags.empty();
this.distributionConfigBuilder = DistributionStatisticConfig.builder();
}

protected Builder(Builder builder) {
this.name = builder.name;
this.tags = builder.tags.copy();
this.distributionConfigBuilder = builder.distributionConfigBuilder.copy();
this.description = builder.description;
this.baseUnit = builder.baseUnit;
this.scale = builder.scale;
}

@Override
public Builder copy() {
return new Builder(this);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package io.micrometer.core.instrument;

import io.micrometer.common.lang.Nullable;
import io.micrometer.core.instrument.internal.Copyable;

import java.util.Collections;
import java.util.function.ToDoubleFunction;
Expand Down Expand Up @@ -46,13 +47,13 @@ default Iterable<Measurement> measure() {
*
* @param <T> The type of the state object from which the counter value is extracted.
*/
class Builder<T> {
class Builder<T> implements Copyable<Builder<T>> {

private final String name;

private final ToDoubleFunction<T> f;

private Tags tags = Tags.empty();
private Tags tags;

@Nullable
private final T obj;
Expand All @@ -63,10 +64,25 @@ class Builder<T> {
@Nullable
private String baseUnit;

private Builder(String name, @Nullable T obj, ToDoubleFunction<T> f) {
protected Builder(String name, @Nullable T obj, ToDoubleFunction<T> f) {
this.name = name;
this.obj = obj;
this.f = f;
this.tags = Tags.empty();
}

protected Builder(Builder<T> builder) {
this.name = builder.name;
this.f = builder.f;
this.tags = builder.tags.copy();
this.obj = builder.obj;
this.description = builder.description;
this.baseUnit = builder.baseUnit;
}

@Override
public Builder<T> copy() {
return new Builder<>(this);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package io.micrometer.core.instrument;

import io.micrometer.common.lang.Nullable;
import io.micrometer.core.instrument.internal.Copyable;

import java.util.Arrays;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -73,7 +74,7 @@ default Iterable<Measurement> measure() {
*
* @param <T> The type of the state object from which the timer values are extracted.
*/
class Builder<T> {
class Builder<T> implements Copyable<Builder<T>> {

private final String name;

Expand All @@ -83,21 +84,37 @@ class Builder<T> {

private final TimeUnit totalTimeFunctionUnit;

private Tags tags = Tags.empty();
private Tags tags;

@Nullable
private final T obj;

@Nullable
private String description;

private Builder(String name, @Nullable T obj, ToLongFunction<T> countFunction,
protected Builder(String name, @Nullable T obj, ToLongFunction<T> countFunction,
ToDoubleFunction<T> totalTimeFunction, TimeUnit totalTimeFunctionUnit) {
this.name = name;
this.obj = obj;
this.countFunction = countFunction;
this.totalTimeFunction = totalTimeFunction;
this.totalTimeFunctionUnit = totalTimeFunctionUnit;
this.tags = Tags.empty();
}

protected Builder(Builder<T> builder) {
this.name = builder.name;
this.countFunction = builder.countFunction;
this.totalTimeFunction = builder.totalTimeFunction;
this.totalTimeFunctionUnit = builder.totalTimeFunctionUnit;
this.tags = builder.tags.copy();
this.obj = builder.obj;
this.description = builder.description;
}

@Override
public Builder<T> copy() {
return new Builder<>(this);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import io.micrometer.common.lang.Nullable;
import io.micrometer.core.annotation.Incubating;
import io.micrometer.core.instrument.distribution.HistogramGauges;
import io.micrometer.core.instrument.internal.Copyable;

import java.util.Collections;
import java.util.function.Supplier;
Expand Down Expand Up @@ -77,13 +78,13 @@ default Iterable<Measurement> measure() {
*
* @param <T> The type of the state object from which the gauge value is extracted.
*/
class Builder<T> {
class Builder<T> implements Copyable<Builder<T>> {

private final String name;

private final ToDoubleFunction<T> f;

private Tags tags = Tags.empty();
private Tags tags;

private boolean strongReference = false;

Expand All @@ -99,10 +100,27 @@ class Builder<T> {
@Nullable
private String baseUnit;

private Builder(String name, @Nullable T obj, ToDoubleFunction<T> f) {
protected Builder(String name, @Nullable T obj, ToDoubleFunction<T> f) {
this.name = name;
this.obj = obj;
this.f = f;
this.tags = Tags.empty();
}

protected Builder(Builder<T> builder) {
this.name = builder.name;
this.f = builder.f;
this.tags = builder.tags.copy();
this.strongReference = builder.strongReference;
this.syntheticAssociation = builder.syntheticAssociation;
this.obj = builder.obj;
this.description = builder.description;
this.baseUnit = builder.baseUnit;
}

@Override
public Builder<T> copy() {
return new Builder<>(this);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import io.micrometer.core.annotation.Timed;
import io.micrometer.core.instrument.distribution.DistributionStatisticConfig;
import io.micrometer.core.instrument.distribution.HistogramSupport;
import io.micrometer.core.instrument.internal.Copyable;

import java.time.Duration;
import java.util.Arrays;
Expand Down Expand Up @@ -289,23 +290,37 @@ abstract class Sample {
/**
* Fluent builder for long task timers.
*/
class Builder {
class Builder implements Copyable<Builder> {

private final String name;

private Tags tags = Tags.empty();
private Tags tags;

private final DistributionStatisticConfig.Builder distributionConfigBuilder = new DistributionStatisticConfig.Builder();
private final DistributionStatisticConfig.Builder distributionConfigBuilder;

@Nullable
private String description;

private Builder(String name) {
protected Builder(String name) {
this.name = name;
this.tags = Tags.empty();
this.distributionConfigBuilder = DistributionStatisticConfig.builder();
minimumExpectedValue(Duration.ofMinutes(2));
maximumExpectedValue(Duration.ofHours(2));
}

protected Builder(Builder builder) {
this.name = builder.name;
this.tags = builder.tags.copy();
this.distributionConfigBuilder = builder.distributionConfigBuilder.copy();
this.description = builder.description;
}

@Override
public Builder copy() {
return new Builder(this);
}

/**
* @param tags Must be an even number of arguments representing key/value pairs of
* tags.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import io.micrometer.common.lang.Nullable;
import io.micrometer.core.annotation.Incubating;
import io.micrometer.core.instrument.internal.Copyable;

import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;
Expand Down Expand Up @@ -124,20 +125,33 @@ public static Row<Supplier<Number>> of(Tags uniqueTags, Supplier<Number> valueFu
/**
* Fluent builder for multi-gauges.
*/
public static class Builder {
public static class Builder implements Copyable<Builder> {

private final String name;

private Tags tags = Tags.empty();
private Tags tags;

@Nullable
private String description;

@Nullable
private String baseUnit;

private Builder(String name) {
protected Builder(String name) {
this.name = name;
this.tags = Tags.empty();
}

protected Builder(Builder builder) {
this.name = builder.name;
this.tags = builder.tags.copy();
this.description = builder.description;
this.baseUnit = builder.baseUnit;
}

@Override
public Builder copy() {
return new Builder(this);
}

/**
Expand Down
Loading