Skip to content
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

Metric listeners #7474

Merged
merged 23 commits into from
Nov 18, 2022
Merged

Metric listeners #7474

merged 23 commits into from
Nov 18, 2022

Conversation

petoalbert
Copy link
Contributor

@petoalbert petoalbert commented Oct 28, 2022

Implementation for: #7393

Note that this needs obviously needs more work (at least tests and benchmarks), at the current stage I'd just like to get some feedback before proceeding.

I just learned that zio.metrics had listeners which were using an unsafe API without zio effects. I used effects so that from the zio-metrics-connector side it would be easier to use ZIO functionality, like ZStream. (see draft PR for zio-metrics-connectors: zio/zio-metrics-connectors#11) I am happy to recreate the original MetricListener with the unsafeUpdate method if that is much better in terms of performance.

@@ -43,4 +44,13 @@ private[zio] object MetricClient {
*/
final def snapshot()(implicit unsafe: Unsafe): Set[MetricPair.Untyped] =
metricRegistry.snapshot()

trait Listener {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For performance reasons, I would create Listener.Unsafe, which is an unsafe version of this trait. Then Listener.Unsafe can extend Listener. This will allow us to create an even lower-level interface to listening to changes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that makes sense. I changed the Listener methods to unsafe (returning Unit) for performance reasons, but I dropped the high-level interface as I don't think the use case for Datadog distribution would cover that.

def updateHistogram(key: MetricKey[MetricKeyType.Histogram], value: Double): UIO[Unit]
def updateGauge(key: MetricKey[MetricKeyType.Gauge], value: Double): UIO[Unit]
def updateFrequency(key: MetricKey[MetricKeyType.Frequency], value: String): UIO[Unit]
def updateSummary(key: MetricKey[MetricKeyType.Summary], value: (Double, java.time.Instant)): UIO[Unit]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be better to avoid the tuple (Double, java.time.Instant) and instead pass two parameters.

@@ -310,7 +325,7 @@ trait Metric[+Type, -In, +Out] extends ZIOAspect[Nothing, Any, Nothing, Any, Not
* provided amount.
*/
final def update(in: => In)(implicit trace: Trace): UIO[Unit] =
ZIO.succeed(unsafe.update(in, Set.empty)(Unsafe.unsafe))
ZIO.succeed(unsafe.update(in, Set.empty)(Unsafe.unsafe)) *> notifyListeners(in)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is much more expensive than the old version. I think what you should do is inside of ZIO.succeed, impurely perform the notification of listeners, so we can avoid the overhead.

Of course, this assumes all listeners are unsafe.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. What I ended up doing is that Unsafe.update handles the impure notification of listeners.

@@ -43,4 +44,13 @@ private[zio] object MetricClient {
*/
final def snapshot()(implicit unsafe: Unsafe): Set[MetricPair.Untyped] =
metricRegistry.snapshot()

trait Listener {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not recommended to put traits inside package objects.

@@ -24,6 +24,10 @@ sealed trait MetricKeyType {
type Out
}
object MetricKeyType {
type Aux[T] = MetricKeyType {
Copy link
Member

@jdegoes jdegoes Oct 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
type Aux[T] = MetricKeyType {
type WithIn[T] = MetricKeyType {

@jdegoes
Copy link
Member

jdegoes commented Nov 7, 2022

@petoalbert Let me know when this is ready for another review, or ping me if you want to pair on it for a bit!

package zio.metrics

private[zio] trait MetricListener {
def updateHistogram(key: MetricKey[MetricKeyType.Histogram], value: Double): Unit
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should all take implicit unsafe: Unsafe.

listeners.computeIfPresent(metricKeyType, { case (a, b) => b - listener })
}

def update[T](key: MetricKey[MetricKeyType.WithIn[T]], value: T)(implicit trace: Trace): Unit = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not actually updating, but rather, notifying listeners, so I would rename this to notifyListenerUpdate or something.


def update[T](key: MetricKey[MetricKeyType.WithIn[T]], value: T)(implicit trace: Trace): Unit = {
val listenersForKey = listeners.get(key.keyType)
val iterator = listenersForKey.iterator
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you are always allocating an Iterator, together with a Function0. At the very least, you should fast-path this by checking to see if listeners.nonEmpty before executing all of this (heavyweight) logic.

iterator.next().updateCounter(key.asInstanceOf[MetricKey.Counter], value)
}
while (iterator.hasNext) {
updateNext()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to inline updateNext() so that you avoid allocating the Function0.

): Unit =
hook(extraTags).update(in)
): Unit = {
val fullKey = key.tagged(extraTags).asInstanceOf[MetricKey[key.keyType.type]]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Computing the fullKey was not necessary before, and will involve additional allocation on every update of every metric.

Can you explain why this is necessary? At the very least, it should be fast-pathed, so that if extraTags.isEmpty, then no fullKey is computed.

@petoalbert petoalbert marked this pull request as ready for review November 17, 2022 13:50
@petoalbert
Copy link
Contributor Author

This PR is ready for review now.

jdegoes
jdegoes previously approved these changes Nov 17, 2022
@jdegoes jdegoes merged commit ffa1176 into zio:series/2.x Nov 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants