forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
37 changed files
with
192 additions
and
310 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
54 changes: 54 additions & 0 deletions
54
server/src/main/java/org/elasticsearch/common/metrics/MetricAccessor.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 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.common.metrics; | ||
|
||
import java.util.Objects; | ||
|
||
/** | ||
* Provides easy access to metrics (or anything really) | ||
* without the need to pass metrics object explicitly. | ||
* Intended to use as a singleton in consumers but still allows injecting test values | ||
* by first checking thread local value before falling back to set value. | ||
*/ | ||
public class MetricAccessor<T> { | ||
// Intentionally not static - we expect users to only create instance | ||
// per each "global" resource. | ||
private ThreadLocal<T> threadLocal = ThreadLocal.withInitial(() -> null); | ||
private T value; | ||
|
||
public MetricAccessor(T value) { | ||
this.value = Objects.requireNonNull(value); | ||
} | ||
|
||
public AutoCloseable initForTest(T value) { | ||
threadLocal.set(value); | ||
|
||
return new Reset(threadLocal); | ||
} | ||
|
||
/** | ||
* returns stored value or thread local value if set | ||
* @return T | ||
*/ | ||
public T get() { | ||
var local = threadLocal.get(); | ||
if (local != null) { | ||
return local; | ||
} | ||
|
||
return value; | ||
} | ||
|
||
private record Reset(ThreadLocal<?> threadLocal) implements AutoCloseable { | ||
@Override | ||
public void close() throws Exception { | ||
threadLocal.remove(); | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.