Skip to content

Commit 1a9a1ed

Browse files
committed
feat: add metrics for used and created connections
1 parent 40bd9a7 commit 1a9a1ed

File tree

7 files changed

+46
-33
lines changed

7 files changed

+46
-33
lines changed

cabal.project

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ packages: postgrest.cabal
22
tests: true
33
package *
44
ghc-options: -split-sections
5+
packages:
6+
/home/laurence/Projects/hasql-notifications

nix/overlays/haskell-packages.nix

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,21 +57,22 @@ let
5757
}
5858
{ });
5959

60-
hasql-pool = lib.dontCheck prev.hasql-pool_1_0_1;
60+
hasql-pool = lib.dontCheck (prev.callHackageDirect
61+
{
62+
pkg = "hasql-pool";
63+
ver = "1.1";
64+
sha256 = "sha256-TJZdUwsBo4pLLYydc8FgxYFI37Tj5K+E7idf4fQYEjU=";
65+
}
66+
{ }
67+
);
6168

6269
postgresql-libpq = lib.dontCheck
6370
(prev.postgresql-libpq.override {
6471
postgresql = super.libpq;
6572
});
6673

67-
hasql-notifications = lib.dontCheck (prev.callHackageDirect
68-
{
69-
pkg = "hasql-notifications";
70-
ver = "0.2.1.1";
71-
sha256 = "sha256-oPhKA/pSQGJvgQyhsi7CVr9iDT7uWpKUz0iJfXsaxXo=";
72-
}
73-
{ }
74-
);
74+
hasql-notifications = lib.dontCheck
75+
(prev.callCabal2nixWithOptions "hasql-notifications" /home/laurence/Projects/hasql-notifications "--subpath=." {} );
7576

7677
};
7778
in

postgrest.cabal

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ library
110110
, hasql >= 1.6.1.1 && < 1.7
111111
, hasql-dynamic-statements >= 0.3.1 && < 0.4
112112
, hasql-notifications >= 0.2.1.1 && < 0.3
113-
, hasql-pool >= 1.0.1 && < 1.1
113+
, hasql-pool >= 1.0.1 && < 1.2
114114
, hasql-transaction >= 1.0.1 && < 1.1
115115
, heredoc >= 0.2 && < 0.3
116116
, http-types >= 0.12.2 && < 0.13
@@ -256,7 +256,7 @@ test-suite spec
256256
, bytestring >= 0.10.8 && < 0.13
257257
, case-insensitive >= 1.2 && < 1.3
258258
, containers >= 0.5.7 && < 0.7
259-
, hasql-pool >= 1.0.1 && < 1.1
259+
, hasql-pool >= 1.0.1 && < 1.2
260260
, hasql-transaction >= 1.0.1 && < 1.1
261261
, heredoc >= 0.2 && < 0.3
262262
, hspec >= 2.3 && < 2.12

src/PostgREST/Metrics.hs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,45 @@ import PostgREST.Observation
1414

1515
import Protolude
1616

17-
data MetricsState =
18-
MetricsState Prom.Counter Prom.Gauge Prom.Gauge Prom.Gauge Prom.Counter Prom.Gauge
17+
data MetricsState = MetricsState
18+
{ msPoolTimeouts :: Prom.Counter
19+
, msPoolCreated :: Prom.Counter
20+
, msPoolAvailable :: Prom.Gauge
21+
, msPoolWaiting :: Prom.Gauge
22+
, msPoolUsed :: Prom.Gauge
23+
, msPoolMaxSize :: Prom.Gauge
24+
, msSchCacheLoads :: Prom.Counter
25+
, msSchCacheQueryTime :: Prom.Gauge
26+
}
1927

2028
init :: Int -> IO MetricsState
2129
init configDbPoolSize = do
2230
poolTimeouts <- Prom.register $ Prom.counter (Prom.Info "pgrst_db_pool_timeouts_total" "The total number of pool connection timeouts")
31+
poolCreated <- Prom.register $ Prom.counter (Prom.Info "pgrst_db_pool_created" "The total number of created pool connections")
2332
poolAvailable <- Prom.register $ Prom.gauge (Prom.Info "pgrst_db_pool_available" "Available connections in the pool")
2433
poolWaiting <- Prom.register $ Prom.gauge (Prom.Info "pgrst_db_pool_waiting" "Requests waiting to acquire a pool connection")
34+
poolUsed <- Prom.register $ Prom.gauge (Prom.Info "pgrst_db_pool_used" "Used connections in the pool")
2535
poolMaxSize <- Prom.register $ Prom.gauge (Prom.Info "pgrst_db_pool_max" "Max pool connections")
2636
schemaCacheLoads <- Prom.register $ Prom.counter (Prom.Info "pgrst_schema_cache_loads_total" "The total number of times the schema cache was loaded")
2737
schemaCacheQueryTime <- Prom.register $ Prom.gauge (Prom.Info "pgrst_schema_cache_query_time_seconds" "The query time in seconds of the last schema cache load")
2838
Prom.setGauge poolMaxSize (fromIntegral configDbPoolSize)
29-
pure $ MetricsState poolTimeouts poolAvailable poolWaiting poolMaxSize schemaCacheLoads schemaCacheQueryTime
39+
pure $ MetricsState poolTimeouts poolCreated poolAvailable poolWaiting poolUsed poolMaxSize schemaCacheLoads schemaCacheQueryTime
3040

3141
observationMetrics :: MetricsState -> ObservationHandler
32-
observationMetrics (MetricsState poolTimeouts poolAvailable poolWaiting _ schemaCacheLoads schemaCacheQueryTime) obs = case obs of
42+
observationMetrics (MetricsState poolTimeouts poolCreated poolAvailable poolWaiting poolUsed _ schemaCacheLoads schemaCacheQueryTime) obs = case obs of
3343
(PoolAcqTimeoutObs _) -> do
3444
Prom.incCounter poolTimeouts
3545
(HasqlPoolObs (SQL.ConnectionObservation _ status)) -> case status of
36-
SQL.ReadyForUseConnectionStatus -> do
37-
Prom.incGauge poolAvailable
46+
SQL.ReadyForUseConnectionStatus reason -> case reason of
47+
SQL.EstablishedConnectionReadyForUseReason -> do
48+
Prom.incCounter poolCreated
49+
Prom.incGauge poolAvailable
50+
_ -> do
51+
Prom.decGauge poolUsed
52+
Prom.incGauge poolAvailable
3853
SQL.InUseConnectionStatus -> do
3954
Prom.decGauge poolAvailable
55+
Prom.incGauge poolUsed
4056
SQL.TerminatedConnectionStatus _ -> do
4157
Prom.decGauge poolAvailable
4258
SQL.ConnectingConnectionStatus -> pure ()

src/PostgREST/Observation.hs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,15 @@ observationMessage = \case
118118
HasqlPoolObs (SQL.ConnectionObservation uuid status) ->
119119
"Connection " <> show uuid <> (
120120
case status of
121-
SQL.ConnectingConnectionStatus -> " is being established"
122-
SQL.ReadyForUseConnectionStatus -> " is available"
123-
SQL.InUseConnectionStatus -> " is used"
121+
SQL.ConnectingConnectionStatus -> " is being established"
122+
SQL.ReadyForUseConnectionStatus _ -> " is available"
123+
SQL.InUseConnectionStatus -> " is used"
124124
SQL.TerminatedConnectionStatus reason -> " is terminated due to " <> case reason of
125125
SQL.AgingConnectionTerminationReason -> "max lifetime"
126126
SQL.IdlenessConnectionTerminationReason -> "max idletime"
127127
SQL.ReleaseConnectionTerminationReason -> "release"
128128
SQL.NetworkErrorConnectionTerminationReason _ -> "network error" -- usage error is already logged, no need to repeat the same message.
129+
SQL.InitializationErrorTerminationReason _ -> "session initialization error"
129130
)
130131
_ -> mempty
131132
where

stack.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ nix:
1212
extra-deps:
1313
- configurator-pg-0.2.7
1414
- fuzzyset-0.2.4
15-
- hasql-notifications-0.2.1.1
16-
- hasql-pool-1.0.1
15+
- /home/laurence/Projects/hasql-notifications
16+
- hasql-pool-1.1
1717
- megaparsec-9.2.2
1818
- postgresql-libpq-0.10.0.0

stack.yaml.lock

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,12 @@ packages:
1919
original:
2020
hackage: fuzzyset-0.2.4
2121
- completed:
22-
hackage: hasql-notifications-0.2.1.1@sha256:e4f41f462e96f3af3f088b747daeeb2275ceaebd0e4b0d05187bd10d329afada,2021
22+
hackage: hasql-pool-1.1@sha256:409cd9e35cf28bcfe591964d992e4f2cf9a1b0ecab88a459a3c9e3e9868961a9,2368
2323
pantry-tree:
24-
sha256: f9041b1c242436324372f6be9a4f2e5cf70203c3efad36a4e9ea4cca5113a2ec
25-
size: 452
24+
sha256: 8c1a592b8d8e22f79e3ae051eb3fc7c5c4712c3060bc6f5e867316001bfe5432
25+
size: 888
2626
original:
27-
hackage: hasql-notifications-0.2.1.1
28-
- completed:
29-
hackage: hasql-pool-1.0.1@sha256:3cfb4c7153a6c536ac7e126c17723e6d26ee03794954deed2d72bcc826d05a40,2302
30-
pantry-tree:
31-
sha256: d98e1269bdd60989b0eb0b84e1d5357eaa9f92821439d9f206663b7251ee95b2
32-
size: 799
33-
original:
34-
hackage: hasql-pool-1.0.1
27+
hackage: hasql-pool-1.1
3528
- completed:
3629
hackage: megaparsec-9.2.2@sha256:c306a135ec25d91d252032c6128f03598a00e87ea12fcf5fc4878fdffc75c768,3219
3730
pantry-tree:

0 commit comments

Comments
 (0)