Skip to content

Commit

Permalink
Merge pull request #460 from opexdev/dev
Browse files Browse the repository at this point in the history
Add logbook for better http logs
  • Loading branch information
Marchosiax authored Jul 23, 2024
2 parents 9cb3567 + 66cbc83 commit c3df3ac
Show file tree
Hide file tree
Showing 48 changed files with 753 additions and 537 deletions.
4 changes: 0 additions & 4 deletions accountant/accountant-app/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,6 @@
<groupId>co.nilin.opex.utility</groupId>
<artifactId>error-handler</artifactId>
</dependency>
<dependency>
<groupId>co.nilin.opex.utility</groupId>
<artifactId>logging-handler</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-vault-config</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ import java.math.BigDecimal
@RestController
class AccountantController(
val walletProxy: WalletProxy,
val financialActionLoader: FinancialActionLoader,
val pairConfigLoader: PairConfigLoader
val financialActionLoader: FinancialActionLoader
) {

private val logger = LoggerFactory.getLogger(AccountantController::class.java)
Expand All @@ -43,42 +42,5 @@ class AccountantController(
BooleanResponse(false)
}

@GetMapping("/config/{pair}/fee/{direction}-{userLevel}")
suspend fun fetchPairFeeConfig(
@PathVariable("pair") pair: String,
@PathVariable("direction") direction: OrderDirection,
@PathVariable("userLevel") level: String
): PairFeeConfig {
return pairConfigLoader.load(pair, direction, level)
}

@GetMapping("/config/{pair}/{direction}")
suspend fun fetchPairConfig(
@PathVariable("pair") pair: String,
@PathVariable("direction") direction: OrderDirection
): PairConfig {
return pairConfigLoader.load(pair, direction)
}

@GetMapping("/config/all")
suspend fun fetchPairConfigs(): List<PairConfig> {
return pairConfigLoader.loadPairConfigs()
}

@GetMapping("/config/fee")
suspend fun getFeeConfigs(): List<PairFeeResponse> {
return pairConfigLoader.loadPairFeeConfigs()
.map { PairFeeResponse(it.pairConfig.pair, it.direction, it.userLevel, it.makerFee, it.takerFee) }
}

@GetMapping("/config/fee/{pair}")
suspend fun getFeeConfig(
@PathVariable pair: String,
@RequestParam(required = false) direction: OrderDirection?,
@RequestParam(required = false) userLevel: String?
): PairFeeResponse {
val fee = pairConfigLoader.loadPairFeeConfigs(pair, direction ?: OrderDirection.BID, userLevel ?: "*")
?: throw OpexError.PairFeeNotFound.exception()
return PairFeeResponse(fee.pairConfig.pair, fee.direction, fee.userLevel, fee.makerFee, fee.takerFee)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package co.nilin.opex.accountant.app.controller

import co.nilin.opex.accountant.app.data.PairFeeResponse
import co.nilin.opex.accountant.core.model.PairConfig
import co.nilin.opex.accountant.core.model.PairFeeConfig
import co.nilin.opex.accountant.core.spi.PairConfigLoader
import co.nilin.opex.common.OpexError
import co.nilin.opex.matching.engine.core.model.OrderDirection
import org.springframework.web.bind.annotation.*

@RestController
@RequestMapping("/config")
class PairConfigController(private val pairConfigLoader: PairConfigLoader) {

@GetMapping("/{pair}/fee/{direction}-{userLevel}")
suspend fun fetchPairFeeConfig(
@PathVariable("pair") pair: String,
@PathVariable("direction") direction: OrderDirection,
@PathVariable("userLevel") level: String
): PairFeeConfig {
return pairConfigLoader.load(pair, direction, level)
}

@GetMapping("/{pair}/{direction}")
suspend fun fetchPairConfig(
@PathVariable("pair") pair: String,
@PathVariable("direction") direction: OrderDirection
): PairConfig {
return pairConfigLoader.load(pair, direction)
}

@GetMapping("/all")
suspend fun fetchPairConfigs(): List<PairConfig> {
return pairConfigLoader.loadPairConfigs()
}

@GetMapping("/fee")
suspend fun getFeeConfigs(): List<PairFeeResponse> {
return pairConfigLoader.loadPairFeeConfigs()
.map { PairFeeResponse(it.pairConfig.pair, it.direction, it.userLevel, it.makerFee, it.takerFee) }
}

@GetMapping("/fee/{pair}")
suspend fun getFeeConfig(
@PathVariable pair: String,
@RequestParam(required = false) direction: OrderDirection?,
@RequestParam(required = false) userLevel: String?
): PairFeeResponse {
val fee = pairConfigLoader.loadPairFeeConfigs(pair, direction ?: OrderDirection.BID, userLevel ?: "*")
?: throw OpexError.PairFeeNotFound.exception()
return PairFeeResponse(fee.pairConfig.pair, fee.direction, fee.userLevel, fee.makerFee, fee.takerFee)
}
}
35 changes: 30 additions & 5 deletions accountant/accountant-app/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
server.port: 8080
logging:
level:
co.nilin: INFO
reactor.netty.http.client: INFO
server:
port: 8080
spring:
application:
name: opex-accountant
Expand Down Expand Up @@ -67,6 +64,34 @@ management:
enabled: true
prometheus:
enabled: true
logbook:
secure-filter:
enabled: true
format:
style: http
filter:
enabled: true
form-request-mode: BODY
obfuscate:
headers:
- Authorization
parameters:
- password
json-body-fields:
- password
replacement: "***"
write:
max-body-size: 5_000 #kb
predicate:
exclude:
- path: /auth**
- path: /actuator/**
- path: /swagger**
- path: /config/**
logging:
level:
co.nilin: INFO
org.zalando.logbook: TRACE
app:
address: 1
wallet:
Expand Down
4 changes: 0 additions & 4 deletions accountant/accountant-ports/accountant-wallet-proxy/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@
<groupId>co.nilin.opex.accountant.core</groupId>
<artifactId>accountant-core</artifactId>
</dependency>
<dependency>
<groupId>co.nilin.opex.utility</groupId>
<artifactId>logging-handler</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,37 +1,25 @@
package co.nilin.opex.accountant.ports.walletproxy.config

import co.nilin.opex.utility.log.CustomLogger
import org.springframework.cloud.client.ServiceInstance
import org.springframework.cloud.client.loadbalancer.LoadBalancerProperties
import org.springframework.cloud.client.loadbalancer.reactive.ReactiveLoadBalancer
import org.springframework.cloud.client.loadbalancer.reactive.ReactorLoadBalancerExchangeFilterFunction
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.http.client.reactive.ReactorClientHttpConnector
import org.springframework.web.reactive.function.client.WebClient
import org.zalando.logbook.Logbook
import org.zalando.logbook.netty.LogbookClientHandler
import reactor.netty.http.client.HttpClient

@Configuration
class WebClientConfig {

@Bean
fun webClient(loadBalancerFactory: ReactiveLoadBalancer.Factory<ServiceInstance>): WebClient {
val logger = CustomLogger(HttpClient::class.java)
fun webClient(loadBalancerFactory: ReactiveLoadBalancer.Factory<ServiceInstance>, logbook: Logbook): WebClient {
val client = HttpClient.create().doOnConnected { it.addHandlerLast(LogbookClientHandler(logbook)) }
return WebClient.builder()
.clientConnector(
ReactorClientHttpConnector(
HttpClient
.create()
.doOnRequest { request, connection ->
connection.addHandlerFirst(logger)
}
)
)
.filter(
ReactorLoadBalancerExchangeFilterFunction(
loadBalancerFactory, LoadBalancerProperties(), emptyList()
)
)
//.clientConnector(ReactorClientHttpConnector(client))
.filter(ReactorLoadBalancerExchangeFilterFunction(loadBalancerFactory, emptyList()))
.build()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ import java.math.BigDecimal

@Component
class WalletProxyImpl(
@Value("\${app.wallet.url}") val walletBaseUrl: String,
val webClient: WebClient
private val webClient: WebClient,
@Value("\${app.wallet.url}")
private val walletBaseUrl: String
) : WalletProxy {

data class TransferBody(
Expand Down Expand Up @@ -63,7 +64,7 @@ class WalletProxyImpl(

override suspend fun canFulfil(symbol: String, walletType: String, uuid: String, amount: BigDecimal): Boolean {
return webClient.get()
.uri("$walletBaseUrl/$uuid/wallet_type/$walletType/can_withdraw/${amount}_$symbol")
.uri("$walletBaseUrl/inquiry/$uuid/wallet_type/$walletType/can_withdraw/${amount}_$symbol")
.header("Content-Type", "application/json")
.retrieve()
.onStatus({ t -> t.isError }, { it.createException() })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ class WalletProxyImplTest {

lateinit var mockServer: MockServerClient
val walletProxyImpl = WalletProxyImpl(
"http://localhost:8089", WebClient.builder().build()
WebClient.builder().build(),
"http://localhost:8089"
)
val objectMapper = ObjectMapper()

Expand Down Expand Up @@ -51,21 +52,49 @@ class WalletProxyImplTest {
mockServer.`when`(
request().withMethod("POST")
.withPath("/v2/transfer/${amount}_$symbol/from/${senderUuid}_$senderWalletType/to/${receiverUuid}_$receiverWalletType")
.withBody(objectMapper.writeValueAsString(WalletProxyImpl.TransferBody(description, transferRef, transferCategory, additionalData)))
.withBody(
objectMapper.writeValueAsString(
WalletProxyImpl.TransferBody(
description,
transferRef,
transferCategory,
additionalData
)
)
)
).respond(
response()
.withStatusCode(200)
.withContentType(MediaType.APPLICATION_JSON)
.withBody(
objectMapper.writeValueAsString(
TransferResult(
System.currentTimeMillis(), senderUuid, senderWalletType, amountObject, amountObject, amountObject, receiverUuid, receiverWalletType, amountObject
System.currentTimeMillis(),
senderUuid,
senderWalletType,
amountObject,
amountObject,
amountObject,
receiverUuid,
receiverWalletType,
amountObject
)
)
)
)
runBlocking {
walletProxyImpl.transfer(symbol, senderWalletType, senderUuid, receiverWalletType, receiverUuid, amount, description, transferRef, transferCategory, additionalData)
walletProxyImpl.transfer(
symbol,
senderWalletType,
senderUuid,
receiverWalletType,
receiverUuid,
amount,
description,
transferRef,
transferCategory,
additionalData
)
}
}

Expand Down
10 changes: 5 additions & 5 deletions accountant/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@
<groupId>co.nilin.opex</groupId>
<artifactId>common</artifactId>
</dependency>
<dependency>
<groupId>org.zalando</groupId>
<artifactId>logbook-spring-boot-webflux-autoconfigure</artifactId>
<version>3.9.0</version>
</dependency>
<dependency>
<groupId>io.mockk</groupId>
<artifactId>mockk</artifactId>
Expand Down Expand Up @@ -77,11 +82,6 @@
<artifactId>error-handler</artifactId>
<version>${error-hanlder.version}</version>
</dependency>
<dependency>
<groupId>co.nilin.opex.utility</groupId>
<artifactId>logging-handler</artifactId>
<version>${logging-handler.version}</version>
</dependency>
<dependency>
<groupId>co.nilin.opex.utility</groupId>
<artifactId>preferences</artifactId>
Expand Down
4 changes: 0 additions & 4 deletions api/api-app/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>co.nilin.opex.utility</groupId>
<artifactId>logging-handler</artifactId>
</dependency>
<dependency>
<groupId>co.nilin.opex.utility</groupId>
<artifactId>error-handler</artifactId>
Expand Down
Loading

0 comments on commit c3df3ac

Please sign in to comment.