2023-05-18 15:55:53 +03:00
|
|
|
{-# LANGUAGE DeriveAnyClass #-}
|
|
|
|
|
2022-07-24 00:18:01 +03:00
|
|
|
-- | Mutable references for Prometheus metrics.
|
|
|
|
--
|
|
|
|
-- These metrics are independent from the metrics in "Hasura.Server.Metrics".
|
|
|
|
module Hasura.Server.Prometheus
|
|
|
|
( PrometheusMetrics (..),
|
|
|
|
GraphQLRequestMetrics (..),
|
2022-08-15 08:32:55 +03:00
|
|
|
EventTriggerMetrics (..),
|
2023-05-16 10:37:02 +03:00
|
|
|
CacheRequestMetrics (..),
|
2022-07-24 00:18:01 +03:00
|
|
|
makeDummyPrometheusMetrics,
|
|
|
|
ConnectionsGauge,
|
|
|
|
Connections (..),
|
|
|
|
newConnectionsGauge,
|
|
|
|
readConnectionsGauge,
|
|
|
|
incWarpThreads,
|
|
|
|
decWarpThreads,
|
|
|
|
incWebsocketConnections,
|
|
|
|
decWebsocketConnections,
|
2023-03-30 08:51:18 +03:00
|
|
|
ScheduledTriggerMetrics (..),
|
2023-04-18 18:31:33 +03:00
|
|
|
SubscriptionMetrics (..),
|
2023-05-24 16:22:06 +03:00
|
|
|
DynamicEventTriggerLabel (..),
|
|
|
|
ResponseStatus (..),
|
|
|
|
responseStatusToLabelValue,
|
|
|
|
EventStatusLabel (..),
|
|
|
|
eventSuccessLabel,
|
|
|
|
eventFailedLabel,
|
|
|
|
EventStatusWithTriggerLabel (..),
|
2023-05-11 08:19:42 +03:00
|
|
|
GranularPrometheusMetricsState (..),
|
|
|
|
observeHistogramWithLabel,
|
2023-05-18 15:55:53 +03:00
|
|
|
SubscriptionKindLabel (..),
|
|
|
|
SubscriptionLabel (..),
|
|
|
|
DynamicSubscriptionLabel (..),
|
|
|
|
streamingSubscriptionLabel,
|
|
|
|
liveQuerySubscriptionLabel,
|
|
|
|
recordMetricWithLabel,
|
|
|
|
recordSubcriptionMetric,
|
2022-07-24 00:18:01 +03:00
|
|
|
)
|
|
|
|
where
|
|
|
|
|
2023-05-11 08:19:42 +03:00
|
|
|
import Data.HashMap.Internal.Strict qualified as Map
|
2023-05-18 15:55:53 +03:00
|
|
|
import Data.HashMap.Strict qualified as HashMap
|
2022-07-24 00:18:01 +03:00
|
|
|
import Data.IORef (IORef, atomicModifyIORef', newIORef, readIORef)
|
|
|
|
import Data.Int (Int64)
|
2023-05-18 15:55:53 +03:00
|
|
|
import Hasura.GraphQL.ParameterizedQueryHash
|
|
|
|
import Hasura.GraphQL.Transport.HTTP.Protocol (OperationName (..))
|
2022-07-24 00:18:01 +03:00
|
|
|
import Hasura.Prelude
|
2023-05-24 16:22:06 +03:00
|
|
|
import Hasura.RQL.Types.Common (SourceName, sourceNameToText)
|
2023-05-11 08:19:42 +03:00
|
|
|
import Hasura.RQL.Types.EventTrigger (TriggerName, triggerNameToTxt)
|
|
|
|
import Hasura.Server.Types (GranularPrometheusMetricsState (..))
|
2023-05-18 15:55:53 +03:00
|
|
|
import Language.GraphQL.Draft.Syntax qualified as G
|
2023-05-11 08:19:42 +03:00
|
|
|
import System.Metrics.Prometheus (ToLabels (..))
|
2022-07-24 00:18:01 +03:00
|
|
|
import System.Metrics.Prometheus.Counter (Counter)
|
|
|
|
import System.Metrics.Prometheus.Counter qualified as Counter
|
2023-05-24 16:22:06 +03:00
|
|
|
import System.Metrics.Prometheus.CounterVector (CounterVector)
|
|
|
|
import System.Metrics.Prometheus.CounterVector qualified as CounterVector
|
2022-07-24 00:18:01 +03:00
|
|
|
import System.Metrics.Prometheus.Gauge (Gauge)
|
|
|
|
import System.Metrics.Prometheus.Gauge qualified as Gauge
|
2023-05-18 15:55:53 +03:00
|
|
|
import System.Metrics.Prometheus.GaugeVector qualified as GaugeVector
|
2022-07-24 00:18:01 +03:00
|
|
|
import System.Metrics.Prometheus.Histogram (Histogram)
|
|
|
|
import System.Metrics.Prometheus.Histogram qualified as Histogram
|
2023-05-11 08:19:42 +03:00
|
|
|
import System.Metrics.Prometheus.HistogramVector (HistogramVector)
|
|
|
|
import System.Metrics.Prometheus.HistogramVector qualified as HistogramVector
|
2022-07-24 00:18:01 +03:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
-- | Mutable references for Prometheus metrics.
|
|
|
|
data PrometheusMetrics = PrometheusMetrics
|
|
|
|
{ pmConnections :: ConnectionsGauge,
|
2022-08-15 08:32:55 +03:00
|
|
|
pmGraphQLRequestMetrics :: GraphQLRequestMetrics,
|
2022-12-28 06:47:42 +03:00
|
|
|
pmEventTriggerMetrics :: EventTriggerMetrics,
|
|
|
|
pmWebSocketBytesReceived :: Counter,
|
|
|
|
pmWebSocketBytesSent :: Counter,
|
|
|
|
pmActionBytesReceived :: Counter,
|
|
|
|
pmActionBytesSent :: Counter,
|
2023-04-18 18:31:33 +03:00
|
|
|
pmScheduledTriggerMetrics :: ScheduledTriggerMetrics,
|
2023-04-25 23:28:03 +03:00
|
|
|
pmSubscriptionMetrics :: SubscriptionMetrics,
|
2023-05-16 10:37:02 +03:00
|
|
|
pmWebsocketMsgQueueTimeSeconds :: Histogram,
|
|
|
|
pmCacheRequestMetrics :: CacheRequestMetrics
|
2022-07-24 00:18:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
data GraphQLRequestMetrics = GraphQLRequestMetrics
|
|
|
|
{ gqlRequestsQuerySuccess :: Counter,
|
|
|
|
gqlRequestsQueryFailure :: Counter,
|
|
|
|
gqlRequestsMutationSuccess :: Counter,
|
|
|
|
gqlRequestsMutationFailure :: Counter,
|
2023-04-25 23:28:03 +03:00
|
|
|
gqlRequestsSubscriptionSuccess :: Counter,
|
|
|
|
gqlRequestsSubscriptionFailure :: Counter,
|
2022-07-24 00:18:01 +03:00
|
|
|
gqlRequestsUnknownFailure :: Counter,
|
|
|
|
gqlExecutionTimeSecondsQuery :: Histogram,
|
|
|
|
gqlExecutionTimeSecondsMutation :: Histogram
|
|
|
|
}
|
|
|
|
|
2022-08-15 08:32:55 +03:00
|
|
|
data EventTriggerMetrics = EventTriggerMetrics
|
|
|
|
{ eventTriggerHTTPWorkers :: Gauge,
|
2023-05-15 18:04:21 +03:00
|
|
|
eventsFetchedPerBatch :: Gauge,
|
2023-05-24 16:22:06 +03:00
|
|
|
eventQueueTimeSeconds :: HistogramVector (Maybe DynamicEventTriggerLabel),
|
2022-11-16 20:10:59 +03:00
|
|
|
eventsFetchTimePerBatch :: Histogram,
|
2023-05-24 16:22:06 +03:00
|
|
|
eventWebhookProcessingTime :: HistogramVector (Maybe DynamicEventTriggerLabel),
|
|
|
|
eventProcessingTime :: HistogramVector (Maybe DynamicEventTriggerLabel),
|
2022-12-28 06:47:42 +03:00
|
|
|
eventTriggerBytesReceived :: Counter,
|
2023-03-29 19:32:14 +03:00
|
|
|
eventTriggerBytesSent :: Counter,
|
2023-05-24 16:22:06 +03:00
|
|
|
eventProcessedTotal :: CounterVector EventStatusWithTriggerLabel,
|
|
|
|
eventInvocationTotal :: CounterVector EventStatusWithTriggerLabel
|
2022-08-15 08:32:55 +03:00
|
|
|
}
|
|
|
|
|
2023-03-30 08:51:18 +03:00
|
|
|
data ScheduledTriggerMetrics = ScheduledTriggerMetrics
|
|
|
|
{ stmScheduledTriggerBytesReceived :: Counter,
|
|
|
|
stmScheduledTriggerBytesSent :: Counter,
|
|
|
|
stmCronEventsInvocationTotalSuccess :: Counter,
|
|
|
|
stmCronEventsInvocationTotalFailure :: Counter,
|
|
|
|
stmOneOffEventsInvocationTotalSuccess :: Counter,
|
|
|
|
stmOneOffEventsInvocationTotalFailure :: Counter,
|
|
|
|
stmCronEventsProcessedTotalSuccess :: Counter,
|
|
|
|
stmCronEventsProcessedTotalFailure :: Counter,
|
|
|
|
stmOneOffEventsProcessedTotalSuccess :: Counter,
|
|
|
|
stmOneOffEventsProcessedTotalFailure :: Counter
|
|
|
|
}
|
|
|
|
|
2023-04-18 18:31:33 +03:00
|
|
|
data SubscriptionMetrics = SubscriptionMetrics
|
|
|
|
{ submActiveLiveQueryPollers :: Gauge,
|
2023-04-24 10:42:31 +03:00
|
|
|
submActiveStreamingPollers :: Gauge,
|
|
|
|
submActiveLiveQueryPollersInError :: Gauge,
|
2023-05-18 15:55:53 +03:00
|
|
|
submActiveStreamingPollersInError :: Gauge,
|
|
|
|
submTotalTime :: HistogramVector.HistogramVector SubscriptionLabel,
|
|
|
|
submDBExecTotalTime :: HistogramVector.HistogramVector SubscriptionLabel,
|
|
|
|
submActiveSubscriptions :: GaugeVector.GaugeVector SubscriptionLabel
|
2023-04-18 18:31:33 +03:00
|
|
|
}
|
|
|
|
|
2023-05-16 10:37:02 +03:00
|
|
|
data CacheRequestMetrics = CacheRequestMetrics
|
|
|
|
{ crmCacheHits :: Counter,
|
|
|
|
crmCacheMisses :: Counter
|
|
|
|
}
|
|
|
|
|
2022-07-24 00:18:01 +03:00
|
|
|
-- | Create dummy mutable references without associating them to a metrics
|
|
|
|
-- store.
|
|
|
|
makeDummyPrometheusMetrics :: IO PrometheusMetrics
|
|
|
|
makeDummyPrometheusMetrics = do
|
|
|
|
pmConnections <- newConnectionsGauge
|
|
|
|
pmGraphQLRequestMetrics <- makeDummyGraphQLRequestMetrics
|
2022-08-15 08:32:55 +03:00
|
|
|
pmEventTriggerMetrics <- makeDummyEventTriggerMetrics
|
2022-12-28 06:47:42 +03:00
|
|
|
pmWebSocketBytesReceived <- Counter.new
|
|
|
|
pmWebSocketBytesSent <- Counter.new
|
|
|
|
pmActionBytesReceived <- Counter.new
|
|
|
|
pmActionBytesSent <- Counter.new
|
2023-03-30 08:51:18 +03:00
|
|
|
pmScheduledTriggerMetrics <- makeDummyScheduledTriggerMetrics
|
2023-04-18 18:31:33 +03:00
|
|
|
pmSubscriptionMetrics <- makeDummySubscriptionMetrics
|
2023-04-25 23:28:03 +03:00
|
|
|
pmWebsocketMsgQueueTimeSeconds <- Histogram.new []
|
2023-05-16 10:37:02 +03:00
|
|
|
pmCacheRequestMetrics <- makeDummyCacheRequestMetrics
|
2022-07-24 00:18:01 +03:00
|
|
|
pure PrometheusMetrics {..}
|
|
|
|
|
|
|
|
makeDummyGraphQLRequestMetrics :: IO GraphQLRequestMetrics
|
|
|
|
makeDummyGraphQLRequestMetrics = do
|
|
|
|
gqlRequestsQuerySuccess <- Counter.new
|
|
|
|
gqlRequestsQueryFailure <- Counter.new
|
|
|
|
gqlRequestsMutationSuccess <- Counter.new
|
|
|
|
gqlRequestsMutationFailure <- Counter.new
|
2023-04-25 23:28:03 +03:00
|
|
|
gqlRequestsSubscriptionSuccess <- Counter.new
|
|
|
|
gqlRequestsSubscriptionFailure <- Counter.new
|
2022-07-24 00:18:01 +03:00
|
|
|
gqlRequestsUnknownFailure <- Counter.new
|
|
|
|
gqlExecutionTimeSecondsQuery <- Histogram.new []
|
|
|
|
gqlExecutionTimeSecondsMutation <- Histogram.new []
|
|
|
|
pure GraphQLRequestMetrics {..}
|
|
|
|
|
2022-08-15 08:32:55 +03:00
|
|
|
makeDummyEventTriggerMetrics :: IO EventTriggerMetrics
|
|
|
|
makeDummyEventTriggerMetrics = do
|
|
|
|
eventTriggerHTTPWorkers <- Gauge.new
|
2023-05-15 18:04:21 +03:00
|
|
|
eventsFetchedPerBatch <- Gauge.new
|
2023-05-24 16:22:06 +03:00
|
|
|
eventQueueTimeSeconds <- HistogramVector.new []
|
2022-11-16 20:10:59 +03:00
|
|
|
eventsFetchTimePerBatch <- Histogram.new []
|
2023-05-24 16:22:06 +03:00
|
|
|
eventWebhookProcessingTime <- HistogramVector.new []
|
2023-05-11 08:19:42 +03:00
|
|
|
eventProcessingTime <- HistogramVector.new []
|
2022-12-28 06:47:42 +03:00
|
|
|
eventTriggerBytesReceived <- Counter.new
|
|
|
|
eventTriggerBytesSent <- Counter.new
|
2023-05-24 16:22:06 +03:00
|
|
|
eventProcessedTotal <- CounterVector.new
|
|
|
|
eventInvocationTotal <- CounterVector.new
|
2022-08-15 08:32:55 +03:00
|
|
|
pure EventTriggerMetrics {..}
|
|
|
|
|
2023-03-30 08:51:18 +03:00
|
|
|
makeDummyScheduledTriggerMetrics :: IO ScheduledTriggerMetrics
|
|
|
|
makeDummyScheduledTriggerMetrics = do
|
|
|
|
stmScheduledTriggerBytesReceived <- Counter.new
|
|
|
|
stmScheduledTriggerBytesSent <- Counter.new
|
|
|
|
stmCronEventsInvocationTotalSuccess <- Counter.new
|
|
|
|
stmCronEventsInvocationTotalFailure <- Counter.new
|
|
|
|
stmOneOffEventsInvocationTotalSuccess <- Counter.new
|
|
|
|
stmOneOffEventsInvocationTotalFailure <- Counter.new
|
|
|
|
stmCronEventsProcessedTotalSuccess <- Counter.new
|
|
|
|
stmCronEventsProcessedTotalFailure <- Counter.new
|
|
|
|
stmOneOffEventsProcessedTotalSuccess <- Counter.new
|
|
|
|
stmOneOffEventsProcessedTotalFailure <- Counter.new
|
|
|
|
pure ScheduledTriggerMetrics {..}
|
|
|
|
|
2023-04-18 18:31:33 +03:00
|
|
|
makeDummySubscriptionMetrics :: IO SubscriptionMetrics
|
|
|
|
makeDummySubscriptionMetrics = do
|
|
|
|
submActiveLiveQueryPollers <- Gauge.new
|
|
|
|
submActiveStreamingPollers <- Gauge.new
|
2023-04-24 10:42:31 +03:00
|
|
|
submActiveLiveQueryPollersInError <- Gauge.new
|
|
|
|
submActiveStreamingPollersInError <- Gauge.new
|
2023-05-18 15:55:53 +03:00
|
|
|
submTotalTime <- HistogramVector.new []
|
|
|
|
submDBExecTotalTime <- HistogramVector.new []
|
|
|
|
submActiveSubscriptions <- GaugeVector.new
|
2023-04-18 18:31:33 +03:00
|
|
|
pure SubscriptionMetrics {..}
|
|
|
|
|
2023-05-16 10:37:02 +03:00
|
|
|
makeDummyCacheRequestMetrics :: IO CacheRequestMetrics
|
|
|
|
makeDummyCacheRequestMetrics = do
|
|
|
|
crmCacheHits <- Counter.new
|
|
|
|
crmCacheMisses <- Counter.new
|
|
|
|
pure CacheRequestMetrics {..}
|
|
|
|
|
2022-07-24 00:18:01 +03:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
-- | A mutable reference for atomically sampling the number of websocket
|
|
|
|
-- connections and number of threads forked by the warp webserver.
|
|
|
|
--
|
|
|
|
-- Because we derive the number of (non-websocket) HTTP connections by the
|
|
|
|
-- difference of these two metrics, we must sample them simultaneously,
|
|
|
|
-- otherwise we might report a negative number of HTTP connections.
|
|
|
|
newtype ConnectionsGauge = ConnectionsGauge (IORef Connections)
|
|
|
|
|
|
|
|
data Connections = Connections
|
|
|
|
{ connWarpThreads :: Int64,
|
|
|
|
connWebsockets :: Int64
|
|
|
|
}
|
|
|
|
|
|
|
|
newConnectionsGauge :: IO ConnectionsGauge
|
|
|
|
newConnectionsGauge =
|
|
|
|
ConnectionsGauge
|
|
|
|
<$> newIORef Connections {connWarpThreads = 0, connWebsockets = 0}
|
|
|
|
|
|
|
|
readConnectionsGauge :: ConnectionsGauge -> IO Connections
|
|
|
|
readConnectionsGauge (ConnectionsGauge ref) = readIORef ref
|
|
|
|
|
|
|
|
incWarpThreads :: ConnectionsGauge -> IO ()
|
|
|
|
incWarpThreads =
|
|
|
|
modifyConnectionsGauge $ \connections ->
|
|
|
|
connections {connWarpThreads = connWarpThreads connections + 1}
|
|
|
|
|
|
|
|
decWarpThreads :: ConnectionsGauge -> IO ()
|
|
|
|
decWarpThreads =
|
|
|
|
modifyConnectionsGauge $ \connections ->
|
|
|
|
connections {connWarpThreads = connWarpThreads connections - 1}
|
|
|
|
|
|
|
|
incWebsocketConnections :: ConnectionsGauge -> IO ()
|
|
|
|
incWebsocketConnections =
|
|
|
|
modifyConnectionsGauge $ \connections ->
|
|
|
|
connections {connWebsockets = connWebsockets connections + 1}
|
|
|
|
|
|
|
|
decWebsocketConnections :: ConnectionsGauge -> IO ()
|
|
|
|
decWebsocketConnections =
|
|
|
|
modifyConnectionsGauge $ \connections ->
|
|
|
|
connections {connWebsockets = connWebsockets connections - 1}
|
|
|
|
|
|
|
|
modifyConnectionsGauge ::
|
|
|
|
(Connections -> Connections) -> ConnectionsGauge -> IO ()
|
|
|
|
modifyConnectionsGauge f (ConnectionsGauge ref) =
|
|
|
|
atomicModifyIORef' ref $ \connections -> (f connections, ())
|
2023-05-11 08:19:42 +03:00
|
|
|
|
2023-05-24 16:22:06 +03:00
|
|
|
data DynamicEventTriggerLabel = DynamicEventTriggerLabel
|
|
|
|
{ _detlTriggerName :: TriggerName,
|
|
|
|
_detlSourceName :: SourceName
|
|
|
|
}
|
2023-05-11 08:19:42 +03:00
|
|
|
deriving (Ord, Eq)
|
|
|
|
|
2023-05-24 16:22:06 +03:00
|
|
|
instance ToLabels (Maybe DynamicEventTriggerLabel) where
|
2023-05-11 08:19:42 +03:00
|
|
|
toLabels Nothing = Map.empty
|
2023-05-24 16:22:06 +03:00
|
|
|
toLabels (Just (DynamicEventTriggerLabel triggerName sourceName)) = Map.fromList $ [("trigger_name", triggerNameToTxt triggerName), ("source_name", sourceNameToText sourceName)]
|
|
|
|
|
|
|
|
data ResponseStatus = Success | Failed
|
|
|
|
|
|
|
|
-- TODO: Make this a method of a new typeclass of the metrics library
|
|
|
|
responseStatusToLabelValue :: ResponseStatus -> Text
|
|
|
|
responseStatusToLabelValue = \case
|
|
|
|
Success -> "success"
|
|
|
|
Failed -> "failed"
|
|
|
|
|
|
|
|
newtype EventStatusLabel = EventStatusLabel
|
|
|
|
{ status :: Text
|
|
|
|
}
|
|
|
|
deriving stock (Generic, Ord, Eq)
|
|
|
|
deriving anyclass (ToLabels)
|
|
|
|
|
|
|
|
eventSuccessLabel :: EventStatusLabel
|
|
|
|
eventSuccessLabel = EventStatusLabel $ responseStatusToLabelValue Success
|
|
|
|
|
|
|
|
eventFailedLabel :: EventStatusLabel
|
|
|
|
eventFailedLabel = EventStatusLabel $ responseStatusToLabelValue Failed
|
|
|
|
|
|
|
|
data EventStatusWithTriggerLabel = EventStatusWithTriggerLabel
|
|
|
|
{ _eswtlStatus :: EventStatusLabel,
|
|
|
|
_eswtlDynamicLabels :: Maybe DynamicEventTriggerLabel
|
|
|
|
}
|
|
|
|
deriving stock (Generic, Ord, Eq)
|
|
|
|
|
|
|
|
instance ToLabels (EventStatusWithTriggerLabel) where
|
|
|
|
toLabels (EventStatusWithTriggerLabel esl tl) = (HashMap.fromList $ [("status", status esl)]) <> toLabels tl
|
2023-05-11 08:19:42 +03:00
|
|
|
|
2023-05-18 15:55:53 +03:00
|
|
|
data SubscriptionKindLabel = SubscriptionKindLabel
|
|
|
|
{ subscription_kind :: Text
|
|
|
|
}
|
|
|
|
deriving stock (Generic, Ord, Eq)
|
|
|
|
deriving anyclass (ToLabels)
|
|
|
|
|
|
|
|
streamingSubscriptionLabel :: SubscriptionKindLabel
|
|
|
|
streamingSubscriptionLabel = SubscriptionKindLabel "streaming"
|
|
|
|
|
|
|
|
liveQuerySubscriptionLabel :: SubscriptionKindLabel
|
|
|
|
liveQuerySubscriptionLabel = SubscriptionKindLabel "live-query"
|
|
|
|
|
|
|
|
data DynamicSubscriptionLabel = DynamicSubscriptionLabel
|
|
|
|
{ _dslParamQueryHash :: ParameterizedQueryHash,
|
|
|
|
_dslOperationName :: Maybe OperationName
|
|
|
|
}
|
|
|
|
deriving stock (Generic, Ord, Eq)
|
|
|
|
|
|
|
|
instance ToLabels DynamicSubscriptionLabel where
|
|
|
|
toLabels (DynamicSubscriptionLabel hash opName) =
|
2023-05-24 16:51:56 +03:00
|
|
|
Map.fromList
|
|
|
|
$ [("parameterized_query_hash", bsToTxt $ unParamQueryHash hash)]
|
|
|
|
<> maybe [] (\op -> [("operation_name", G.unName $ _unOperationName op)]) opName
|
2023-05-18 15:55:53 +03:00
|
|
|
|
|
|
|
data SubscriptionLabel = SubscriptionLabel
|
|
|
|
{ _slKind :: SubscriptionKindLabel,
|
|
|
|
_slDynamicLabels :: Maybe DynamicSubscriptionLabel
|
|
|
|
}
|
|
|
|
deriving stock (Generic, Ord, Eq)
|
|
|
|
|
|
|
|
instance ToLabels SubscriptionLabel where
|
|
|
|
toLabels (SubscriptionLabel kind Nothing) = Map.fromList $ [("subscription_kind", subscription_kind kind)]
|
|
|
|
toLabels (SubscriptionLabel kind (Just dl)) = (Map.fromList $ [("subscription_kind", subscription_kind kind)]) <> toLabels dl
|
|
|
|
|
2023-05-11 08:19:42 +03:00
|
|
|
-- | Record metrics with dynamic label
|
|
|
|
recordMetricWithLabel ::
|
|
|
|
(MonadIO m) =>
|
|
|
|
(IO GranularPrometheusMetricsState) ->
|
|
|
|
-- should the metric be observed without a label when granularMetricsState is OFF
|
|
|
|
Bool ->
|
|
|
|
-- the action to perform when granularMetricsState is ON
|
|
|
|
IO () ->
|
|
|
|
-- the action to perform when granularMetricsState is OFF
|
|
|
|
IO () ->
|
|
|
|
m ()
|
|
|
|
recordMetricWithLabel getMetricState alwaysObserve metricActionWithLabel metricActionWithoutLabel = do
|
|
|
|
metricState <- liftIO $ getMetricState
|
|
|
|
case metricState of
|
|
|
|
GranularMetricsOn -> liftIO $ metricActionWithLabel
|
|
|
|
-- Some metrics do not make sense without a dynamic label, hence only record the
|
|
|
|
-- metric when alwaysObserve is set to true else do not record the metric
|
|
|
|
GranularMetricsOff -> do
|
2023-05-24 16:51:56 +03:00
|
|
|
when alwaysObserve
|
|
|
|
$ liftIO
|
|
|
|
$ metricActionWithoutLabel
|
2023-05-11 08:19:42 +03:00
|
|
|
|
|
|
|
-- | Observe a histogram metric with a label.
|
|
|
|
--
|
|
|
|
-- If the granularity is set to 'GranularMetricsOn', the label will be
|
|
|
|
-- included in the metric. Otherwise, the label will be set to `Nothing`
|
|
|
|
observeHistogramWithLabel ::
|
|
|
|
(Ord l, MonadIO m) =>
|
|
|
|
(IO GranularPrometheusMetricsState) ->
|
|
|
|
-- should the metric be observed without a label when granularMetricsState is OFF
|
|
|
|
Bool ->
|
|
|
|
HistogramVector (Maybe l) ->
|
|
|
|
l ->
|
|
|
|
Double ->
|
|
|
|
m ()
|
|
|
|
observeHistogramWithLabel getMetricState alwaysObserve histogramVector label value = do
|
|
|
|
recordMetricWithLabel
|
|
|
|
getMetricState
|
|
|
|
alwaysObserve
|
|
|
|
(liftIO $ HistogramVector.observe histogramVector (Just label) value)
|
|
|
|
(liftIO $ HistogramVector.observe histogramVector Nothing value)
|
2023-05-18 15:55:53 +03:00
|
|
|
|
|
|
|
-- | Record a subscription metric for all the operation names present in the subscription.
|
|
|
|
-- Use this when you want to update the same value of the metric for all the operation names.
|
|
|
|
recordSubcriptionMetric ::
|
|
|
|
(MonadIO m) =>
|
|
|
|
(IO GranularPrometheusMetricsState) ->
|
|
|
|
-- should the metric be observed without a label when granularMetricsState is OFF
|
|
|
|
Bool ->
|
|
|
|
HashMap (Maybe OperationName) Int ->
|
|
|
|
ParameterizedQueryHash ->
|
|
|
|
SubscriptionKindLabel ->
|
|
|
|
-- the mertic action to perform
|
|
|
|
(SubscriptionLabel -> IO ()) ->
|
|
|
|
m ()
|
|
|
|
recordSubcriptionMetric getMetricState alwaysObserve operationNamesMap parameterizedQueryHash subscriptionKind metricAction = do
|
|
|
|
-- if no operation names are present, then emit metric with only param query hash as dynamic label
|
|
|
|
if (null operationNamesMap)
|
|
|
|
then do
|
|
|
|
let promMetricGranularLabel = SubscriptionLabel subscriptionKind (Just $ DynamicSubscriptionLabel parameterizedQueryHash Nothing)
|
|
|
|
promMetricLabel = SubscriptionLabel subscriptionKind Nothing
|
|
|
|
recordMetricWithLabel
|
|
|
|
getMetricState
|
|
|
|
alwaysObserve
|
|
|
|
(metricAction promMetricGranularLabel)
|
|
|
|
(metricAction promMetricLabel)
|
|
|
|
else -- if operationNames are present, then emit the same metric for all the operation names
|
|
|
|
do
|
|
|
|
let operationNames = HashMap.keys operationNamesMap
|
|
|
|
for_ operationNames $ \opName -> do
|
|
|
|
let promMetricGranularLabel = SubscriptionLabel subscriptionKind (Just $ DynamicSubscriptionLabel parameterizedQueryHash opName)
|
|
|
|
promMetricLabel = SubscriptionLabel subscriptionKind Nothing
|
|
|
|
recordMetricWithLabel
|
|
|
|
getMetricState
|
|
|
|
alwaysObserve
|
|
|
|
(metricAction promMetricGranularLabel)
|
|
|
|
(metricAction promMetricLabel)
|