Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KVL-914 Add unit test and fix trace context propagation from config management service #9694

Merged
merged 2 commits into from
May 17, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add unit test for trace context propagation from config management se…
…rvice

Fix invalid telemetry context
CHANGELOG_BEGIN
CHANGELOG_END
  • Loading branch information
hubert-da committed May 14, 2021
commit d1a42912e3196d34d80007959cefe88f9a80e5df
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,124 @@
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the sake of curiosity: was this test working with the NoOp?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ApiConfigManagementService uses DefaultTelemetry by default, so hard to tell, TBH. The setAttribute method wouldn't work in the NoOp. Not sure if this answers your question.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or you're asking about the change from KeyValueParticipantStateWriter? Then yes, as this test doesn't go that far.

}
}
}

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)
}
}
}
Comment on lines +72 to +97
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not hugely important but, since you already have a twin object, it looks like you could move these two definitions to it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sadly I can't because:

  • mockIndexConfigManagementService uses mock from MockitoSugar
  • TestWriteConfigService uses anApplicationIdSpanAttribute from the spec base


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)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why it's been working so far:
image

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And is it still working after this change? 😄

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will have to test it e2e from the oem integration kit 😄

Copy link
Collaborator Author

@hubert-da hubert-da May 17, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it works, as we do not create any new spans and attributes between this place and next gRPC calls.

}

override def allocateParty(
Expand Down