-
Notifications
You must be signed in to change notification settings - Fork 740
Port cardano-submit-api
to trace-dispatcher
#6326
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Russoul
wants to merge
5
commits into
master
Choose a base branch
from
russoul/cardano-submit-api-trace-dispatcher
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
67b2e87
cardano-submit-api: Port existing traces and metrics to `trace-dispat…
Russoul aa50d6e
cardano-submit-api: Eliminate the legacy tracing system
Russoul c1add54
cardano-submit-api: Tidy up the imports
Russoul 90739e7
nix: bump iohkNix input and adjust edge node nixos test
Russoul de5b3d4
cardano-submit-api: Update CHANGELOG and cabal file
Russoul File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,39 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
{-# LANGUAGE TypeApplications #-} | ||
|
||
module Cardano.TxSubmit.Metrics | ||
( TxSubmitMetrics (..) | ||
, makeMetrics | ||
, registerMetricsServer | ||
) | ||
(registerMetricsServer) | ||
where | ||
|
||
import Cardano.Api.Pretty (textShow) | ||
|
||
import Cardano.BM.Data.Trace (Trace) | ||
import Cardano.BM.Trace (logError, logInfo, logWarning) | ||
import Cardano.Logging (Trace, traceWith) | ||
import Cardano.TxSubmit.Tracing.TraceSubmitApi (TraceSubmitApi (..)) | ||
|
||
import Control.Exception.Safe | ||
import Control.Monad.Reader (MonadReader (ask), ReaderT (runReaderT)) | ||
import Data.Text (Text) | ||
import qualified Data.Text as T | ||
import System.Metrics.Prometheus.Concurrent.RegistryT (RegistryT (..), registerGauge, | ||
runRegistryT, unRegistryT) | ||
import System.Metrics.Prometheus.Http.Scrape (serveMetricsT) | ||
import System.Metrics.Prometheus.Metric.Gauge (Gauge) | ||
|
||
data TxSubmitMetrics = TxSubmitMetrics | ||
{ tsmCount :: Gauge | ||
, tsmFailCount :: Gauge | ||
} | ||
import System.Metrics.Prometheus.Http.Scrape (serveMetrics) | ||
import System.Metrics.Prometheus.Registry (RegistrySample) | ||
|
||
-- | Register metrics server. Returns metrics and an IO action which starts metrics server and should | ||
-- be passed to 'withAsync'. | ||
registerMetricsServer | ||
:: Trace IO Text | ||
:: Trace IO TraceSubmitApi | ||
-> IO RegistrySample | ||
-> Int | ||
-> IO (TxSubmitMetrics, IO ()) | ||
registerMetricsServer tracer metricsPort = | ||
runRegistryT $ do | ||
metrics <- makeMetrics | ||
registry <- RegistryT ask | ||
let runServer = | ||
tryWithPort metricsPort $ \port -> do | ||
logInfo tracer $ "Starting metrics server on port " <> textShow port | ||
flip runReaderT registry . unRegistryT $ serveMetricsT port [] | ||
pure (metrics, runServer) | ||
-> IO () | ||
registerMetricsServer tracer registrySample metricsPort = do | ||
tryWithPort metricsPort $ \port -> do | ||
traceWith tracer $ MetricsServerStarted port | ||
serveMetrics port [] registrySample | ||
where | ||
|
||
-- try opening the metrics server on the specified port, if it fails, try using next. Gives up after 1000 attempts and disables metrics server. | ||
tryWithPort :: Int -> (Int -> IO ()) -> IO () | ||
tryWithPort startingPort f = go startingPort | ||
where | ||
go port = do | ||
catch @_ @IOException (f port) $ \e -> do | ||
logWarning tracer $ T.pack $ "Metrics server error: " <> displayException e | ||
traceWith tracer $ MetricsServerError e | ||
if port <= (startingPort + 1000) | ||
then do | ||
logWarning tracer $ "Could not allocate metrics server port " <> textShow port <> " - trying next available..." | ||
traceWith tracer $ MetricsServerPortOccupied port | ||
go $ port + 1 | ||
else do | ||
logError tracer $ | ||
"Could not allocate any metrics port until " <> textShow port <> " - metrics endpoint disabled" | ||
pure () | ||
|
||
makeMetrics :: RegistryT IO TxSubmitMetrics | ||
makeMetrics = | ||
TxSubmitMetrics | ||
<$> registerGauge "tx_submit_count" mempty | ||
<*> registerGauge "tx_submit_fail_count" mempty | ||
else | ||
traceWith tracer $ MetricsServerPortNotBound port |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,29 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
|
||
module Cardano.TxSubmit.Rest.Web | ||
( runSettings | ||
) | ||
where | ||
|
||
import Cardano.Api.Pretty (textShow) | ||
|
||
import Cardano.BM.Trace (Trace, logInfo) | ||
import Cardano.Logging.Trace (traceWith) | ||
import qualified Cardano.Logging.Types as TraceD | ||
import Cardano.TxSubmit.Tracing.TraceSubmitApi (TraceSubmitApi (..)) | ||
|
||
import Control.Exception (bracket) | ||
import Data.Streaming.Network (bindPortTCP) | ||
import Data.Text (Text) | ||
import Network.Socket (close, getSocketName, withSocketsDo) | ||
import Network.Wai.Handler.Warp (Settings, getHost, getPort, runSettingsSocket) | ||
|
||
import Servant (Application) | ||
|
||
-- | Like 'Network.Wai.Handler.Warp.runSettings', except with better logging. | ||
runSettings :: Trace IO Text -> Settings -> Application -> IO () | ||
runSettings :: TraceD.Trace IO TraceSubmitApi -> Settings -> Application -> IO () | ||
runSettings trace settings app = | ||
withSocketsDo $ | ||
bracket | ||
(bindPortTCP (getPort settings) (getHost settings)) | ||
close | ||
( \socket -> do | ||
addr <- getSocketName socket | ||
logInfo trace $ "Web API listening on port " <> textShow addr | ||
traceWith trace $ EndpointListeningOnPort addr | ||
runSettingsSocket settings socket app | ||
) |
42 changes: 0 additions & 42 deletions
42
cardano-submit-api/src/Cardano/TxSubmit/Tracing/ToObjectOrphans.hs
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.