Skip to content

Commit

Permalink
KVL-914 Add unit test and fix trace context propagation from config m…
Browse files Browse the repository at this point in the history
…anagement service (#9694)

* Add unit test for trace context propagation from config management service
Fix invalid telemetry context
CHANGELOG_BEGIN
CHANGELOG_END
  • Loading branch information
hubert-da authored May 17, 2021
1 parent 4e63299 commit 3666cf2
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 2 deletions.
6 changes: 6 additions & 0 deletions ledger/participant-integration-api/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,10 @@ da_scala_test_suite(
"//ledger/ledger-resources",
"//ledger/ledger-resources:ledger-resources-test-lib",
"//ledger/metrics",
"//ledger/metrics:metrics-test-lib",
"//ledger/participant-state",
"//ledger/participant-state-index",
"//ledger/participant-state/kvutils",
"//ledger/test-common",
"//ledger/test-common:dar-files-stable-lib",
"//libs-scala/concurrent",
Expand All @@ -273,6 +275,10 @@ da_scala_test_suite(
"@maven//:io_grpc_grpc_netty",
"@maven//:io_netty_netty_handler",
"@maven//:io_netty_netty_transport",
"@maven//:io_opentelemetry_opentelemetry_api",
"@maven//:io_opentelemetry_opentelemetry_context",
"@maven//:io_opentelemetry_opentelemetry_sdk_testing",
"@maven//:io_opentelemetry_opentelemetry_sdk_trace",
"@maven//:org_flywaydb_flyway_core",
"@maven//:org_mockito_mockito_core",
"@maven//:org_reactivestreams_reactive_streams",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
// Copyright (c) 2021 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package com.daml.platform.apiserver.services.admin

import java.time.{Duration => jDuration}
import java.util.concurrent.{CompletableFuture, CompletionStage}

import akka.stream.scaladsl.Source
import com.daml.api.util.TimeProvider
import com.daml.ledger.api.domain
import com.daml.ledger.api.domain.LedgerOffset.Absolute
import com.daml.ledger.api.testing.utils.AkkaBeforeAndAfterAll
import com.daml.ledger.api.v1.admin.config_management_service.{SetTimeModelRequest, TimeModel}
import com.daml.ledger.participant.state.index.v2.IndexConfigManagementService
import com.daml.ledger.participant.state.v1
import com.daml.ledger.participant.state.v1.{
Configuration,
SubmissionId,
SubmissionResult,
WriteConfigService,
}
import com.daml.lf.data.{Ref, Time}
import com.daml.logging.LoggingContext
import com.daml.platform.configuration.LedgerConfiguration
import com.daml.telemetry.{TelemetryContext, TelemetrySpecBase}
import com.google.protobuf.duration.Duration
import com.google.protobuf.timestamp.Timestamp
import org.mockito.{ArgumentMatchersSugar, MockitoSugar}
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AsyncWordSpec

import scala.concurrent.Future

class ApiConfigManagementServiceSpec
extends AsyncWordSpec
with TelemetrySpecBase
with MockitoSugar
with Matchers
with ArgumentMatchersSugar
with AkkaBeforeAndAfterAll {

import ApiConfigManagementServiceSpec._

private implicit val loggingContext: LoggingContext = LoggingContext.ForTesting

"ApiConfigManagementService" should {
"propagate trace context" in {
val mockReadBackend = mockIndexConfigManagementService()

val apiConfigManagementService = ApiConfigManagementService.createApiService(
mockReadBackend,
TestWriteConfigService,
TimeProvider.UTC,
LedgerConfiguration.defaultLocalLedger,
)

val span = anEmptySpan()
val scope = span.makeCurrent()
apiConfigManagementService
.setTimeModel(aSetTimeModelRequest)
.andThen { case _ =>
scope.close()
span.end()
}
.map { _ =>
spanExporter.finishedSpanAttributes should contain(anApplicationIdSpanAttribute)
}
}
}

private def mockIndexConfigManagementService() = {
val mockIndexConfigManagementService = mock[IndexConfigManagementService]
when(mockIndexConfigManagementService.lookupConfiguration()(any[LoggingContext]))
.thenReturn(Future.successful(None))
when(
mockIndexConfigManagementService.configurationEntries(any[Option[Absolute]])(
any[LoggingContext]
)
).thenReturn(configurationEntries)
mockIndexConfigManagementService
}

private object TestWriteConfigService extends WriteConfigService {
override def submitConfiguration(
maxRecordTime: Time.Timestamp,
submissionId: SubmissionId,
config: Configuration,
)(implicit telemetryContext: TelemetryContext): CompletionStage[SubmissionResult] = {
telemetryContext.setAttribute(
anApplicationIdSpanAttribute._1,
anApplicationIdSpanAttribute._2,
)
CompletableFuture.completedFuture(SubmissionResult.Acknowledged)
}
}
}

object ApiConfigManagementServiceSpec {
private val aSubmissionId = "aSubmission"

private val aConfigurationGeneration = 0L

private val configurationEntries = Source.single(
Absolute(Ref.LedgerString.assertFromString("0")) -> domain.ConfigurationEntry.Accepted(
aSubmissionId,
Configuration(
aConfigurationGeneration,
v1.TimeModel.reasonableDefault,
jDuration.ZERO,
),
)
)

private val aSetTimeModelRequest = SetTimeModelRequest(
aSubmissionId,
Some(Timestamp.defaultInstance),
aConfigurationGeneration,
Some(
TimeModel(
Some(Duration.defaultInstance),
Some(Duration.defaultInstance),
Some(Duration.defaultInstance),
)
),
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import com.daml.ledger.participant.state.kvutils.{Envelope, KeyValueSubmission}
import com.daml.ledger.participant.state.v1._
import com.daml.lf.data.{Ref, Time}
import com.daml.metrics.Metrics
import com.daml.telemetry.{NoOpTelemetryContext, TelemetryContext}
import com.daml.telemetry.TelemetryContext

import scala.compat.java8.FutureConverters

Expand Down Expand Up @@ -64,7 +64,7 @@ class KeyValueParticipantStateWriter(writer: LedgerWriter, metrics: Metrics) ext
val submission =
keyValueSubmission
.configurationToSubmission(maxRecordTime, submissionId, writer.participantId, config)
commit(submissionId, submission)(NoOpTelemetryContext)
commit(submissionId, submission)
}

override def allocateParty(
Expand Down

0 comments on commit 3666cf2

Please sign in to comment.