Skip to content

Commit

Permalink
Add Empty GRPC Metering Report Service [DPP-856] (#12515)
Browse files Browse the repository at this point in the history
* With mock metering service

* Reformat

changelog_begin
changelog_end

* Include nanos in generation time

* Update with review comments

* Update service count
  • Loading branch information
simonmaxen-da authored Jan 21, 2022
1 parent 4d26f08 commit cbb4986
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 10 deletions.
1 change: 1 addition & 0 deletions ledger-api/grpc-definitions/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ filegroup(
ledger_api_haskellpb_sources_admin = [
"PackageManagementService.hs",
"PartyManagementService.hs",
"MeteringReportService.hs",
]

genrule(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (c) 2022 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package com.daml.ledger.api.auth.services

import com.daml.ledger.api.auth.Authorizer
import com.daml.ledger.api.v1.admin.metering_report_service.MeteringReportServiceGrpc.MeteringReportService
import com.daml.ledger.api.v1.admin.metering_report_service.{
GetMeteringReportRequest,
GetMeteringReportResponse,
MeteringReportServiceGrpc,
}
import com.daml.platform.api.grpc.GrpcApiService
import com.daml.platform.server.api.ProxyCloseable
import io.grpc.ServerServiceDefinition

import scala.concurrent.{ExecutionContext, Future}

private[daml] final class MeteringReportServiceAuthorization(
protected val service: MeteringReportService with AutoCloseable,
private val authorizer: Authorizer,
)(implicit executionContext: ExecutionContext)
extends MeteringReportService
with ProxyCloseable
with GrpcApiService {

override def getMeteringReport(
request: GetMeteringReportRequest
): Future[GetMeteringReportResponse] = {
authorizer.requireAdminClaims(service.getMeteringReport)(request)
}

override def bindService(): ServerServiceDefinition =
MeteringReportServiceGrpc.bindService(this, executionContext)

override def close(): Unit = service.close()

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import com.daml.ledger.api.v1.active_contracts_service.ActiveContractsServiceGrp
import com.daml.ledger.api.v1.active_contracts_service.ActiveContractsServiceGrpc.ActiveContractsService
import com.daml.ledger.api.v1.admin.config_management_service.ConfigManagementServiceGrpc
import com.daml.ledger.api.v1.admin.config_management_service.ConfigManagementServiceGrpc.ConfigManagementService
import com.daml.ledger.api.v1.admin.metering_report_service.MeteringReportServiceGrpc
import com.daml.ledger.api.v1.admin.metering_report_service.MeteringReportServiceGrpc.MeteringReportService
import com.daml.ledger.api.v1.admin.package_management_service.PackageManagementServiceGrpc
import com.daml.ledger.api.v1.admin.package_management_service.PackageManagementServiceGrpc.PackageManagementService
import com.daml.ledger.api.v1.admin.participant_pruning_service.ParticipantPruningServiceGrpc
Expand Down Expand Up @@ -66,6 +68,9 @@ private[infrastructure] final class LedgerServices(
val partyManagement: PartyManagementService =
PartyManagementServiceGrpc.stub(participant)

val meteringReport: MeteringReportService =
MeteringReportServiceGrpc.stub(participant)

val packageManagement: PackageManagementService =
PackageManagementServiceGrpc.stub(participant)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

package com.daml.platform.apiserver

import java.time.Duration

import akka.stream.Materializer
import com.daml.api.util.TimeProvider
import com.daml.error.ErrorCodesVersionSwitcher
Expand All @@ -31,13 +29,7 @@ import com.daml.platform.apiserver.execution.{
TimedCommandExecutor,
}
import com.daml.platform.apiserver.services._
import com.daml.platform.apiserver.services.admin.{
ApiConfigManagementService,
ApiPackageManagementService,
ApiParticipantPruningService,
ApiPartyManagementService,
ApiUserManagementService,
}
import com.daml.platform.apiserver.services.admin._
import com.daml.platform.apiserver.services.transaction.ApiTransactionService
import com.daml.platform.configuration.{
CommandConfiguration,
Expand All @@ -52,6 +44,7 @@ import com.daml.telemetry.TelemetryContext
import io.grpc.BindableService
import io.grpc.protobuf.services.ProtoReflectionService

import java.time.Duration
import scala.collection.immutable
import scala.concurrent.duration.{Duration => ScalaDuration}
import scala.concurrent.{ExecutionContext, Future}
Expand Down Expand Up @@ -220,6 +213,8 @@ private[daml] object ApiServices {
None
}

val apiMeteringReportService = new ApiMeteringReportService()

apiTimeServiceOpt.toList :::
writeServiceBackedApiServices :::
List(
Expand All @@ -232,6 +227,7 @@ private[daml] object ApiServices {
apiReflectionService,
apiHealthService,
apiVersionService,
new MeteringReportServiceAuthorization(apiMeteringReportService, authorizer),
) ::: maybeApiUserManagementService.toList
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright (c) 2022 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package com.daml.platform.apiserver.services

import com.daml.ledger.api.v1.admin.metering_report_service.MeteringReportServiceGrpc.MeteringReportService
import com.daml.ledger.api.v1.admin.metering_report_service._
import com.daml.logging.{ContextualizedLogger, LoggingContext}
import com.daml.platform.api.grpc.GrpcApiService
import io.grpc.ServerServiceDefinition

import scala.concurrent.{ExecutionContext, Future}

private[apiserver] final class ApiMeteringReportService()(implicit
executionContext: ExecutionContext,
loggingContext: LoggingContext,
) extends MeteringReportService
with GrpcApiService {

private implicit val logger: ContextualizedLogger = ContextualizedLogger.get(this.getClass)

override def bindService(): ServerServiceDefinition =
MeteringReportServiceGrpc.bindService(this, executionContext)

override def close(): Unit = ()

override def getMeteringReport(
request: GetMeteringReportRequest
): Future[GetMeteringReportResponse] = {
logger.info(s"Received metering report request: $request")
val now = java.time.Instant.now()
val generationTime =
com.google.protobuf.timestamp.Timestamp.of(now.getEpochSecond, now.getNano)
val participantReport = ParticipantMeteringReport(
participantId = "participant1",
request.to.orElse(Some(generationTime)),
Seq(
ApplicationMeteringReport("app1", 100),
ApplicationMeteringReport("app2", 200),
),
)
val response =
GetMeteringReportResponse(Some(request), Some(participantReport), Some(generationTime))
Future.successful(response).andThen(logger.logErrorsOnCall[GetMeteringReportResponse])
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (c) 2022 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package com.daml
package platform.sandbox.auth

import com.daml.ledger.api.v1.admin.metering_report_service.{
GetMeteringReportRequest,
MeteringReportServiceGrpc,
}

import scala.concurrent.Future

final class MeteringReportAuthIT extends AdminServiceCallAuthTests {

override def serviceCallName: String = "MeteringReportService#GetMeteringReport"

override def serviceCallWithToken(token: Option[String]): Future[Any] =
stub(MeteringReportServiceGrpc.stub(channel), token)
.getMeteringReport(GetMeteringReportRequest(None, None))

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ final class ReflectionIT
"accessed" should {

"provide a list of exposed services" in {
val expectedServiceCount: Int = 17
val expectedServiceCount: Int = 18
for {
response <- execRequest(listServices)
} yield {
Expand Down

0 comments on commit cbb4986

Please sign in to comment.