Skip to content

Commit b3e468c

Browse files
committed
Add git revision to Prometheus metrics
1 parent e1c9768 commit b3e468c

File tree

5 files changed

+28
-6
lines changed

5 files changed

+28
-6
lines changed

cabal.project.freeze

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ constraints: any.Cabal ==3.14.1.0,
190190
any.ghc-platform ==0.1.0.0,
191191
any.ghc-prim ==0.12.0,
192192
any.ghci ==9.10.3,
193+
any.gitrev ==1.3.1,
193194
any.gridtables ==0.1.1.0,
194195
any.haddock-library ==1.11.0,
195196
any.half ==0.3.3,

flora.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ library
191191
effectful-plugin,
192192
envparse,
193193
filepath,
194+
gitrev,
194195
heptapod ^>=1.1.0.0,
195196
hsec-core,
196197
http-api-data,

src/core/Flora/Environment/Env.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ data FloraEnv = FloraEnv
3737

3838
data AppMetrics = AppMetrics
3939
{ packageImportCounter :: P.Vector P.Label1 P.Counter
40+
, gitRevision :: P.Vector P.Label1 P.Gauge
4041
}
4142

4243
data BlobStoreImpl = BlobStoreFS FilePath | BlobStorePure

src/core/Flora/Monitoring.hs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
{-# LANGUAGE TemplateHaskell #-}
12
module Flora.Monitoring
23
( increaseCounter
34
, increasePackageImportCounter
45
, registerMetrics
56
, increaseCounterBy
67
, increasePackageImportCounterBy
8+
, setGitHash
79
) where
810

911
import Control.Monad (replicateM_)
@@ -12,6 +14,7 @@ import Effectful
1214
import Effectful.Prometheus
1315
import Prometheus
1416
import Prometheus qualified as P
17+
import Development.GitRev (gitHash)
1518

1619
import Flora.Environment.Env
1720

@@ -24,8 +27,21 @@ registerMetrics = do
2427
{ metricName = "flora_imported_packages_total"
2528
, metricHelp = "Packages imported and their index"
2629
}
30+
let gitHashMetric =
31+
P.vector "git_revision" $
32+
P.gauge
33+
P.Info
34+
{ metricName = "git_revision"
35+
, metricHelp = "Git revision"
36+
}
2737
packageImportCounter <- P.register packageImportCount
28-
pure $ AppMetrics packageImportCounter
38+
gitHashText <- P.register gitHashMetric
39+
pure $ AppMetrics packageImportCounter gitHashText
40+
41+
setGitHash :: Metrics AppMetrics :> es
42+
=> Eff es ()
43+
setGitHash =
44+
setLabelledGauge gitRevision $(gitHash) 1.0
2945

3046
increaseCounterBy
3147
:: Metrics AppMetrics :> es

src/web/FloraWeb/Server.hs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ import System.Info qualified as System
7070

7171
import Flora.Environment (getFloraEnv)
7272
import Flora.Environment.Config (DeploymentEnv (..))
73+
import Flora.Monitoring (setGitHash)
7374
import Flora.Environment.Env
7475
( BlobStoreImpl (..)
7576
, FeatureEnv (..)
@@ -125,11 +126,13 @@ runFlora = do
125126
liftIO $ do
126127
forM_ env.mltp.eventlogSocket Socket.start
127128
when (isJust env.mltp.eventlogSocket) (blueMessage "🔥 Sending live events to socket")
128-
liftIO $ when env.mltp.prometheusEnabled $ do
129-
blueMessage $ "🔥 Exposing Prometheus metrics at " <> baseURL <> "/metrics"
130-
void $ P.register P.ghcMetrics
131-
when (System.os == "linux") $ void $ P.register P.procMetrics
132-
void $ P.register (P.counter (P.Info "flora_imported_packages_total" "The number of imported packages"))
129+
when env.mltp.prometheusEnabled $ do
130+
liftIO $ blueMessage $ "🔥 Exposing Prometheus metrics at " <> baseURL <> "/metrics"
131+
runPrometheusMetrics env.metrics $ do
132+
void $ P.register P.ghcMetrics
133+
when (System.os == "linux") $ void $ P.register P.procMetrics
134+
setGitHash
135+
133136
liftIO $ when env.mltp.zipkinEnabled (blueMessage "🖊️ Connecting to Zipkin endpoint")
134137
liftIO $ when (env.environment == Development) (blueMessage "🔁 Live reloading enabled")
135138
let withLogger = Logging.makeLogger env.mltp.logger

0 commit comments

Comments
 (0)