All Downloads are FREE. Search and download functionalities are using the official Maven repository.

goodmetrics.Metrics.kt Maven / Gradle / Ivy

There is a newer version: 0.7.2
Show newest version
package goodmetrics

/**
 * Not thread safe.
 */
data class Metrics internal constructor(
    internal val name: String,
    internal var timestampNanos: Long,
    internal val startNanoTime: Long,
) {
    sealed interface Dimension {
        val name: kotlin.String
        data class String(override val name: kotlin.String, val value: kotlin.String) : Dimension
        data class Number(override val name: kotlin.String, val value: Long) : Dimension
        data class Boolean(override val name: kotlin.String, val value: kotlin.Boolean) : Dimension
    }
    internal val metricMeasurements: MutableMap = mutableMapOf()
    internal val metricDistributions: MutableMap = mutableMapOf()
    internal val metricDimensions: MutableMap = mutableMapOf()

    data class View(
        val metricName: String,
        val timestampNanos: Long,
        val startNanoTime: Long,
        val dimensions: Map,
        val measurements: Map,
        val distributions: Map,
    )

    fun getView(): View {
        return View(
            name,
            timestampNanos,
            startNanoTime,
            metricDimensions.toMap(),
            metricMeasurements.toMap(),
            metricDistributions.toMap(),
        )
    }

    fun dimension(dimension: String, value: Boolean) {
        metricDimensions[dimension] = Dimension.Boolean(dimension, value)
    }

    fun dimension(dimension: String, value: Long) {
        metricDimensions[dimension] = Dimension.Number(dimension, value)
    }

    fun dimension(dimension: String, value: String) {
        metricDimensions[dimension] = Dimension.String(dimension, value)
    }

    fun measure(name: String, value: Long) {
        metricMeasurements[name] = value
    }

    fun measure(name: String, value: Int) {
        metricMeasurements[name] = value.toLong()
    }

    fun measure(name: String, value: Double) {
        metricMeasurements[name] = value
    }

    fun measure(name: String, value: Float) {
        metricMeasurements[name] = value.toDouble()
    }

    /**
     * Distributions are positive only.
     * This only records 1 position of a distribution per Metrics lifetime.
     */
    fun distribution(name: String, value: Long) {
        if (value < 0) {
            return
        }
        metricDistributions[name] = value
    }

    /**
     * Distributions are positive only.
     * This only records 1 position of a distribution per Metrics lifetime.
     */
    fun distribution(name: String, value: Int) {
        if (value < 0) {
            return
        }
        metricDistributions[name] = value.toLong()
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy