-
Notifications
You must be signed in to change notification settings - Fork 325
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
a56a18a
commit e859176
Showing
22 changed files
with
290 additions
and
71 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
- Extending the information returned in errors for Federator. Paths and response bodies, if available, are included in error logs. | ||
- Prometheus metrics for outgoing and incoming federation requests added. |
This file contains 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 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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
module API.Federator where | ||
|
||
import Data.Function | ||
import GHC.Stack | ||
import Network.HTTP.Client qualified as HTTP | ||
import Testlib.Prelude | ||
|
||
getMetrics :: | ||
(HasCallStack, MakesValue domain) => | ||
domain -> | ||
(ServiceMap -> HostPort) -> | ||
App Response | ||
getMetrics domain service = do | ||
req <- rawBaseRequestF domain service "i/metrics" | ||
submit "GET" req | ||
|
||
rawBaseRequestF :: (HasCallStack, MakesValue domain) => domain -> (ServiceMap -> HostPort) -> String -> App HTTP.Request | ||
rawBaseRequestF domain getService path = do | ||
domainV <- objDomain domain | ||
serviceMap <- getServiceMap domainV | ||
|
||
liftIO . HTTP.parseRequest $ | ||
let HostPort h p = getService serviceMap | ||
in "http://" <> h <> ":" <> show p <> ("/" <> joinHttpPath (splitHttpPath path)) |
This file contains 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,36 +1,61 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
|
||
module Test.Federator where | ||
|
||
import API.Brig | ||
import API.Federator (getMetrics) | ||
import Data.Attoparsec.Text | ||
import Data.ByteString qualified as BS | ||
import Data.String.Conversions | ||
import Network.HTTP.Client qualified as HTTP | ||
import Data.Text | ||
import SetupHelpers (randomUser) | ||
import Testlib.Prelude | ||
|
||
runFederatorMetrics :: (ServiceMap -> HostPort) -> App () | ||
runFederatorMetrics getService = do | ||
let req = submit "GET" =<< rawBaseRequestF OwnDomain getService "/i/metrics" | ||
handleRes res = res <$ res.status `shouldMatchInt` 200 | ||
first <- bindResponse req handleRes | ||
second <- bindResponse req handleRes | ||
let handleRes res = res <$ res.status `shouldMatchInt` 200 | ||
first <- bindResponse (getMetrics OwnDomain getService) handleRes | ||
second <- bindResponse (getMetrics OwnDomain getService) handleRes | ||
assertBool "Two metric requests should never match" $ first.body /= second.body | ||
assertBool "Second metric response should never be 0 length (the first might be)" $ BS.length second.body /= 0 | ||
assertBool "The seconds metric response should have text indicating that it is returning metrics" $ | ||
BS.isInfixOf (cs expectedString) second.body | ||
BS.isInfixOf expectedString second.body | ||
where | ||
expectedString = "# TYPE http_request_duration_seconds histogram" | ||
|
||
rawBaseRequestF :: (HasCallStack, MakesValue domain) => domain -> (ServiceMap -> HostPort) -> String -> App HTTP.Request | ||
rawBaseRequestF domain getService path = do | ||
domainV <- objDomain domain | ||
serviceMap <- getServiceMap domainV | ||
|
||
liftIO . HTTP.parseRequest $ | ||
let HostPort h p = getService serviceMap | ||
in "http://" <> h <> ":" <> show p <> ("/" <> joinHttpPath (splitHttpPath path)) | ||
|
||
-- The metrics setup for both internal and external federator servers | ||
-- are the same, so we can simply run the same test for both. | ||
testFederatorMetricsInternal :: App () | ||
testFederatorMetricsInternal = runFederatorMetrics federatorInternal | ||
|
||
testFederatorMetricsExternal :: App () | ||
testFederatorMetricsExternal = runFederatorMetrics federatorExternal | ||
|
||
testFederatorNumRequestsMetrics :: HasCallStack => App () | ||
testFederatorNumRequestsMetrics = do | ||
u1 <- randomUser OwnDomain def | ||
u2 <- randomUser OtherDomain def | ||
incomingBefore <- getMetric parseIncomingRequestCount OtherDomain OwnDomain | ||
outgoingBefore <- getMetric parseOutgoingRequestCount OwnDomain OtherDomain | ||
bindResponse (searchContacts u1 (u2 %. "name") OtherDomain) $ \resp -> | ||
resp.status `shouldMatchInt` 200 | ||
incomingAfter <- getMetric parseIncomingRequestCount OtherDomain OwnDomain | ||
outgoingAfter <- getMetric parseOutgoingRequestCount OwnDomain OtherDomain | ||
assertBool "Incoming requests count should have increased by at least 2" $ incomingAfter >= incomingBefore + 2 | ||
assertBool "Outgoing requests count should have increased by at least 2" $ outgoingAfter >= outgoingBefore + 2 | ||
where | ||
getMetric :: (Text -> Parser Integer) -> Domain -> Domain -> App Integer | ||
getMetric p domain origin = do | ||
m <- getMetrics domain federatorInternal | ||
d <- cs <$> asString origin | ||
pure $ fromRight 0 (parseOnly (p d) (cs m.body)) | ||
|
||
parseIncomingRequestCount :: Text -> Parser Integer | ||
parseIncomingRequestCount d = | ||
manyTill anyChar (string ("com_wire_federator_incoming_requests{origin_domain=\"" <> d <> "\"} ")) | ||
*> decimal | ||
|
||
parseOutgoingRequestCount :: Text -> Parser Integer | ||
parseOutgoingRequestCount d = | ||
manyTill anyChar (string ("com_wire_federator_outgoing_requests{target_domain=\"" <> d <> "\"} ")) | ||
*> decimal |
This file contains 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 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 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 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 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 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 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 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
Oops, something went wrong.