-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
96a44ce
commit 2adc897
Showing
8 changed files
with
1,122 additions
and
11 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
89 changes: 89 additions & 0 deletions
89
...src/main/java/rocks/inspectit/oce/eum/server/metrics/percentiles/AsyncMetricRecorder.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,89 @@ | ||
package rocks.inspectit.oce.eum.server.metrics.percentiles; | ||
|
||
import com.google.common.annotations.VisibleForTesting; | ||
import io.opencensus.common.Timestamp; | ||
import io.opencensus.tags.TagContext; | ||
import lombok.Value; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import java.util.concurrent.ArrayBlockingQueue; | ||
|
||
/** | ||
* COPIED FROM THE OCELOT CORE PROJECT! | ||
* <p> | ||
* Consumer thread for asynchronously processing measurement observations. | ||
*/ | ||
@Slf4j | ||
class AsyncMetricRecorder { | ||
|
||
private static final int QUEUE_CAPACITY = 8096; | ||
|
||
private final MetricConsumer metricConsumer; | ||
|
||
private volatile boolean overflowLogged = false; | ||
|
||
private volatile boolean isDestroyed = false; | ||
|
||
@VisibleForTesting | ||
final ArrayBlockingQueue<MetricRecord> recordsQueue = new ArrayBlockingQueue<>(QUEUE_CAPACITY); | ||
|
||
@VisibleForTesting | ||
final Thread worker; | ||
|
||
AsyncMetricRecorder(MetricConsumer consumer) { | ||
metricConsumer = consumer; | ||
worker = new Thread(this::doRecord); | ||
worker.setDaemon(true); | ||
worker.setName("InspectIT Ocelot percentile Recorder"); | ||
worker.start(); | ||
} | ||
|
||
void record(String measureName, double value, Timestamp time, TagContext tags) { | ||
boolean success = recordsQueue.offer(new MetricRecord(value, measureName, time, tags)); | ||
if (!success && !overflowLogged) { | ||
overflowLogged = true; | ||
log.warn("Measurement for percentiles has been dropped because queue is full. This message will not be shown for further drops!"); | ||
} | ||
} | ||
|
||
void destroy() { | ||
isDestroyed = true; | ||
worker.interrupt(); | ||
} | ||
|
||
private void doRecord() { | ||
while (true) { | ||
try { | ||
MetricRecord record = recordsQueue.take(); | ||
metricConsumer.record(record.measure, record.value, record.time, record.tagContext); | ||
} catch (InterruptedException e) { | ||
if (isDestroyed) { | ||
return; | ||
} else { | ||
log.error("Unexpected interrupt", e); | ||
} | ||
} catch (Exception e) { | ||
log.error("Error processing record: ", e); | ||
} | ||
} | ||
} | ||
|
||
public interface MetricConsumer { | ||
|
||
void record(String measure, double value, Timestamp time, TagContext tags); | ||
} | ||
|
||
@Value | ||
private static class MetricRecord { | ||
|
||
double value; | ||
|
||
String measure; | ||
|
||
Timestamp time; | ||
|
||
TagContext tagContext; | ||
|
||
} | ||
|
||
} |
54 changes: 54 additions & 0 deletions
54
...c/main/java/rocks/inspectit/oce/eum/server/metrics/percentiles/CachingMetricProducer.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,54 @@ | ||
package rocks.inspectit.oce.eum.server.metrics.percentiles; | ||
|
||
import io.opencensus.metrics.export.Metric; | ||
import io.opencensus.metrics.export.MetricProducer; | ||
|
||
import java.time.Duration; | ||
import java.util.Collection; | ||
import java.util.function.Supplier; | ||
|
||
/** | ||
* COPIED FROM THE OCELOT CORE PROJECT! | ||
* <p> | ||
* A metric producer which caches the metrics for a specified amount of time. | ||
*/ | ||
public class CachingMetricProducer extends MetricProducer { | ||
|
||
/** | ||
* The function invoked to generate the metrics. | ||
*/ | ||
private final Supplier<Collection<Metric>> computeMetricsFunction; | ||
|
||
/** | ||
* The duration for which cached metrics are kept. | ||
*/ | ||
private final long cacheDurationNanos; | ||
|
||
/** | ||
* The timestamp when the metrics were computed the last time. | ||
*/ | ||
private long cacheTimestamp; | ||
|
||
private Collection<Metric> cachedMetrics = null; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param computeMetricsFunction the function to invoke for computing the metrics | ||
* @param cacheDuration the duration for which the values shall be cached. | ||
*/ | ||
public CachingMetricProducer(Supplier<Collection<Metric>> computeMetricsFunction, Duration cacheDuration) { | ||
this.computeMetricsFunction = computeMetricsFunction; | ||
cacheDurationNanos = cacheDuration.toNanos(); | ||
} | ||
|
||
@Override | ||
public synchronized Collection<Metric> getMetrics() { | ||
long now = System.nanoTime(); | ||
if (cachedMetrics == null || (now - cacheTimestamp) > cacheDurationNanos) { | ||
cachedMetrics = computeMetricsFunction.get(); | ||
cacheTimestamp = now; | ||
} | ||
return cachedMetrics; | ||
} | ||
} |
Oops, something went wrong.