Skip to content

Commit

Permalink
Merge pull request #82 from navikt/additional-metrics
Browse files Browse the repository at this point in the history
Additional metrics
  • Loading branch information
antonfofanov authored Jan 23, 2024
2 parents 9eb5031 + d092248 commit c3fded6
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 16 deletions.
96 changes: 96 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
root = true

[*]
ij_continuation_indent_size = 8
ij_formatter_off_tag = @formatter:off
ij_formatter_on_tag = @formatter:on
ij_formatter_tags_enabled = true
ij_smart_tabs = false
ij_visual_guides =
ij_wrap_on_typing = false

[{*.gradle.kts,*.kt,*.kts,*.main.kts,*.space.kts}]
max_line_length = 250
ij_kotlin_align_in_columns_case_branch = false
ij_kotlin_align_multiline_binary_operation = false
ij_kotlin_align_multiline_extends_list = false
ij_kotlin_align_multiline_method_parentheses = false
ij_kotlin_align_multiline_parameters = true
ij_kotlin_align_multiline_parameters_in_calls = false
ij_kotlin_allow_trailing_comma = false
ij_kotlin_allow_trailing_comma_on_call_site = false
ij_kotlin_assignment_wrap = normal
ij_kotlin_blank_lines_after_class_header = 0
ij_kotlin_blank_lines_around_block_when_branches = 0
ij_kotlin_blank_lines_before_declaration_with_comment_or_annotation_on_separate_line = 1
ij_kotlin_block_comment_add_space = false
ij_kotlin_block_comment_at_first_column = true
ij_kotlin_call_parameters_new_line_after_left_paren = true
ij_kotlin_call_parameters_right_paren_on_new_line = true
ij_kotlin_call_parameters_wrap = on_every_item
ij_kotlin_catch_on_new_line = false
ij_kotlin_class_annotation_wrap = split_into_lines
ij_kotlin_code_style_defaults = KOTLIN_OFFICIAL
ij_kotlin_continuation_indent_for_chained_calls = false
ij_kotlin_continuation_indent_for_expression_bodies = false
ij_kotlin_continuation_indent_in_argument_lists = false
ij_kotlin_continuation_indent_in_elvis = false
ij_kotlin_continuation_indent_in_if_conditions = false
ij_kotlin_continuation_indent_in_parameter_lists = false
ij_kotlin_continuation_indent_in_supertype_lists = false
ij_kotlin_else_on_new_line = false
ij_kotlin_enum_constants_wrap = off
ij_kotlin_extends_list_wrap = normal
ij_kotlin_field_annotation_wrap = split_into_lines
ij_kotlin_finally_on_new_line = false
ij_kotlin_if_rparen_on_new_line = true
ij_kotlin_import_nested_classes = false
ij_kotlin_imports_layout = *,java.**,javax.**,kotlin.**,^
ij_kotlin_insert_whitespaces_in_simple_one_line_method = true
ij_kotlin_keep_blank_lines_before_right_brace = 2
ij_kotlin_keep_blank_lines_in_code = 2
ij_kotlin_keep_blank_lines_in_declarations = 2
ij_kotlin_keep_first_column_comment = true
ij_kotlin_keep_indents_on_empty_lines = false
ij_kotlin_keep_line_breaks = true
ij_kotlin_lbrace_on_next_line = false
ij_kotlin_line_break_after_multiline_when_entry = true
ij_kotlin_line_comment_add_space = false
ij_kotlin_line_comment_add_space_on_reformat = false
ij_kotlin_line_comment_at_first_column = true
ij_kotlin_method_annotation_wrap = split_into_lines
ij_kotlin_method_call_chain_wrap = normal
ij_kotlin_method_parameters_new_line_after_left_paren = true
ij_kotlin_method_parameters_right_paren_on_new_line = true
ij_kotlin_method_parameters_wrap = on_every_item
ij_kotlin_name_count_to_use_star_import = 5
ij_kotlin_name_count_to_use_star_import_for_members = 3
ij_kotlin_packages_to_use_import_on_demand = java.util.*,kotlinx.android.synthetic.**,io.ktor.**
ij_kotlin_parameter_annotation_wrap = off
ij_kotlin_space_after_comma = true
ij_kotlin_space_after_extend_colon = true
ij_kotlin_space_after_type_colon = true
ij_kotlin_space_before_catch_parentheses = true
ij_kotlin_space_before_comma = false
ij_kotlin_space_before_extend_colon = true
ij_kotlin_space_before_for_parentheses = true
ij_kotlin_space_before_if_parentheses = true
ij_kotlin_space_before_lambda_arrow = true
ij_kotlin_space_before_type_colon = false
ij_kotlin_space_before_when_parentheses = true
ij_kotlin_space_before_while_parentheses = true
ij_kotlin_spaces_around_additive_operators = true
ij_kotlin_spaces_around_assignment_operators = true
ij_kotlin_spaces_around_equality_operators = true
ij_kotlin_spaces_around_function_type_arrow = true
ij_kotlin_spaces_around_logical_operators = true
ij_kotlin_spaces_around_multiplicative_operators = true
ij_kotlin_spaces_around_range = false
ij_kotlin_spaces_around_relational_operators = true
ij_kotlin_spaces_around_unary_operator = false
ij_kotlin_spaces_around_when_arrow = true
ij_kotlin_variable_annotation_wrap = off
ij_kotlin_while_on_new_line = false
ij_kotlin_wrap_elvis_expressions = 1
ij_kotlin_wrap_expression_body_functions = 1
ij_kotlin_wrap_first_method_in_call_chain = false
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ class AppMetrics(
getCounter(prefix, metric)!!.increment()
}

fun incrementCounterWithTag(metric: String, tagName: String) {
meterRegistry.counter("${APP_NAME}_${metric}", "tagname", tagName).increment()
}

fun incrementRestCounter(tpLeverandorName: String, status: String) {
restMetrics.getOrPut(tpLeverandorName) {
Counter.builder("${APP_NAME}_rest_request")
Expand Down Expand Up @@ -123,6 +127,7 @@ class AppMetrics(
const val TP_TOTAL_SIMULERING_CALLS = APP_NAME + "_tp_simulering_calls_"
const val TP_TOTAL_SIMULERING_TIME = APP_NAME + "_tp_simulering_time_"
const val TP_LATEST_SIMULERING_TIME = APP_NAME + "_tp_simulering_time_latest_"
const val TP_REQUESTED_LEVERANDOR = APP_NAME + "_tp_requested_leverandor"
const val APP_TOTAL_STILLINGSPROSENT_OK = APP_NAME + "_stillingsprosent_ok"
const val APP_TOTAL_STILLINGSPROSENT_ERROR = APP_NAME + "_stillingsprosent_error"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ class SimuleringEndpoint(

@PostMapping("/simulering")
fun simuler(
@RequestBody body: SimulerOffentligTjenestepensjonRequest, @RequestHeader(value = NAV_CALL_ID, required = false) navCallId: String?
@RequestBody body: SimulerOffentligTjenestepensjonRequest,
@RequestHeader(value = NAV_CALL_ID, required = false) navCallId: String?
): ResponseEntity<Any> {
addHeaderToRequestContext(NAV_CALL_ID, navCallId)
log.info("Processing nav-call-id: ${getHeaderFromRequestContext(NAV_CALL_ID)}")
Expand All @@ -63,11 +64,11 @@ class SimuleringEndpoint(

return try {
val fnr = FNR(body.fnr)
val tpOrdningAndLeverandorMap = tpClient.getTpOrdningerForPerson(fnr).let(::getTpLeverandorer)
val stillingsprosentResponse =
stillingsprosentService.getStillingsprosentListe(fnr, tpOrdningAndLeverandorMap)
val tpOrdning =
stillingsprosentService.getLatestFromStillingsprosent(stillingsprosentResponse.tpOrdningStillingsprosentMap)
val tpOrdningAndLeverandorMap = tpClient.findForhold(fnr)
.mapNotNull { forhold -> tpClient.findTssId(forhold.ordning)?.let { TPOrdning(tpId = forhold.ordning, tssId = it) } }
.let(::getTpLeverandorer)
val stillingsprosentResponse = stillingsprosentService.getStillingsprosentListe(fnr, tpOrdningAndLeverandorMap)
val tpOrdning = stillingsprosentService.getLatestFromStillingsprosent(stillingsprosentResponse.tpOrdningStillingsprosentMap)

metrics.incrementCounter(APP_NAME, APP_TOTAL_STILLINGSPROSENT_OK)
val tpLeverandor = tpOrdningAndLeverandorMap[tpOrdning]!!
Expand Down Expand Up @@ -144,12 +145,14 @@ class SimuleringEndpoint(
fun getHeaderFromRequestContext(key: String) =
currentRequestAttributes().getAttribute(key, SCOPE_REQUEST)?.toString()

private fun getTpLeverandorer(tpOrdningList: List<TPOrdning>) =
asyncExecutor.executeAsync(tpOrdningList.associateWith { tpOrdning ->
FindTpLeverandorCallable(tpOrdning, tpClient, tpLeverandorList)
private fun getTpLeverandorer(tpOrdningList: List<TPOrdning>): MutableMap<TPOrdning, TpLeverandor> {
if (tpOrdningList.isEmpty()) throw LeveradoerNotFoundException("TSSnr not found for any tpOrdning.")
return asyncExecutor.executeAsync(tpOrdningList.associateWith { tpOrdning ->
FindTpLeverandorCallable(tpOrdning, tpClient, tpLeverandorList, metrics)
}).resultMap.apply {
if (isEmpty()) throw LeveradoerNotFoundException("No Tp-leverandoer found for person.")
}
}

companion object {
const val NAV_CALL_ID = "nav-call-id"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package no.nav.tjenestepensjon.simulering.service

import com.fasterxml.jackson.databind.json.JsonMapper
import com.fasterxml.jackson.module.kotlin.readValue
import no.nav.tjenestepensjon.simulering.AppMetrics
import no.nav.tjenestepensjon.simulering.config.CacheConfig.Companion.TP_ORDNING_LEVERANDOR_CACHE
import no.nav.tjenestepensjon.simulering.config.CacheConfig.Companion.TP_ORDNING_PERSON_CACHE
import no.nav.tjenestepensjon.simulering.config.CacheConfig.Companion.TP_ORDNING_TSSID_CACHE
Expand Down Expand Up @@ -29,7 +30,7 @@ class TpClient(
private val aadClient: AADClient,
private val jsonMapper: JsonMapper,
@Value("\${tp.url}") private var tpUrl: String,
@Value("\${tp.scope}") private val tpScope: String
@Value("\${tp.scope}") private val tpScope: String,
) {
private val log = LoggerFactory.getLogger(javaClass)

Expand Down Expand Up @@ -73,7 +74,7 @@ class TpClient(
}

@Cacheable(TP_ORDNING_LEVERANDOR_CACHE)
fun findTpLeverandor(tpOrdning: TPOrdning): String? =
fun findTpLeverandorName(tpOrdning: TPOrdning): String? =
webClient.get().uri("$tpUrl/api/tpconfig/tpleverandoer/${tpOrdning.tpId}").exchangeToMono {
when (it.statusCode().value()) {
200 -> it.bodyToMono<String>()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package no.nav.tjenestepensjon.simulering.v1.consumer

import no.nav.tjenestepensjon.simulering.AppMetrics
import no.nav.tjenestepensjon.simulering.exceptions.LeveradoerNotFoundException
import no.nav.tjenestepensjon.simulering.model.domain.TPOrdning
import no.nav.tjenestepensjon.simulering.model.domain.TpLeverandor
Expand All @@ -10,11 +11,13 @@ import java.util.concurrent.Callable
class FindTpLeverandorCallable(
private val tpOrdning: TPOrdning,
private val tpClient: TpClient,
@Qualifier("tpLeverandor") private val tpLeverandorList: List<TpLeverandor>
@Qualifier("tpLeverandor") private val tpLeverandorList: List<TpLeverandor>,
private val metrics: AppMetrics
) : Callable<TpLeverandor> {
@Throws(Exception::class)
override fun call(): TpLeverandor {
return tpClient.findTpLeverandor(tpOrdning).let { tpLeverandor ->
return tpClient.findTpLeverandorName(tpOrdning).let { tpLeverandor ->
metrics.incrementCounterWithTag(AppMetrics.Metrics.TP_REQUESTED_LEVERANDOR, "${tpOrdning.tpId} $tpLeverandor")
tpLeverandorList.firstOrNull { l: TpLeverandor -> tpLeverandor.equals(l.name, ignoreCase = true) }
} ?: throw LeveradoerNotFoundException("Leveradoer not found for tpOrdning ${tpOrdning.tpId}.")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ internal class WebClientConfigTest {

@Test
fun `Should throw exception if read timeout exceeded`() {
assertThrows<RuntimeException> { tpClient.findTpLeverandor(TPOrdning(defaultTssid, defaultTpid)) }
assertThrows<RuntimeException> { tpClient.findTpLeverandorName(TPOrdning(defaultTssid, defaultTpid)) }
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package no.nav.tjenestepensjon.simulering.service

import no.nav.tjenestepensjon.simulering.AppMetrics
import no.nav.tjenestepensjon.simulering.model.domain.TPOrdning
import no.nav.tjenestepensjon.simulering.model.domain.TpLeverandor
import no.nav.tjenestepensjon.simulering.model.domain.TpLeverandor.EndpointImpl.SOAP
Expand All @@ -16,14 +17,16 @@ internal class FindTpLeverandorCallableTest {

@Mock
private lateinit var tpClient: TpClient
@Mock
private lateinit var appMetrics: AppMetrics

private val tpOrdning = TPOrdning("80001234", "1234")
private val tpLeverandorMap = listOf(TpLeverandor("tpLeverandorName", SOAP, "simulerUrl", "stillingUrl"))

@Test
fun `Should return mapped leverandor`() {
`when`(tpClient.findTpLeverandor(tpOrdning)).thenReturn("tpLeverandorName")
FindTpLeverandorCallable(tpOrdning, tpClient, tpLeverandorMap).call().apply {
`when`(tpClient.findTpLeverandorName(tpOrdning)).thenReturn("tpLeverandorName")
FindTpLeverandorCallable(tpOrdning, tpClient, tpLeverandorMap, appMetrics).call().apply {
assertEquals("tpLeverandorName", name)
assertEquals(SOAP, impl)
assertEquals("simulerUrl", simuleringUrl)
Expand Down

0 comments on commit c3fded6

Please sign in to comment.