Skip to content

Commit

Permalink
Merge pull request #11 from hmrc/APB-792-monitoring
Browse files Browse the repository at this point in the history
[AO][APB-792] monitor outgoing des and mapping calls
  • Loading branch information
frankoid authored May 18, 2017
2 parents 567f8ed + 60da9c1 commit e6cd22b
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ package uk.gov.hmrc.agentclientrelationships.connectors
import java.net.URL
import javax.inject.{Inject, Named, Singleton}

import play.api.libs.json._
import com.codahale.metrics.MetricRegistry
import com.kenshoo.play.metrics.Metrics
import play.api.libs.json.Reads._
import play.api.libs.json._
import uk.gov.hmrc.agent.kenshoo.monitoring.HttpAPIMonitor
import uk.gov.hmrc.agentclientrelationships.UriPathEncoding.encodePathSegment
import uk.gov.hmrc.agentmtdidentifiers.model.MtdItId
import uk.gov.hmrc.domain.{Nino, SaAgentReference}
Expand Down Expand Up @@ -53,25 +56,30 @@ object ClientRelationship {
class DesConnector @Inject()(@Named("des-baseUrl") baseUrl: URL,
@Named("des.authorizationToken") authorizationToken: String,
@Named("des.environment") environment: String,
httpGet: HttpGet) {
httpGet: HttpGet,
metrics: Metrics)
extends HttpAPIMonitor {
override val kenshooRegistry: MetricRegistry = metrics.defaultRegistry

def getNinoFor(mtdbsa: MtdItId)(implicit hc: HeaderCarrier): Future[Nino] = {
val url = new URL(baseUrl, s"/registration/business-details/mtdbsa/${ encodePathSegment(mtdbsa.value) }")
getWithDesHeaders[BusinessDetails](url).map(_.nino)
getWithDesHeaders[BusinessDetails]("GetRegistrationBusinessDetailsByMtdbsa", url).map(_.nino)
}

def getClientSaAgentSaReferences(nino: Nino)(implicit hc: HeaderCarrier): Future[Seq[SaAgentReference]] = {
val url = new URL(baseUrl, s"/registration/relationship/nino/${ encodePathSegment(nino.value) }")
getWithDesHeaders[ClientRelationship](url).map(_.agents.flatMap {
getWithDesHeaders[ClientRelationship]("GetStatusAgentRelationship", url).map(_.agents.flatMap {
case Agent(true, Some(agentId), None) => Some(agentId)
case _ => None
})
}

private def getWithDesHeaders[A: HttpReads](url: URL)(implicit hc: HeaderCarrier): Future[A] = {
private def getWithDesHeaders[A: HttpReads](apiName: String, url: URL)(implicit hc: HeaderCarrier): Future[A] = {
val desHeaderCarrier = hc.copy(
authorization = Some(Authorization(s"Bearer $authorizationToken")),
extraHeaders = hc.extraHeaders :+ "Environment" -> environment)
httpGet.GET[A](url.toString)(implicitly[HttpReads[A]], desHeaderCarrier)
monitor(s"ConsumedAPI-DES-$apiName-GET") {
httpGet.GET[A](url.toString)(implicitly[HttpReads[A]], desHeaderCarrier)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ package uk.gov.hmrc.agentclientrelationships.connectors
import java.net.URL
import javax.inject.{Inject, Named, Singleton}

import com.codahale.metrics.MetricRegistry
import com.kenshoo.play.metrics.Metrics
import play.api.libs.json._
import uk.gov.hmrc.agent.kenshoo.monitoring.HttpAPIMonitor
import uk.gov.hmrc.agentmtdidentifiers.model.Arn
import uk.gov.hmrc.domain.SaAgentReference
import uk.gov.hmrc.play.http._
Expand All @@ -37,11 +40,17 @@ object Mappings {
}

@Singleton
class MappingConnector @Inject()(@Named("agent-mapping-baseUrl") baseUrl: URL, httpGet: HttpGet) {
class MappingConnector @Inject()(
@Named("agent-mapping-baseUrl") baseUrl: URL,
httpGet: HttpGet,
metrics: Metrics)
extends HttpAPIMonitor {
override val kenshooRegistry: MetricRegistry = metrics.defaultRegistry

def getSaAgentReferencesFor(arn: Arn)(implicit hc: HeaderCarrier): Future[Seq[SaAgentReference]] = {
val url = new URL(baseUrl, s"/agent-mapping/mappings/${arn.value}")
httpGet.GET[Mappings](url.toString).map(_.mappings.map(_.saAgentReference))
monitor(s"ConsumedAPI-Digital-Mappings-GET") {httpGet.GET[Mappings](url.toString)}
.map(_.mappings.map(_.saAgentReference))
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package uk.gov.hmrc.agentrelationships.connectors

import com.kenshoo.play.metrics.Metrics
import org.scalatestplus.play.OneAppPerSuite
import play.api.Application
import play.api.inject.guice.GuiceApplicationBuilder
import uk.gov.hmrc.agentclientrelationships.WSHttp
import uk.gov.hmrc.agentclientrelationships.connectors.DesConnector
import uk.gov.hmrc.agentmtdidentifiers.model.MtdItId
Expand All @@ -12,9 +15,20 @@ import uk.gov.hmrc.play.test.UnitSpec

class DesConnectorSpec extends UnitSpec with OneAppPerSuite with WireMockSupport with DesStubs {

override implicit lazy val app: Application = appBuilder
.build()

protected def appBuilder: GuiceApplicationBuilder =
new GuiceApplicationBuilder()
.configure(
"microservice.services.des.port" -> wireMockPort,
"auditing.consumer.baseUri.host" -> wireMockHost,
"auditing.consumer.baseUri.port" -> wireMockPort
)

private implicit val hc = HeaderCarrier()

val desConnector = new DesConnector(wireMockBaseUrl, "token", "stub", WSHttp)
val desConnector = new DesConnector(wireMockBaseUrl, "token", "stub", WSHttp, app.injector.instanceOf[Metrics])

"DesConnector GetRegistrationBusinessDetails" should {

Expand Down Expand Up @@ -45,6 +59,13 @@ class DesConnectorSpec extends UnitSpec with OneAppPerSuite with WireMockSupport
givenDesReturnsServerError()
an[Exception] should be thrownBy await(desConnector.getNinoFor(mtdItId))
}

"record metrics for GetRegistrationBusinessDetailsByMtdbsa" in {
givenNinoIsKnownFor(mtdItId, Nino("AB123456C"))
await(desConnector.getNinoFor(mtdItId))
val metricsRegistry = app.injector.instanceOf[Metrics].defaultRegistry
metricsRegistry.getTimers.get("Timer-ConsumedAPI-DES-GetRegistrationBusinessDetailsByMtdbsa-GET").getCount should be >= 1L
}
}

"DesConnector GetStatusAgentRelationship" should {
Expand Down Expand Up @@ -102,5 +123,12 @@ class DesConnectorSpec extends UnitSpec with OneAppPerSuite with WireMockSupport
givenDesReturnsServerError()
an[Exception] should be thrownBy await(desConnector.getClientSaAgentSaReferences(nino))
}

"record metrics for GetStatusAgentRelationship" in {
givenClientHasRelationshipWithAgent(nino, "bar")
await(desConnector.getClientSaAgentSaReferences(nino))
val metricsRegistry = app.injector.instanceOf[Metrics].defaultRegistry
metricsRegistry.getTimers.get("Timer-ConsumedAPI-DES-GetStatusAgentRelationship-GET").getCount should be >= 1L
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,26 @@ class GovernmentGatewayProxyConnectorSpec extends UnitSpec with OneServerPerSuit
givenAgentIsNotAllocatedToClient("foo")
await(connector.getAllocatedAgentCodes(MtdItId("foo"))) should not contain AgentCode("bar")
}

"record metrics GsoAdminGetCredentialsForDirectEnrolments" in {
givenAgentCredentialsAreFoundFor(Arn("foo"), "bar")
await(connector.getCredIdFor(Arn("foo")))
val metricsRegistry = app.injector.instanceOf[Metrics].defaultRegistry
metricsRegistry.getTimers.get("Timer-ConsumedAPI-GGW-GsoAdminGetCredentialsForDirectEnrolments-POST").getCount should be >= 1L
}

"record metrics GsoAdminGetUserDetails" in {
givenAgentCodeIsFoundFor("foo", "bar")
await(connector.getAgentCodeFor("foo"))
val metricsRegistry = app.injector.instanceOf[Metrics].defaultRegistry
metricsRegistry.getTimers.get("Timer-ConsumedAPI-GGW-GsoAdminGetUserDetails-POST").getCount should be >= 1L
}

"record metrics GsoAdminGetAssignedAgents" in {
givenAgentIsAllocatedAndAssignedToClient("foo", "bar")
await(connector.getAllocatedAgentCodes(MtdItId("foo")))
val metricsRegistry = app.injector.instanceOf[Metrics].defaultRegistry
metricsRegistry.getTimers.get("Timer-ConsumedAPI-GGW-GsoAdminGetAssignedAgents-POST").getCount should be >= 1L
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package uk.gov.hmrc.agentrelationships.connectors

import com.kenshoo.play.metrics.Metrics
import org.scalatestplus.play.OneAppPerSuite
import play.api.Application
import play.api.inject.guice.GuiceApplicationBuilder
import uk.gov.hmrc.agentclientrelationships.WSHttp
import uk.gov.hmrc.agentclientrelationships.connectors.MappingConnector
import uk.gov.hmrc.agentmtdidentifiers.model.Arn
Expand All @@ -12,9 +15,20 @@ import uk.gov.hmrc.play.test.UnitSpec

class MappingConnectorSpec extends UnitSpec with OneAppPerSuite with WireMockSupport with MappingStubs {

override implicit lazy val app: Application = appBuilder
.build()

protected def appBuilder: GuiceApplicationBuilder =
new GuiceApplicationBuilder()
.configure(
"microservice.services.agent-mapping.port" -> wireMockPort,
"auditing.consumer.baseUri.host" -> wireMockHost,
"auditing.consumer.baseUri.port" -> wireMockPort
)

private implicit val hc = HeaderCarrier()

val mappingConnector = new MappingConnector(wireMockBaseUrl, WSHttp)
val mappingConnector = new MappingConnector(wireMockBaseUrl, WSHttp, app.injector.instanceOf[Metrics])

"MappingConnector" should {

Expand Down Expand Up @@ -46,6 +60,14 @@ class MappingConnectorSpec extends UnitSpec with OneAppPerSuite with WireMockSup
an[Exception] should be thrownBy await(mappingConnector.getSaAgentReferencesFor(arn))
}

"record metrics for Mappings" in {
givenArnIsKnownFor(arn, SaAgentReference("foo"))
await(mappingConnector.getSaAgentReferencesFor(arn))
val metricsRegistry = app.injector.instanceOf[Metrics].defaultRegistry
metricsRegistry.getTimers.get("Timer-ConsumedAPI-Digital-Mappings-GET").getCount should be >= 1L
}


}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package uk.gov.hmrc.agentrelationships.controllers

import com.github.tomakehurst.wiremock.client.WireMock.{aResponse, get, stubFor, urlMatching}
import org.scalatestplus.play.OneServerPerSuite
import play.api.Application
import play.api.inject.guice.GuiceApplicationBuilder
Expand Down Expand Up @@ -68,6 +69,7 @@ class RelationshipISpec extends UnitSpec with OneServerPerSuite with WireMockSup
givenAgentCredentialsAreNotFoundFor(Arn(arn))
givenAgentCodeIsFoundFor("foo", "bar")
givenAgentIsAllocatedAndAssignedToClient(mtditid, "bar")
givenNinoIsUnknownFor(MtdItId(mtditid))
val result = await(doAgentRequest())
result.status shouldBe 404
(result.json \ "code").as[String] shouldBe "INVALID_ARN"
Expand All @@ -77,6 +79,7 @@ class RelationshipISpec extends UnitSpec with OneServerPerSuite with WireMockSup
givenAgentCredentialsAreFoundFor(Arn(arn), "foo")
givenAgentCodeIsNotInTheResponseFor("foo")
givenAgentIsAllocatedAndAssignedToClient(mtditid, "bar")
givenNinoIsUnknownFor(MtdItId(mtditid))
val result = await(doAgentRequest())
result.status shouldBe 404
(result.json \ "code").as[String] shouldBe "UNKNOWN_AGENT_CODE"
Expand Down

0 comments on commit e6cd22b

Please sign in to comment.