Skip to content

Instantly share code, notes, and snippets.

@loganlinn
Last active August 29, 2018 21:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save loganlinn/4f1e6e2a2de999c4d3c6321c269003b4 to your computer and use it in GitHub Desktop.
Save loganlinn/4f1e6e2a2de999c4d3c6321c269003b4 to your computer and use it in GitHub Desktop.
Kotlin version of com.codahale.metrics.SharedMetricRegistries
import com.codahale.metrics.MetricRegistry
import java.util.concurrent.ConcurrentHashMap
/**
* A map of shared, named metric registries.
*/
object SharedMetricRegistries {
private val REGISTRIES = ConcurrentHashMap<String, MetricRegistry>()
@Volatile
private var defaultRegistryName: String? = null
val default: MetricRegistry
get() = defaultRegistryName?.let { get(it) }
?: throw IllegalStateException("Default registry name has not been set.")
fun clear() {
REGISTRIES.clear()
}
fun names(): Set<String> = REGISTRIES.keys.toSet()
fun remove(key: String) {
REGISTRIES.remove(key)
}
fun set(name: String, registry: MetricRegistry): MetricRegistry? {
return REGISTRIES.putIfAbsent(name, registry)
}
fun get(name: String): MetricRegistry {
val existing = REGISTRIES[name]
if (existing == null) {
val created = MetricRegistry()
return set(name, created) ?: return created
}
return existing
}
@Synchronized
fun setDefault(name: String): MetricRegistry {
val registry = get(name)
return setDefault(name, registry)
}
fun setDefault(name: String, metricRegistry: MetricRegistry): MetricRegistry {
if (defaultRegistryName == null) {
defaultRegistryName = name
set(name, metricRegistry)
return metricRegistry
}
throw IllegalStateException("Default metric registry name is already set.")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment