http-env-0.0.0.0: src/Network/HTTP/Env.hs
-- | Call each HTTP dependency through an env that holds its retries, circuit
-- breaker, failure budget, rate limiting and log policy.
--
-- Start here. For servant-client, start from "Network.HTTP.Env.Servant".
--
-- = Usage
--
-- Make one env per dependency, and make every call to it through that env:
--
-- > import Control.Monad.IO.Class (liftIO)
-- > import Control.Monad.Logger (runStderrLoggingT)
-- > import Network.HTTP.Client (newManager, parseUrlThrow)
-- > import Network.HTTP.Env
-- >
-- > main :: IO ()
-- > main = runStderrLoggingT $ do
-- > manager <- liftIO $ newManager (withUserAgent "my-app" (tlsManagerSettingsWithMaxHeaderLength 65536))
-- > env <- newHttpEnv (HttpEnvName "example") defaultHttpEnvSettings defaultLogPolicy manager
-- > request <- liftIO $ parseUrlThrow "https://example.com/api"
-- > result <- httpCall env request
-- > case result of
-- > Right response -> ...
-- > Left HttpEnvErrorCircuitOpen -> ...
-- > Left (HttpEnvErrorRateLimited wait _) -> ...
-- > Left (HttpEnvErrorFailed exception) -> ...
--
-- A request built with 'Network.HTTP.Client.parseUrlThrow' fails on a refusal;
-- one built with 'Network.HTTP.Client.parseRequest' returns the refusal as the
-- response, except for a spent quota, which is told apart either way.
--
-- = Settings
--
-- Parse one global t'HttpEnvSettings' with opt-env-conf, with its defaults,
-- and one t'HttpEnvOverrides' per dependency with
-- 'httpEnvOverridesParser', defaulting to what the application chooses for that
-- dependency. 'resolveHttpEnvSettings' combines the two:
--
-- > data Settings = Settings {settingsHttp :: HttpEnvSettings, settingsGitHubHttp :: HttpEnvOverrides}
-- >
-- > instance HasParser Settings where
-- > settingsParser =
-- > Settings
-- > <$> subSettings "http"
-- > <*> subAll "github" (subAll "http" (httpEnvOverridesParser noHttpEnvOverrides))
-- >
-- > gitHubSettings :: Settings -> HttpEnvSettings
-- > gitHubSettings settings = resolveHttpEnvSettings (settingsHttp settings) (settingsGitHubHttp settings)
--
-- = Monitoring
--
-- Ask an env what its breaker and its failure budget say, whenever something
-- needs to know; asking does not count as a call.
-- For a notice shown to people, read the two apart:
--
-- > report <- peekHttpEnv env
-- > let down = httpEnvReportCircuit report == Just CircuitReportRejecting
-- > let unwell = fmap failureBudgetReportStatus (httpEnvReportFailureBudget report) == Just FailureBudgetStatusExhausted
--
-- For Prometheus, sample every env when it is scraped, each under labels of
-- its own. 'httpEnvReportValue' reads 1 when all is well, 0.75 while the budget
-- is exhausted, 0.5 while the breaker is probing and 0 while it rejects:
--
-- > import qualified Data.Map.Strict as Map
-- > import qualified System.Metrics.Prometheus.MetricId as Labels
-- > import System.Metrics.Prometheus.Registry (RegistrySample (..))
-- >
-- > sampleEnvs :: [(Text, HttpEnv)] -> IO RegistrySample
-- > sampleEnvs envs = do
-- > samples <- for envs $ \(dependency, env) ->
-- > httpEnvSample "http_env_health" (Labels.fromList [("dependency", dependency)]) <$> peekHttpEnv env
-- > pure (RegistrySample (Map.fromList samples))
module Network.HTTP.Env
( -- * Settings
HttpEnvSettings (..),
defaultHttpEnvSettings,
Attempts (..),
BackoffSettings (..),
defaultBackoffSettings,
BackoffOverrides (..),
CircuitBreakerSettings (..),
FailureBudgetSettings (..),
HttpEnvOverrides (..),
CircuitBreakerOverrides (..),
FailureBudgetOverrides (..),
noHttpEnvOverrides,
httpEnvOverridesParser,
resolveHttpEnvSettings,
RateLimitHeaders (..),
-- * Making an env
-- | One per dependency, each with a breaker and a budget of its own.
newHttpEnv,
HttpEnvName (..),
HttpEnv,
withRequestLimiter,
RequestLimiter,
newRequestLimiter,
tlsManagerSettingsWithMaxHeaderLength,
withUserAgent,
LogPolicy (..),
defaultLogPolicy,
-- * Calling
-- | A spent quota is told apart only for an env whose settings choose
-- t'RateLimitHeaders'.
httpCall,
httpCallWith,
Repeatable (..),
HttpEnvError (..),
httpEnvErrorCircuitBreakerError,
CircuitBreakerError (..),
-- * Inspecting
-- | What an env's breaker and budget say, for a dashboard or a person.
-- Reading does not count as a call.
peekHttpEnv,
HttpEnvReport (..),
CircuitReport (..),
FailureBudgetReport (..),
FailureBudgetStatus (..),
httpEnvSample,
httpEnvReportValue,
)
where
import Network.HTTP.Env.CircuitBreaker (CircuitBreakerError (..), CircuitBreakerSettings (..), CircuitReport (..))
import Network.HTTP.Env.CircuitBreaker.Retry (Attempts (..))
import Network.HTTP.Env.FailureBudget (FailureBudgetReport (..), FailureBudgetSettings (..), FailureBudgetStatus (..))
import Network.HTTP.Env.HttpClient (HttpEnv, HttpEnvError (..), HttpEnvReport (..), httpCall, httpCallWith, httpEnvErrorCircuitBreakerError, peekHttpEnv, withRequestLimiter)
import Network.HTTP.Env.HttpClient.Retry (Repeatable (..))
import Network.HTTP.Env.LogPolicy (LogPolicy (..), defaultLogPolicy)
import Network.HTTP.Env.Manager (tlsManagerSettingsWithMaxHeaderLength, withUserAgent)
import Network.HTTP.Env.Metrics (httpEnvReportValue, httpEnvSample)
import Network.HTTP.Env.OptParse
( BackoffOverrides (..),
BackoffSettings (..),
CircuitBreakerOverrides (..),
FailureBudgetOverrides (..),
HttpEnvName (..),
HttpEnvOverrides (..),
HttpEnvSettings (..),
defaultBackoffSettings,
defaultHttpEnvSettings,
httpEnvOverridesParser,
newHttpEnv,
noHttpEnvOverrides,
resolveHttpEnvSettings,
)
import Network.HTTP.Env.Pacing (RequestLimiter, newRequestLimiter)
import Network.HTTP.Env.RateLimit (RateLimitHeaders (..))