10
10
11
11
package com .snowplowanalytics .snowplow .lakes
12
12
13
+ import cats .Functor
13
14
import cats .effect .Async
14
15
import cats .effect .kernel .Ref
15
16
import cats .implicits ._
16
17
import fs2 .Stream
17
18
18
19
import scala .concurrent .duration .{Duration , FiniteDuration }
19
20
21
+ import com .snowplowanalytics .snowplow .sources .SourceAndAck
20
22
import com .snowplowanalytics .snowplow .runtime .{Metrics => CommonMetrics }
21
23
22
24
trait Metrics [F [_]] {
@@ -32,8 +34,8 @@ trait Metrics[F[_]] {
32
34
33
35
object Metrics {
34
36
35
- def build [F [_]: Async ](config : Config .Metrics ): F [Metrics [F ]] =
36
- Ref [ F ].of (State .empty) .map(impl(config, _))
37
+ def build [F [_]: Async ](config : Config .Metrics , sourceAndAck : SourceAndAck [ F ] ): F [Metrics [F ]] =
38
+ Ref .ofEffect (State .initialize(sourceAndAck)) .map(impl(config, _, sourceAndAck ))
37
39
38
40
private case class State (
39
41
received : Int ,
@@ -53,11 +55,18 @@ object Metrics {
53
55
}
54
56
55
57
private object State {
56
- def empty : State = State (0 , 0 , 0 , Duration .Zero , None , None )
58
+ def initialize [F [_]: Functor ](sourceAndAck : SourceAndAck [F ]): F [State ] =
59
+ sourceAndAck.currentStreamLatency.map { latency =>
60
+ State (0 , 0 , 0 , latency.getOrElse(Duration .Zero ), None , None )
61
+ }
57
62
}
58
63
59
- private def impl [F [_]: Async ](config : Config .Metrics , ref : Ref [F , State ]): Metrics [F ] =
60
- new CommonMetrics [F , State ](ref, State .empty, config.statsd) with Metrics [F ] {
64
+ private def impl [F [_]: Async ](
65
+ config : Config .Metrics ,
66
+ ref : Ref [F , State ],
67
+ sourceAndAck : SourceAndAck [F ]
68
+ ): Metrics [F ] =
69
+ new CommonMetrics [F , State ](ref, State .initialize(sourceAndAck), config.statsd) with Metrics [F ] {
61
70
def addReceived (count : Int ): F [Unit ] =
62
71
ref.update(s => s.copy(received = s.received + count))
63
72
def addBad (count : Int ): F [Unit ] =
0 commit comments