Skip to content

Commit d69b889

Browse files
committed
add offchain user header to configuration
1 parent 0dab850 commit d69b889

File tree

4 files changed

+61
-13
lines changed

4 files changed

+61
-13
lines changed

cardano-db-sync/src/Cardano/DbSync/Config/Types.hs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ module Cardano.DbSync.Config.Types (
3838
PlutusConfig (..),
3939
GovernanceConfig (..),
4040
OffchainPoolDataConfig (..),
41+
OffChainUserAgent (..),
4142
JsonTypeConfig (..),
4243
LedgerStateDir (..),
4344
LogFileDir (..),
@@ -189,6 +190,7 @@ data SyncInsertOptions = SyncInsertOptions
189190
, sioPoolStats :: PoolStatsConfig
190191
, sioJsonType :: JsonTypeConfig
191192
, sioRemoveJsonbFromSchema :: RemoveJsonbFromSchemaConfig
193+
, sioOffchainUserAgent :: OffChainUserAgent
192194
, sioStopAtBlock :: Maybe Word64
193195
}
194196
deriving (Eq, Show)
@@ -263,6 +265,12 @@ newtype OffchainPoolDataConfig = OffchainPoolDataConfig
263265
}
264266
deriving (Eq, Show)
265267

268+
newtype OffChainUserAgent = OffChainUserAgent
269+
{ unOffChainUserAgent :: Maybe Text
270+
}
271+
deriving (Eq, Show)
272+
deriving newtype (ToJSON, FromJSON)
273+
266274
newtype RemoveJsonbFromSchemaConfig = RemoveJsonbFromSchemaConfig
267275
{ isRemoveJsonbFromSchemaEnabled :: Bool
268276
}
@@ -456,6 +464,7 @@ parseOverrides obj baseOptions = do
456464
<*> obj .:? "pool_stat" .!= sioPoolStats baseOptions
457465
<*> obj .:? "json_type" .!= sioJsonType baseOptions
458466
<*> obj .:? "remove_jsonb_from_schema" .!= sioRemoveJsonbFromSchema baseOptions
467+
<*> obj .:? "offchain_user_agent" .!= sioOffchainUserAgent baseOptions
459468
<*> obj .:? "stop_at_block" .!= sioStopAtBlock baseOptions
460469

461470
instance ToJSON SyncInsertConfig where
@@ -478,6 +487,7 @@ optionsToList SyncInsertOptions {..} =
478487
, toJsonIfSet "pool_stat" sioPoolStats
479488
, toJsonIfSet "json_type" sioJsonType
480489
, toJsonIfSet "remove_jsonb_from_schema" sioRemoveJsonbFromSchema
490+
, toJsonIfSet "offchain_user_agent" sioOffchainUserAgent
481491
, toJsonIfSet "stop_at_block" sioStopAtBlock
482492
]
483493

@@ -500,6 +510,7 @@ instance FromJSON SyncInsertOptions where
500510
<*> obj .:? "pool_stat" .!= sioPoolStats def
501511
<*> obj .:? "json_type" .!= sioJsonType def
502512
<*> obj .:? "remove_jsonb_from_schema" .!= sioRemoveJsonbFromSchema def
513+
<*> obj .:? "offchain_user_agent" .!= sioOffchainUserAgent def
503514
<*> obj .:? "stop_at_block" .!= sioStopAtBlock def
504515

505516
instance ToJSON SyncInsertOptions where
@@ -517,6 +528,7 @@ instance ToJSON SyncInsertOptions where
517528
, "pool_stat" .= sioPoolStats
518529
, "json_type" .= sioJsonType
519530
, "remove_jsonb_from_schema" .= sioRemoveJsonbFromSchema
531+
, "offchain_user_agent" .= sioOffchainUserAgent
520532
, "stop_at_block" .= sioStopAtBlock
521533
]
522534

@@ -747,6 +759,7 @@ instance Default SyncInsertOptions where
747759
, sioPoolStats = PoolStatsConfig False
748760
, sioJsonType = JsonTypeText
749761
, sioRemoveJsonbFromSchema = RemoveJsonbFromSchemaConfig False
762+
, sioOffchainUserAgent = OffChainUserAgent Nothing
750763
, sioStopAtBlock = Nothing
751764
}
752765

@@ -766,6 +779,7 @@ fullInsertOptions =
766779
, sioPoolStats = PoolStatsConfig True
767780
, sioJsonType = JsonTypeText
768781
, sioRemoveJsonbFromSchema = RemoveJsonbFromSchemaConfig False
782+
, sioOffchainUserAgent = OffChainUserAgent Nothing
769783
, sioStopAtBlock = Nothing
770784
}
771785

@@ -785,6 +799,7 @@ onlyUTxOInsertOptions =
785799
, sioPoolStats = PoolStatsConfig False
786800
, sioJsonType = JsonTypeText
787801
, sioRemoveJsonbFromSchema = RemoveJsonbFromSchemaConfig False
802+
, sioOffchainUserAgent = OffChainUserAgent Nothing
788803
, sioStopAtBlock = Nothing
789804
}
790805

@@ -812,6 +827,7 @@ disableAllInsertOptions =
812827
, sioGovernance = GovernanceConfig False
813828
, sioJsonType = JsonTypeText
814829
, sioRemoveJsonbFromSchema = RemoveJsonbFromSchemaConfig False
830+
, sioOffchainUserAgent = OffChainUserAgent Nothing
815831
, sioStopAtBlock = Nothing
816832
}
817833

cardano-db-sync/src/Cardano/DbSync/OffChain/Http.hs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import qualified Cardano.Crypto.Hash.Blake2b as Crypto
1313
import qualified Cardano.Crypto.Hash.Class as Crypto
1414
import Cardano.Db (PoolMetaHash (..), PoolUrl (..), VoteMetaHash (..), VoteUrl (..))
1515
import qualified Cardano.Db as DB
16+
import Cardano.DbSync.Config.Types (OffChainUserAgent (..))
1617
import Cardano.DbSync.OffChain.Types (
1718
PoolOffChainMetadata (..),
1819
PoolTicker (..),
@@ -46,11 +47,12 @@ import qualified Network.HTTP.Types as Http
4647
-------------------------------------------------------------------------------------
4748
httpGetOffChainPoolData ::
4849
Http.Manager ->
49-
Http.Request ->
5050
PoolUrl ->
51+
OffChainUserAgent ->
5152
Maybe PoolMetaHash ->
5253
ExceptT OffChainFetchError IO SimplifiedOffChainPoolData
53-
httpGetOffChainPoolData manager request purl expectedMetaHash = do
54+
httpGetOffChainPoolData manager purl userAgent expectedMetaHash = do
55+
request <- parseOffChainUrl url userAgent
5456
httpRes <- handleExceptT (convertHttpException url) req
5557
(respBS, respLBS, mContentType) <- hoistEither httpRes
5658
let metadataHash = Crypto.digest (Proxy :: Proxy Crypto.Blake2b_256) respBS
@@ -81,30 +83,32 @@ httpGetOffChainPoolData manager request purl expectedMetaHash = do
8183
httpGetOffChainVoteData ::
8284
[Text] ->
8385
VoteUrl ->
86+
OffChainUserAgent ->
8487
Maybe VoteMetaHash ->
8588
DB.AnchorType ->
8689
ExceptT OffChainFetchError IO SimplifiedOffChainVoteData
87-
httpGetOffChainVoteData gateways vurl metaHash anchorType = do
90+
httpGetOffChainVoteData gateways vurl userAgent metaHash anchorType = do
8891
case useIpfsGatewayMaybe vurl gateways of
89-
Nothing -> httpGetOffChainVoteDataSingle vurl metaHash anchorType
92+
Nothing -> httpGetOffChainVoteDataSingle vurl userAgent metaHash anchorType
9093
Just [] -> left $ OCFErrNoIpfsGateway (OffChainVoteUrl vurl)
9194
Just urls -> tryAllGatewaysRec urls []
9295
where
9396
tryAllGatewaysRec [] acc = left $ OCFErrIpfsGatewayFailures (OffChainVoteUrl vurl) (reverse acc)
9497
tryAllGatewaysRec (url : rest) acc = do
95-
msocd <- liftIO $ runExceptT $ httpGetOffChainVoteDataSingle url metaHash anchorType
98+
msocd <- liftIO $ runExceptT $ httpGetOffChainVoteDataSingle url userAgent metaHash anchorType
9699
case msocd of
97100
Right socd -> pure socd
98101
Left err -> tryAllGatewaysRec rest (err : acc)
99102

100103
httpGetOffChainVoteDataSingle ::
101104
VoteUrl ->
105+
OffChainUserAgent ->
102106
Maybe VoteMetaHash ->
103107
DB.AnchorType ->
104108
ExceptT OffChainFetchError IO SimplifiedOffChainVoteData
105-
httpGetOffChainVoteDataSingle vurl metaHash anchorType = do
109+
httpGetOffChainVoteDataSingle vurl userAgent metaHash anchorType = do
106110
manager <- liftIO $ Http.newManager tlsManagerSettings
107-
request <- parseOffChainUrl url
111+
request <- parseOffChainUrl url userAgent
108112
let req = httpGetBytes manager request 3000000 3000000 url
109113
httpRes <- handleExceptT (convertHttpException url) req
110114
(respBS, respLBS, mContentType) <- hoistEither httpRes
@@ -205,22 +209,24 @@ isPossiblyJsonObject bs =
205209
-------------------------------------------------------------------------------------
206210
-- Url
207211
-------------------------------------------------------------------------------------
208-
parseOffChainUrl :: OffChainUrlType -> ExceptT OffChainFetchError IO Http.Request
209-
parseOffChainUrl url =
210-
handleExceptT wrapHttpException $ applyHeaders <$> Http.parseRequest (showUrl url)
212+
parseOffChainUrl :: OffChainUrlType -> OffChainUserAgent -> ExceptT OffChainFetchError IO Http.Request
213+
parseOffChainUrl url userAgent =
214+
handleExceptT wrapHttpException $ applyHeaders userAgent <$> Http.parseRequest (showUrl url)
211215
where
212216
wrapHttpException :: HttpException -> OffChainFetchError
213217
wrapHttpException err = OCFErrHttpException url (textShow err)
214218

215-
applyHeaders :: Http.Request -> Http.Request
216-
applyHeaders req =
219+
applyHeaders :: OffChainUserAgent -> Http.Request -> Http.Request
220+
applyHeaders (OffChainUserAgent mUserAgent) req =
217221
req
218222
{ Http.requestHeaders =
219223
Http.requestHeaders req
220224
++ [ (CI.mk "content-type", "application/json")
221-
, (CI.mk "user-agent", "cardano-db-sync")
225+
, (CI.mk "user-agent", Text.encodeUtf8 userAgent)
222226
]
223227
}
228+
where
229+
userAgent = fromMaybe "cardano-db-sync" mUserAgent
224230

225231
-------------------------------------------------------------------------------------
226232
-- Exceptions to Error

cardano-db-sync/test/Cardano/DbSync/Config/TypesTest.hs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,18 @@ genDefaultJson =
140140
},
141141
"governance": "enable",
142142
"offchain_pool_data": "enable",
143+
"offchain_user_agent": null,
143144
"json_type": "text"
144145
}
145146
|]
146147
, [aesonQQ|
147148
{ }
148149
|]
150+
, [aesonQQ|
151+
{
152+
"offchain_user_agent": "test-agent-v1.0"
153+
}
154+
|]
149155
, [aesonQQ|
150156
{
151157
"tx_out": {

doc/configuration.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ Below is a sample `insert_options` section that shows all the defaults:
6363
| [plutus](#plutus) | `object` | Optional |
6464
| [governance](#governance) | `enum` | Optional |
6565
| [offchain\_pool\_data](#offchain-pool-data) | `enum` | Optional |
66+
| [offchain\_user\_agent](#offchain-user-agent) | `string` | Optional |
6667
| [pool\_stat](#pool-stat) | `enum` | Optional |
6768
| [remove\_jsonb_from_schema](#remove-jsonb-from-schema) | `enum` | Optional |
6869
| [stop\_at\_block](#stop-at-block) | `integer` | Optional |
@@ -534,6 +535,25 @@ This will effect all governance related data/functionality.
534535
| `"enable"` | Enables fetching offchain metadata. |
535536
| `"disable"`| Disables fetching pool offchain metadata. |
536537

538+
## Offchain User Agent
539+
540+
`offchain_user_agent`
541+
542+
* Type: `string`
543+
* Optional: When not specified, defaults to "cardano-db-sync"
544+
545+
Configures the User-Agent header sent when fetching offchain metadata (pool metadata, governance vote data, etc.). This allows operators to identify their db-sync instance in server logs.
546+
547+
### Example
548+
549+
```json
550+
{
551+
"insert_options": {
552+
"offchain_user_agent": "my-cardano-node-v1.0"
553+
}
554+
}
555+
```
556+
537557
## Pool Stat
538558

539559
`pool_stat`

0 commit comments

Comments
 (0)