Skip to content

Commit

Permalink
ledger/metrics: Move metric helpers to their own Bazel package. (digi…
Browse files Browse the repository at this point in the history
…tal-asset#5542)

* ledger/metrics: Move metric helpers to their own Bazel package.

CHANGELOG_BEGIN
CHANGELOG_END

* sandbox: Use ledger/metrics.

* metrics: Rename `Metrics` to `Timed` and drop the `timed` prefix.

Importing methods is harder than importing objects.

* metrics: Publish to Maven Central.
  • Loading branch information
SamirTalwar authored Apr 14, 2020
1 parent 08a5a64 commit bdb476f
Show file tree
Hide file tree
Showing 34 changed files with 152 additions and 145 deletions.
25 changes: 25 additions & 0 deletions ledger/metrics/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

load(
"//bazel_tools:scala.bzl",
"da_scala_library",
)

da_scala_library(
name = "metrics",
srcs = glob(["src/main/scala/**/*.scala"]),
resources = glob(["src/main/resources/**/*"]),
tags = ["maven_coordinates=com.daml:metrics:__VERSION__"],
visibility = [
"//visibility:public",
],
runtime_deps = [],
deps = [
"//libs-scala/direct-execution-context",
"@maven//:com_typesafe_akka_akka_actor_2_12",
"@maven//:com_typesafe_akka_akka_stream_2_12",
"@maven//:io_dropwizard_metrics_metrics_core",
"@maven//:io_dropwizard_metrics_metrics_jvm",
],
)
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package com.daml.ledger.participant.state.metrics
package com.daml.metrics

import java.util

Expand All @@ -13,7 +13,7 @@ import com.codahale.metrics.jvm.{
ThreadStatesGaugeSet
}
import com.codahale.metrics.{Metric, MetricSet}
import com.daml.ledger.participant.state.metrics.JvmMetricSet._
import com.daml.metrics.JvmMetricSet._

import scala.collection.JavaConverters._

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package com.daml.ledger.participant.state.metrics
package com.daml.metrics

import scala.language.implicitConversions

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package com.daml.ledger.participant.state.metrics
package com.daml.metrics

import java.util.concurrent.CompletionStage

Expand All @@ -12,24 +12,24 @@ import com.daml.dec.DirectExecutionContext

import scala.concurrent.Future

object Metrics {
object Timed {

def timedCompletionStage[T](timer: Timer, future: => CompletionStage[T]): CompletionStage[T] = {
def completionStage[T](timer: Timer, future: => CompletionStage[T]): CompletionStage[T] = {
val ctx = timer.time()
future.whenComplete { (_, _) =>
ctx.stop()
()
}
}

def timedFuture[T](timer: Timer, future: => Future[T]): Future[T] = {
def future[T](timer: Timer, future: => Future[T]): Future[T] = {
val ctx = timer.time()
val result = future
result.onComplete(_ => ctx.stop())(DirectExecutionContext)
result
}

def timedSource[Out, Mat](timer: Timer, source: => Source[Out, Mat]): Source[Out, Mat] = {
def source[Out, Mat](timer: Timer, source: => Source[Out, Mat]): Source[Out, Mat] = {
val ctx = timer.time()
source
.watchTermination()(Keep.both[Mat, Future[Done]])
Expand Down
3 changes: 1 addition & 2 deletions ledger/participant-state-metrics/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@ da_scala_library(
"//daml-lf/data",
"//daml-lf/transaction",
"//ledger/ledger-api-health",
"//ledger/metrics",
"//ledger/participant-state",
"//libs-scala/direct-execution-context",
"@maven//:com_google_protobuf_protobuf_java",
"@maven//:com_typesafe_akka_akka_actor_2_12",
"@maven//:com_typesafe_akka_akka_stream_2_12",
"@maven//:io_dropwizard_metrics_metrics_core",
"@maven//:io_dropwizard_metrics_metrics_jvm",
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ package com.daml.ledger.participant.state.v1.metrics
import akka.NotUsed
import akka.stream.scaladsl.Source
import com.codahale.metrics.MetricRegistry
import com.daml.ledger.participant.state.metrics.{MetricName, Metrics}
import com.daml.ledger.participant.state.v1.{LedgerInitialConditions, Offset, ReadService, Update}
import com.daml.ledger.api.health.HealthStatus
import com.daml.ledger.participant.state.v1.{LedgerInitialConditions, Offset, ReadService, Update}
import com.daml.metrics.{MetricName, Timed}

final class TimedReadService(delegate: ReadService, metrics: MetricRegistry, prefix: MetricName)
extends ReadService {
Expand All @@ -22,5 +22,5 @@ final class TimedReadService(delegate: ReadService, metrics: MetricRegistry, pre
delegate.currentHealth()

private def time[Out, Mat](name: String, source: => Source[Out, Mat]): Source[Out, Mat] =
Metrics.timedSource(metrics.timer(prefix :+ name), source)
Timed.source(metrics.timer(prefix :+ name), source)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ package com.daml.ledger.participant.state.v1.metrics
import java.util.concurrent.CompletionStage

import com.codahale.metrics.MetricRegistry
import com.daml.ledger.participant.state.metrics.{MetricName, Metrics}
import com.daml.daml_lf_dev.DamlLf
import com.daml.ledger.api.health.HealthStatus
import com.daml.ledger.participant.state.v1.{
Configuration,
Party,
Expand All @@ -18,8 +19,7 @@ import com.daml.ledger.participant.state.v1.{
WriteService
}
import com.daml.lf.data.Time
import com.daml.daml_lf_dev.DamlLf
import com.daml.ledger.api.health.HealthStatus
import com.daml.metrics.{MetricName, Timed}

final class TimedWriteService(delegate: WriteService, metrics: MetricRegistry, prefix: MetricName)
extends WriteService {
Expand Down Expand Up @@ -57,5 +57,5 @@ final class TimedWriteService(delegate: WriteService, metrics: MetricRegistry, p
delegate.currentHealth()

private def time[T](name: String, future: => CompletionStage[T]): CompletionStage[T] =
Metrics.timedCompletionStage(metrics.timer(prefix :+ name), future)
Timed.completionStage(metrics.timer(prefix :+ name), future)
}
2 changes: 1 addition & 1 deletion ledger/participant-state/kvutils/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ da_scala_library(
"//daml-lf/transaction:value_java_proto",
"//ledger/ledger-api-common",
"//ledger/ledger-api-health",
"//ledger/metrics",
"//ledger/participant-state",
"//ledger/participant-state-metrics",
"//ledger/participant-state/protobuf:ledger_configuration_java_proto",
"//libs-scala/contextualized-logging",
"@maven//:com_github_ben_manes_caffeine_caffeine",
Expand Down
1 change: 1 addition & 0 deletions ledger/participant-state/kvutils/app/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ da_scala_library(
"//ledger/ledger-api-auth",
"//ledger/ledger-api-common",
"//ledger/ledger-api-health",
"//ledger/metrics",
"//ledger/participant-state",
"//ledger/participant-state-index",
"//ledger/participant-state-metrics",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

package com.daml.ledger.participant.state.kvutils.app

import com.daml.ledger.participant.state.metrics.MetricName
import com.daml.metrics.MetricName

private[app] object Metrics {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ import java.util.concurrent.TimeUnit

import akka.actor.ActorSystem
import akka.stream.Materializer
import com.daml.daml_lf_dev.DamlLf.Archive
import com.daml.ledger.participant.state.kvutils.app.Metrics.{
IndexServicePrefix,
ReadServicePrefix,
WriteServicePrefix
}
import com.daml.ledger.participant.state.metrics.JvmMetricSet
import com.daml.ledger.participant.state.v1.metrics.{TimedReadService, TimedWriteService}
import com.daml.ledger.participant.state.v1.{SubmissionId, WritePackagesService}
import com.daml.lf.archive.DarReader
import com.daml.daml_lf_dev.DamlLf.Archive
import com.daml.logging.LoggingContext.newLoggingContext
import com.daml.metrics.JvmMetricSet
import com.daml.platform.apiserver.{StandaloneApiServer, TimedIndexService}
import com.daml.platform.indexer.StandaloneIndexerServer
import com.daml.resources.akka.AkkaResourceOwner
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import com.daml.ledger.participant.state.kvutils.DamlKvutils.{
}
import com.daml.ledger.participant.state.kvutils.DamlStateMap
import com.daml.ledger.participant.state.kvutils.committer.Committer._
import com.daml.ledger.participant.state.metrics.MetricName
import com.daml.ledger.participant.state.v1.ParticipantId
import com.daml.lf.data.Time
import com.daml.metrics.MetricName
import org.slf4j.{Logger, LoggerFactory}

/** A committer processes a submission, with its inputs into an ordered set of output state and a log entry.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
package com.daml.ledger.participant.state

import com.daml.ledger.participant.state.kvutils.DamlKvutils.{DamlStateKey, DamlStateValue}
import com.daml.ledger.participant.state.metrics.MetricName
import com.daml.metrics.MetricName
import com.google.protobuf.ByteString

/** The participant-state key-value utilities provide methods to succinctly implement
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@ import com.daml.ledger.participant.state.kvutils.DamlKvutils._
import com.daml.ledger.participant.state.kvutils.api.LedgerReader
import com.daml.ledger.participant.state.kvutils.caching.Cache
import com.daml.ledger.participant.state.kvutils.{Bytes, Envelope, KeyValueCommitting}
import com.daml.ledger.participant.state.metrics.MetricName
import com.daml.ledger.participant.state.metrics.Metrics.timedFuture
import com.daml.ledger.participant.state.v1.ParticipantId
import com.daml.ledger.validator.SubmissionValidator._
import com.daml.ledger.validator.ValidationFailed.{MissingInputState, ValidationError}
import com.daml.lf.data.Time.Timestamp
import com.daml.lf.engine.Engine
import com.daml.logging.LoggingContext.newLoggingContext
import com.daml.logging.{ContextualizedLogger, LoggingContext}
import com.daml.metrics.{MetricName, Timed}
import com.google.protobuf.ByteString

import scala.annotation.tailrec
Expand Down Expand Up @@ -185,7 +184,7 @@ class SubmissionValidator[LogResult] private[validator] (
timedLedgerStateAccess
.inTransaction { stateOperations =>
for {
readInputs <- timedFuture(
readInputs <- Timed.future(
Metrics.validateSubmission,
for {
readStateValues <- stateOperations.readState(inputKeysAsBytes)
Expand All @@ -199,7 +198,7 @@ class SubmissionValidator[LogResult] private[validator] (
_ <- verifyAllInputsArePresent(declaredInputs, readInputs)
} yield readInputs
)
logEntryAndState <- timedFuture(
logEntryAndState <- Timed.future(
Metrics.processSubmission,
Future.fromTry(
Try(
Expand All @@ -217,7 +216,7 @@ class SubmissionValidator[LogResult] private[validator] (
stateOperations,
)
result <- postProcessResultTimer.fold(processResult())(
timedFuture(_, processResult()))
Timed.future(_, processResult()))
} yield result
}
.transform {
Expand Down
2 changes: 1 addition & 1 deletion ledger/participant-state/kvutils/tools/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ da_scala_binary(
"//daml-lf/archive:daml_lf_dev_archive_java_proto",
"//daml-lf/data",
"//daml-lf/engine",
"//ledger/metrics",
"//ledger/participant-state",
"//ledger/participant-state-metrics",
"//ledger/participant-state/kvutils",
"//ledger/participant-state/kvutils:daml_kvutils_java_proto",
"@maven//:com_google_protobuf_protobuf_java",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import java.util.concurrent.TimeUnit
import com.codahale.metrics
import com.codahale.metrics.MetricRegistry
import com.daml.ledger.participant.state.kvutils.{DamlKvutils => Proto, _}
import com.daml.ledger.participant.state.metrics.JvmMetricSet
import com.daml.ledger.participant.state.v1._
import com.daml.lf.data.Ref
import com.daml.lf.engine.Engine
import com.daml.metrics.JvmMetricSet

import scala.collection.JavaConverters._
import scala.util.Try
Expand Down
2 changes: 2 additions & 0 deletions ledger/sandbox/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ compile_deps = [
"//ledger/ledger-api-domain",
"//ledger/ledger-api-health",
"//ledger/ledger-on-sql",
"//ledger/metrics",
"//ledger/participant-state",
"//ledger/participant-state-index",
"//ledger/participant-state-metrics",
Expand Down Expand Up @@ -284,6 +285,7 @@ da_scala_test_suite(
"//ledger/ledger-api-common:ledger-api-common-scala-tests-lib",
"//ledger/ledger-api-domain",
"//ledger/ledger-api-health",
"//ledger/metrics",
"//ledger/participant-state",
"//ledger/participant-state-index",
"//ledger/participant-state-metrics",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package com.daml.platform.apiserver
import java.util.concurrent.atomic.AtomicBoolean

import com.codahale.metrics.{MetricRegistry, Timer}
import com.daml.ledger.participant.state.metrics.MetricName
import com.daml.metrics.MetricName
import io.grpc.{Metadata, ServerCall, ServerCallHandler, ServerInterceptor}

import scala.collection.concurrent.TrieMap
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

package com.daml.platform.apiserver

import com.daml.ledger.participant.state.metrics.MetricName
import com.daml.metrics.MetricName

private[apiserver] object MetricsNaming {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,6 @@ import java.time.Instant
import akka.NotUsed
import akka.stream.scaladsl.Source
import com.codahale.metrics.MetricRegistry
import com.daml.ledger.participant.state.index.v2
import com.daml.ledger.participant.state.index.v2.IndexService
import com.daml.ledger.participant.state.metrics.{MetricName, Metrics}
import com.daml.ledger.participant.state.v1.{Configuration, PackageId, ParticipantId, Party}
import com.daml.lf.data.Ref
import com.daml.lf.language.Ast
import com.daml.lf.transaction.Node
import com.daml.lf.value.Value
import com.daml.daml_lf_dev.DamlLf
import com.daml.ledger.api.domain
import com.daml.ledger.api.domain.{ApplicationId, CommandId, LedgerId, LedgerOffset, TransactionId}
Expand All @@ -28,6 +20,14 @@ import com.daml.ledger.api.v1.transaction_service.{
GetTransactionTreesResponse,
GetTransactionsResponse
}
import com.daml.ledger.participant.state.index.v2
import com.daml.ledger.participant.state.index.v2.IndexService
import com.daml.ledger.participant.state.v1.{Configuration, PackageId, ParticipantId, Party}
import com.daml.lf.data.Ref
import com.daml.lf.language.Ast
import com.daml.lf.transaction.Node
import com.daml.lf.value.Value
import com.daml.metrics.{MetricName, Timed}

import scala.concurrent.Future

Expand Down Expand Up @@ -156,8 +156,8 @@ final class TimedIndexService(delegate: IndexService, metrics: MetricRegistry, p
delegate.currentHealth()

private def time[T](name: String, future: => Future[T]): Future[T] =
Metrics.timedFuture(metrics.timer(prefix :+ name), future)
Timed.future(metrics.timer(prefix :+ name), future)

private def time[Out, Mat](name: String, source: => Source[Out, Mat]): Source[Out, Mat] =
Metrics.timedSource(metrics.timer(prefix :+ name), source)
Timed.source(metrics.timer(prefix :+ name), source)
}
Loading

0 comments on commit bdb476f

Please sign in to comment.