Skip to content

Commit

Permalink
Merge pull request #206 from guardian/add-workspace-search-metric
Browse files Browse the repository at this point in the history
Create cloudwatch metric when user does workspace search
  • Loading branch information
marjisound authored Mar 25, 2024
2 parents cdd5215 + 63b9579 commit 819ca40
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 5 deletions.
2 changes: 1 addition & 1 deletion backend/app/AppComponents.scala
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ class AppComponents(context: Context, config: Config)
val collectionsController = new Collections(authControllerComponents, manifest, users, esResources, config.s3, esEvents, esPages, ingestionServices, annotations)
val blobsController = new Blobs(authControllerComponents, manifest, esResources, blobStorage, previewStorage, postgresClient)
val filtersController = new Filters(authControllerComponents, manifest, annotations)
val searchController = new Search(authControllerComponents, users, esResources, annotations)
val searchController = new Search(authControllerComponents, users, esResources, annotations, metricsService)
val documentsController = new Documents(authControllerComponents, manifest, esResources, blobStorage, users, annotations, config.auth.timeouts.maxDownloadAuthAge)
val resourceController = new Resource(authControllerComponents, manifest, esResources, esPages, annotations, previewStorage)
val emailController = new Emails(authControllerComponents, manifest, esResources, annotations)
Expand Down
8 changes: 6 additions & 2 deletions backend/app/controllers/api/Search.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,20 @@ import utils.Logging
import utils.attempt.{Attempt, ClientFailure, NotFoundFailure}
import utils.auth.{User, UserIdentityRequest}
import utils.controller.{AuthApiController, AuthControllerComponents}
import services.{MetricsService}

import scala.concurrent.ExecutionContext

class Search(override val controllerComponents: AuthControllerComponents, userManagement: UserManagement,
index: Index, annotations: Annotations) extends AuthApiController with Logging {
index: Index, annotations: Annotations, metricsService: MetricsService) extends AuthApiController with Logging {

def search() = ApiAction.attempt { req: UserIdentityRequest[_] =>
val q = req.queryString.getOrElse("q", Seq("")).head
val proposedParams = Search.buildSearchParameters(q, req)
proposedParams.workspaceContext.map(wc => logger.info(req.user.asLogMarker, s"Performing workspace search"))
proposedParams.workspaceContext.map(wc => {
logger.info(req.user.asLogMarker, "Performing workspace search")
metricsService.recordSearchInFolderEvent(req.user.username)
})

buildSearch(req.user, proposedParams, proposedParams.workspaceContext).flatMap { case (verifiedParams, context) =>
val returnEmptyResult = Search.shouldReturnEmptyResult(proposedParams, verifiedParams, context)
Expand Down
10 changes: 10 additions & 0 deletions backend/app/services/MetricsService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ object Metrics {
val batchesFailed = "IngestBatchesFailed"
val failureToResultMapper = "ErrorsInGiantFailureToResultMapper"
val usageEvents = "UsageEvents"
val searchInFolderEvents = "SearchInFolderEvents"


def metricDatum(name: String, dimensions: List[Dimension], value: Double): MetricDatum = {
Expand All @@ -34,12 +35,14 @@ trait MetricsService {
def updateMetrics(metrics: List[MetricUpdate]): Unit
def updateMetric(metricName: String, metricValue: Double = 1): Unit
def recordUsageEvent(username: String): Unit
def recordSearchInFolderEvent(username: String): Unit
}

class NoOpMetricsService() extends MetricsService {
def updateMetrics(metrics:List[MetricUpdate]): Unit = ()
def updateMetric(metricName: String, metricValue: Double = 1): Unit = ()
def recordUsageEvent(username: String): Unit = ()
def recordSearchInFolderEvent(username: String): Unit = ()
}

class CloudwatchMetricsService(config: AWSDiscoveryConfig) extends MetricsService with Logging {
Expand Down Expand Up @@ -90,4 +93,11 @@ class CloudwatchMetricsService(config: AWSDiscoveryConfig) extends MetricsServic

updateMetrics(List(MetricUpdate(Metrics.usageEvents, 1)), dimensions)
}

def recordSearchInFolderEvent(userEmail: String): Unit = {
val standardisedStage = if (config.stack == "pfi-giant") "PROD" else "CODE"
val dimensions = List(("App", "Giant"), ("Stage", standardisedStage), ("UserEmail", userEmail))

updateMetrics(List(MetricUpdate(Metrics.searchInFolderEvents, 1)), dimensions)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import test.integration.Helpers.stubControllerComponentsAsUser
import test.{TestAnnotations, TestUserManagement, TestUserRegistration}
import utils.IndexTestHelpers
import utils.attempt.AttemptAwait._
import services.{NoOpMetricsService}

class ElasticsearchResourcesITest extends AnyFreeSpec with Matchers with ElasticsearchTestService with IndexTestHelpers {
import TestUserManagement._
Expand Down Expand Up @@ -615,7 +616,8 @@ class ElasticsearchResourcesITest extends AnyFreeSpec with Matchers with Elastic
val annotations = new TestAnnotations(usersToWorkspaces)

val controllerComponents = stubControllerComponentsAsUser(reqUser.username, userManagement)
val search = new Search(controllerComponents, userManagement, elasticResources, annotations)
val metricsService = new NoOpMetricsService()
val search = new Search(controllerComponents, userManagement, elasticResources, annotations, metricsService)

fn(search)
}
Expand Down
4 changes: 3 additions & 1 deletion backend/test/test/integration/Helpers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import services.ingestion.IngestionServices
import services.manifest.Neo4jManifest
import services.users.{Neo4jUserManagement, UserManagement}
import services.{BucketConfig, Neo4jQueryLoggingConfig, S3Config, TestTypeDetector}
import services.{NoOpMetricsService}
import test.integration.Helpers.BlobAndNodeId
import test.{TestAuthActionBuilder, TestObjectStorage, TestPostgresClient, TestPreviewService, TestUserManagement}
import utils.Logging
Expand Down Expand Up @@ -257,7 +258,8 @@ object Helpers extends Matchers with Logging with OptionValues with Inside {
val resourceController = new Resource(controllerComponents, manifest, elasticsearch.elasticResources, elasticsearch.elasticPages, annotations, null)
val filtersController = new Filters(controllerComponents, manifest, annotations)
val workspaceController = new Workspaces(controllerComponents, annotations, elasticsearch.elasticResources, manifest, userManagement, new TestObjectStorage(), new TestObjectStorage(), new TestPostgresClient)
val searchController = new Search(controllerComponents, userManagement, elasticsearch.elasticResources, annotations)
val metricsService = new NoOpMetricsService()
val searchController = new Search(controllerComponents, userManagement, elasticsearch.elasticResources, annotations, metricsService)
val documentsController = new Documents(controllerComponents, manifest, elasticsearch.elasticResources, null, userManagement, annotations, downloadExpiryPeriod)
val previewsController = new Previews(controllerComponents, manifest, elasticsearch.elasticResources, new TestPreviewService, userManagement, annotations, downloadExpiryPeriod)

Expand Down

0 comments on commit 819ca40

Please sign in to comment.