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 (..),
|
2022-07-24 00:18:01 +03:00
|
|
|
makeDummyPrometheusMetrics,
|
|
|
|
ConnectionsGauge,
|
|
|
|
Connections (..),
|
|
|
|
newConnectionsGauge,
|
|
|
|
readConnectionsGauge,
|
|
|
|
incWarpThreads,
|
|
|
|
decWarpThreads,
|
|
|
|
incWebsocketConnections,
|
|
|
|
decWebsocketConnections,
|
|
|
|
)
|
|
|
|
where
|
|
|
|
|
|
|
|
import Data.IORef (IORef, atomicModifyIORef', newIORef, readIORef)
|
|
|
|
import Data.Int (Int64)
|
|
|
|
import Hasura.Prelude
|
|
|
|
import System.Metrics.Prometheus.Counter (Counter)
|
|
|
|
import System.Metrics.Prometheus.Counter qualified as Counter
|
|
|
|
import System.Metrics.Prometheus.Gauge (Gauge)
|
|
|
|
import System.Metrics.Prometheus.Gauge qualified as Gauge
|
|
|
|
import System.Metrics.Prometheus.Histogram (Histogram)
|
|
|
|
import System.Metrics.Prometheus.Histogram qualified as Histogram
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
-- | Mutable references for Prometheus metrics.
|
|
|
|
data PrometheusMetrics = PrometheusMetrics
|
|
|
|
{ pmConnections :: ConnectionsGauge,
|
|
|
|
pmActiveSubscriptions :: Gauge,
|
2022-08-15 08:32:55 +03:00
|
|
|
pmGraphQLRequestMetrics :: GraphQLRequestMetrics,
|
|
|
|
pmEventTriggerMetrics :: EventTriggerMetrics
|
2022-07-24 00:18:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
data GraphQLRequestMetrics = GraphQLRequestMetrics
|
|
|
|
{ gqlRequestsQuerySuccess :: Counter,
|
|
|
|
gqlRequestsQueryFailure :: Counter,
|
|
|
|
gqlRequestsMutationSuccess :: Counter,
|
|
|
|
gqlRequestsMutationFailure :: Counter,
|
|
|
|
gqlRequestsUnknownFailure :: Counter,
|
|
|
|
gqlExecutionTimeSecondsQuery :: Histogram,
|
|
|
|
gqlExecutionTimeSecondsMutation :: Histogram
|
|
|
|
}
|
|
|
|
|
2022-08-15 08:32:55 +03:00
|
|
|
data EventTriggerMetrics = EventTriggerMetrics
|
|
|
|
{ eventTriggerHTTPWorkers :: Gauge,
|
2022-11-16 20:10:59 +03:00
|
|
|
eventQueueTimeSeconds :: Histogram,
|
|
|
|
eventsFetchTimePerBatch :: Histogram,
|
2022-11-21 10:47:45 +03:00
|
|
|
eventWebhookProcessingTime :: Histogram
|
2022-08-15 08:32:55 +03:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
pmActiveSubscriptions <- Gauge.new
|
|
|
|
pmGraphQLRequestMetrics <- makeDummyGraphQLRequestMetrics
|
2022-08-15 08:32:55 +03:00
|
|
|
pmEventTriggerMetrics <- makeDummyEventTriggerMetrics
|
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
|
|
|
|
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
|
|
|
|
eventQueueTimeSeconds <- Histogram.new []
|
2022-11-16 20:10:59 +03:00
|
|
|
eventsFetchTimePerBatch <- Histogram.new []
|
2022-11-21 10:47:45 +03:00
|
|
|
eventWebhookProcessingTime <- Histogram.new []
|
2022-08-15 08:32:55 +03:00
|
|
|
pure EventTriggerMetrics {..}
|
|
|
|
|
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, ())
|