-
Notifications
You must be signed in to change notification settings - Fork 75
[CGKIELE-168] Add support for metrics #450
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,18 @@ | ||
package io.iohk.ethereum.metrics | ||
|
||
object Metrics { | ||
/** | ||
* Signifies that Mantis has started. | ||
*/ | ||
final val StartEvent = "start.event" | ||
|
||
/** | ||
* Signifies that Mantis has stopped. | ||
*/ | ||
final val StopEvent = "stop.event" | ||
|
||
/** | ||
* Measures the block number of the last imported block. | ||
*/ | ||
final val LedgerImportBlockNumber = "ledger.import.block.number" | ||
} |
83 changes: 83 additions & 0 deletions
83
src/main/scala/io/iohk/ethereum/metrics/MetricsClient.scala
This file contains hidden or 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,83 @@ | ||
package io.iohk.ethereum.metrics | ||
|
||
import java.util.concurrent.atomic.AtomicReference | ||
|
||
import com.timgroup.statsd.{NoOpStatsDClient, NonBlockingStatsDClient, StatsDClient} | ||
import com.typesafe.config.Config | ||
import io.iohk.ethereum.utils.Logger | ||
|
||
object MetricsClient extends Logger { | ||
private[this] final val NoOpClient = new NoOpStatsDClient | ||
private[this] final val clientRef = new AtomicReference[StatsDClient](NoOpClient) | ||
|
||
private[this] def setOnce(client: StatsDClient): Boolean = clientRef.compareAndSet(NoOpClient, client) | ||
|
||
/** | ||
* Retrieves the application-wide metrics client. | ||
*/ | ||
def get(): StatsDClient = clientRef.get() | ||
|
||
|
||
/** | ||
* A prefix for all metrics. | ||
*/ | ||
final val Prefix = "mantis" // TODO there are several other strings of this value. Can we consolidate? | ||
|
||
/** | ||
* Default tags we send to StatsD (actually Datadog). | ||
* | ||
* See https://github.com/input-output-hk/iohk-ops/blob/618748e09035f7bc3e3b055818c0cde4cf1958ce/modules/production.nix#L15 | ||
*/ | ||
object Tag { | ||
final val Env = "env" | ||
final val Depl = "depl" | ||
} | ||
|
||
def mkTag(name: String, value: String): String = s"$name:$value" | ||
|
||
/** | ||
* Instantiates and configures the metrics client. This should happen once in the lifetime of the application. | ||
* After this call completes successfully, you can obtain the metrics client by using `MetricsClient.get()`. | ||
*/ | ||
def configure(config: MetricsConfig): Unit = { | ||
val enabled = config.enabled | ||
|
||
if(enabled) { | ||
val hostname = config.host | ||
val port = config.port | ||
val queueSize = config.queueSize | ||
val logErrors = config.logErrors | ||
val constantTags = Array( | ||
mkTag(Tag.Env, config.environment), | ||
mkTag(Tag.Depl, config.deployment) | ||
) | ||
val errorHandler = if(logErrors) new MetricsErrorHandler else null // null indicates NOOP | ||
|
||
val client = | ||
new NonBlockingStatsDClient( | ||
Prefix, | ||
hostname, | ||
port, | ||
queueSize, | ||
constantTags, | ||
errorHandler | ||
) | ||
|
||
if(setOnce(client)) { | ||
log.info(s"Configured metrics client: $client") | ||
} else { | ||
log.warn(s"Could not configure metrics client: $client") | ||
client.close() | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Instantiates and configures the metrics client. This should happen once in the lifetime of the application. | ||
* After this call completes successfully, you can obtain the metrics client by using `MetricsClient.get()`. | ||
*/ | ||
def configure(mantisConfig: Config): Unit = { | ||
val config = MetricsConfig(mantisConfig) | ||
configure(config) | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
src/main/scala/io/iohk/ethereum/metrics/MetricsConfig.scala
This file contains hidden or 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,51 @@ | ||
package io.iohk.ethereum.metrics | ||
|
||
import com.typesafe.config.{Config ⇒ TypesafeConfig} | ||
|
||
final case class MetricsConfig( | ||
enabled: Boolean, | ||
host: String, | ||
port: Int, | ||
queueSize: Int, | ||
logErrors: Boolean, | ||
environment: String, // `public`, `private` | ||
deployment: String // `testnet-kevm`, `testnet-iele` | ||
) | ||
|
||
object MetricsConfig { | ||
object Keys { | ||
final val Metrics = "metrics" | ||
|
||
final val Enabled = "enabled" | ||
final val Host = "host" | ||
final val Port = "port" | ||
final val QueueSize = "queue-size" | ||
final val LogErrors = "log-errors" | ||
|
||
final val Environment = "environment" | ||
final val Deployment = "deployment" | ||
} | ||
|
||
def apply(mantisConfig: TypesafeConfig): MetricsConfig = { | ||
val config = mantisConfig.getConfig(Keys.Metrics) | ||
|
||
val enabled = config.getBoolean(Keys.Enabled) | ||
val host = config.getString(Keys.Host) | ||
val port = config.getInt(Keys.Port) | ||
val queueSize = config.getInt(Keys.QueueSize) | ||
val logErrors = config.getBoolean(Keys.LogErrors) | ||
|
||
val environment = config.getString(Keys.Environment) | ||
val deployment = config.getString(Keys.Deployment) | ||
|
||
MetricsConfig( | ||
enabled = enabled, | ||
host = host, | ||
port = port, | ||
queueSize = queueSize, | ||
logErrors = logErrors, | ||
environment = environment, | ||
deployment = environment | ||
) | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/scala/io/iohk/ethereum/metrics/MetricsErrorHandler.scala
This file contains hidden or 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,9 @@ | ||
package io.iohk.ethereum.metrics | ||
|
||
import com.timgroup.statsd.StatsDClientErrorHandler | ||
import io.iohk.ethereum.utils.Logger | ||
|
||
final class MetricsErrorHandler extends StatsDClientErrorHandler with Logger { | ||
def handle(exception: Exception): Unit = | ||
log.error("[" + classOf[StatsDClientErrorHandler].getSimpleName + "]", exception) | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 @@ | ||
mantis { | ||
metrics { | ||
# Set to `true` iff your deployment supports metrics collection. | ||
# We push metrics to a StatsD-compatible agent and we use Datadog for collecting them in one place. | ||
# We default to `false` here because we do not expect all deployments to support metrics collection. | ||
# enabled = false | ||
|
||
# The StatsD-compatible agent host. | ||
# host = "localhost" | ||
|
||
# The StatsD-compatible agent port. | ||
# port = 8125 | ||
|
||
# All metrics sent will be tagged with the environment. | ||
# Sample values are: `public`, `private`, `production`, `dev`, depending on the use-case. | ||
# If metrics are `enabled`, this is mandatory and must be set explicitly to a non-null value. | ||
# | ||
# environment = null | ||
|
||
# All metrics sent will be tagged with the deployment. | ||
# Sample values are: `kevm-testnet`, `iele-testnet`, `raft-kevm`, depending on the use-case. | ||
# If metrics are `enabled`, this is mandatory and must be set explicitly to a non-null value. | ||
# | ||
# deployment = null | ||
|
||
# Size of the metrics requests queue. | ||
# If the queue contains that many outstanding requests to the metrics agent, then | ||
# subsequent requests are blocked until the queue has room again. | ||
# queue-size = 1024 | ||
|
||
# Iff true, any errors during metrics client operations will be logged. | ||
# log-errors = true | ||
} | ||
} |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Iff
- do you mean "if and only if" or is it a typo?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
:-D