-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1061 from Ladicek/apply-fault-tolerance-metrics
separate metrics of @ApplyFaultTolerance for each method
- Loading branch information
Showing
21 changed files
with
575 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
...va/io/smallrye/faulttolerance/core/metrics/DelegatingCompletionStageMetricsCollector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package io.smallrye.faulttolerance.core.metrics; | ||
|
||
import java.util.concurrent.CompletionStage; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.ConcurrentMap; | ||
|
||
import io.smallrye.faulttolerance.core.FaultToleranceStrategy; | ||
import io.smallrye.faulttolerance.core.InvocationContext; | ||
|
||
public class DelegatingCompletionStageMetricsCollector<V> implements FaultToleranceStrategy<CompletionStage<V>> { | ||
private final FaultToleranceStrategy<CompletionStage<V>> delegate; | ||
private final MetricsProvider provider; | ||
private final MeteredOperation originalOperation; | ||
|
||
private final ConcurrentMap<MeteredOperation, CompletionStageMetricsCollector<V>> cache = new ConcurrentHashMap<>(); | ||
|
||
public DelegatingCompletionStageMetricsCollector(FaultToleranceStrategy<CompletionStage<V>> delegate, | ||
MetricsProvider provider, MeteredOperation originalOperation) { | ||
this.delegate = delegate; | ||
this.provider = provider; | ||
this.originalOperation = originalOperation; | ||
} | ||
|
||
@Override | ||
public CompletionStage<V> apply(InvocationContext<CompletionStage<V>> ctx) throws Exception { | ||
MeteredOperationName name = ctx.get(MeteredOperationName.class); | ||
MeteredOperation operation = name != null | ||
? new DelegatingMeteredOperation(originalOperation, name.get()) | ||
: originalOperation; | ||
CompletionStageMetricsCollector<V> delegate = cache.computeIfAbsent(operation, | ||
ignored -> new CompletionStageMetricsCollector<>(this.delegate, provider.create(operation), operation)); | ||
return delegate.apply(ctx); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
...ore/src/main/java/io/smallrye/faulttolerance/core/metrics/DelegatingMeteredOperation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package io.smallrye.faulttolerance.core.metrics; | ||
|
||
final class DelegatingMeteredOperation implements MeteredOperation { | ||
private final MeteredOperation operation; | ||
private final String name; | ||
|
||
DelegatingMeteredOperation(MeteredOperation operation, String name) { | ||
this.operation = operation; | ||
this.name = name; | ||
} | ||
|
||
@Override | ||
public boolean isAsynchronous() { | ||
return operation.isAsynchronous(); | ||
} | ||
|
||
@Override | ||
public boolean hasBulkhead() { | ||
return operation.hasBulkhead(); | ||
} | ||
|
||
@Override | ||
public boolean hasCircuitBreaker() { | ||
return operation.hasCircuitBreaker(); | ||
} | ||
|
||
@Override | ||
public boolean hasFallback() { | ||
return operation.hasFallback(); | ||
} | ||
|
||
@Override | ||
public boolean hasRateLimit() { | ||
return operation.hasRateLimit(); | ||
} | ||
|
||
@Override | ||
public boolean hasRetry() { | ||
return operation.hasRetry(); | ||
} | ||
|
||
@Override | ||
public boolean hasTimeout() { | ||
return operation.hasTimeout(); | ||
} | ||
|
||
@Override | ||
public String name() { | ||
return name; | ||
} | ||
|
||
@Override | ||
public Object cacheKey() { | ||
return name; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...ore/src/main/java/io/smallrye/faulttolerance/core/metrics/DelegatingMetricsCollector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package io.smallrye.faulttolerance.core.metrics; | ||
|
||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.ConcurrentMap; | ||
|
||
import io.smallrye.faulttolerance.core.FaultToleranceStrategy; | ||
import io.smallrye.faulttolerance.core.InvocationContext; | ||
|
||
public class DelegatingMetricsCollector<V> implements FaultToleranceStrategy<V> { | ||
private final FaultToleranceStrategy<V> delegate; | ||
private final MetricsProvider provider; | ||
private final MeteredOperation originalOperation; | ||
|
||
private final ConcurrentMap<MeteredOperation, MetricsCollector<V>> cache = new ConcurrentHashMap<>(); | ||
|
||
public DelegatingMetricsCollector(FaultToleranceStrategy<V> delegate, MetricsProvider provider, | ||
MeteredOperation originalOperation) { | ||
this.delegate = delegate; | ||
this.provider = provider; | ||
this.originalOperation = originalOperation; | ||
} | ||
|
||
@Override | ||
public V apply(InvocationContext<V> ctx) throws Exception { | ||
MeteredOperationName name = ctx.get(MeteredOperationName.class); | ||
MeteredOperation operation = name != null | ||
? new DelegatingMeteredOperation(originalOperation, name.get()) | ||
: originalOperation; | ||
MetricsCollector<V> delegate = cache.computeIfAbsent(operation, | ||
ignored -> new MetricsCollector<>(this.delegate, provider.create(operation), operation)); | ||
return delegate.apply(ctx); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...tion/core/src/main/java/io/smallrye/faulttolerance/core/metrics/MeteredOperationName.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package io.smallrye.faulttolerance.core.metrics; | ||
|
||
public final class MeteredOperationName { | ||
private final String name; | ||
|
||
public MeteredOperationName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String get() { | ||
return name; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
...java/io/smallrye/faulttolerance/reuse/async/completionstage/metrics/MyFaultTolerance.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package io.smallrye.faulttolerance.reuse.async.completionstage.metrics; | ||
|
||
import static java.util.concurrent.CompletableFuture.completedFuture; | ||
|
||
import java.util.concurrent.CompletionStage; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.enterprise.inject.Produces; | ||
|
||
import io.smallrye.common.annotation.Identifier; | ||
import io.smallrye.faulttolerance.api.FaultTolerance; | ||
|
||
@ApplicationScoped | ||
public class MyFaultTolerance { | ||
@Produces | ||
@Identifier("my-fault-tolerance") | ||
public static final FaultTolerance<CompletionStage<String>> FT = FaultTolerance.<String> createAsync() | ||
.withRetry().maxRetries(2).done() | ||
.withFallback().handler(() -> completedFuture("fallback")).done() | ||
.build(); | ||
} |
Oops, something went wrong.