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

daml-lf/data: Optionally truncate party names in structured logs. [KVL-996] #10163

Merged
merged 6 commits into from
Jul 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 0 additions & 3 deletions daml-lf/data/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,6 @@ da_scala_test(
"-P:silencer:lineContentFilters=import ImmArraySeq.Implicits._",
"-P:silencer:lineContentFilters=signum",
],
versioned_deps = {
"2.12": ["//libs-scala/logging-entries"],
},
deps = [
":data",
"//daml-lf/data-scalacheck",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
package com.daml.lf
package data

import com.daml.logging.entries.ToLoggingValue

object Ref {

val IdString: IdString = new IdStringImpl
Expand All @@ -24,8 +22,8 @@ object Ref {
val PackageVersion: IdString.PackageVersion.type = IdString.PackageVersion

/** Party identifiers are non-empty US-ASCII strings built from letters, digits, space, colon, minus and,
* underscore. We use them to represent [Party] literals. In this way, we avoid
* empty identifiers, escaping problems, and other similar pitfalls.
* underscore. We use them to represent [Party] literals. In this way, we avoid
* empty identifiers, escaping problems, and other similar pitfalls.
*/
type Party = IdString.Party
val Party: IdString.Party.type = IdString.Party
Expand Down Expand Up @@ -98,9 +96,10 @@ object Ref {
override def toString: String = dottedName

override def compare(that: DottedName): Int = {
import scala.math.Ordering.Implicits._
import Name.ordering

import scala.math.Ordering.Implicits._

implicitly[Ordering[Seq[Name]]].compare(segments.toSeq, that.segments.toSeq)
}

Expand Down Expand Up @@ -211,9 +210,6 @@ object Ref {
@throws[IllegalArgumentException]
def assertFromString(s: String): Identifier =
assertRight(fromString(s))

implicit val `Identifier to LoggingValue`: ToLoggingValue[Identifier] =
ToLoggingValue.ToStringToLoggingValue
}

/* Choice name in a template. */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) 2021 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package com.daml.lf.data.logging

case class LoggingConfiguration(
maxPartyNameLength: Option[Int]
)

object LoggingConfiguration {
private val Default = LoggingConfiguration(
maxPartyNameLength = None
)

@volatile
private var _current = Default

def current: LoggingConfiguration = _current

def modify(f: LoggingConfiguration => LoggingConfiguration): Unit = {
_current = f(_current)
}

def set(configuration: LoggingConfiguration): Unit = {
_current = configuration
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) 2021 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package com.daml.lf.data

import com.daml.lf.data.Ref.{Identifier, Party}
import com.daml.logging.entries.{LoggingKey, LoggingValue, ToLoggingKey, ToLoggingValue}

package object logging {

implicit val `Identifier to LoggingValue`: ToLoggingValue[Identifier] =
ToLoggingValue.ToStringToLoggingValue

// The party name can grow quite long, so we offer ledger implementors the opportunity to truncate
// it in structured log output.
implicit val `Party to LoggingKey and LoggingValue`
: ToLoggingKey[Party] with ToLoggingValue[Party] =
new ToLoggingKey[Party] with ToLoggingValue[Party] {
override def toLoggingKey(party: Party): LoggingKey =
wrap(party).value

override def toLoggingValue(party: Party): LoggingValue =
wrap(party)

private def wrap(party: Party): LoggingValue.OfString =
LoggingConfiguration.current.maxPartyNameLength match {
case None => LoggingValue.OfString(party)
case Some(length) => LoggingValue.OfString(party).truncated(length)
}
}

}
1 change: 0 additions & 1 deletion daml-lf/encoder/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ da_scala_library(
"//daml-lf/archive:daml_lf_dev_archive_proto_java",
"//daml-lf/data",
"//daml-lf/language",
"//libs-scala/logging-entries",
"@maven//:com_google_protobuf_protobuf_java",
],
)
Expand Down
4 changes: 1 addition & 3 deletions daml-lf/engine/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ da_scala_library(
],
scalacopts = lf_scalacopts,
tags = ["maven_coordinates=com.daml:daml-lf-engine:__VERSION__"],
versioned_deps = {
"2.12": ["//libs-scala/logging-entries"],
},
versioned_scala_deps = {
"2.12": ["@maven//:org_scala_lang_modules_scala_collection_compat"],
},
Expand Down Expand Up @@ -70,6 +67,7 @@ da_scala_test_suite(
"//daml-lf/parser",
"//daml-lf/transaction",
"//daml-lf/transaction-test-lib",
"//libs-scala/logging-entries",
"@maven//:com_google_protobuf_protobuf_java",
],
)
Expand Down
3 changes: 0 additions & 3 deletions daml-lf/interface/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ da_scala_library(
],
scalacopts = lf_scalacopts,
tags = ["maven_coordinates=com.daml:daml-lf-interface:__VERSION__"],
versioned_deps = {
"2.13": ["//libs-scala/logging-entries"],
},
visibility = [
"//daml-assistant/daml-sdk:__subpackages__",
"//daml-lf:__subpackages__",
Expand Down
4 changes: 1 addition & 3 deletions daml-lf/interpreter/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,6 @@ da_scala_test_suite(
"@maven//:org_typelevel_paiges_core",
],
scalacopts = lf_scalacopts,
versioned_deps = {
"2.12": ["//libs-scala/logging-entries"],
},
deps = [
":interpreter",
"//daml-lf/data",
Expand All @@ -72,6 +69,7 @@ da_scala_test_suite(
"//daml-lf/transaction",
"//daml-lf/transaction-test-lib",
"//daml-lf/validation",
"//libs-scala/logging-entries",
"@maven//:org_slf4j_slf4j_api",
],
)
Expand Down
1 change: 0 additions & 1 deletion daml-lf/parser/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ da_scala_library(
deps = [
"//daml-lf/data",
"//daml-lf/language",
"//libs-scala/logging-entries",
],
)

Expand Down
1 change: 0 additions & 1 deletion daml-lf/transaction/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ da_scala_test(
"//daml-lf/interface",
"//daml-lf/language",
"//daml-lf/transaction-test-lib",
"//libs-scala/logging-entries",
"//libs-scala/scalatest-utils",
"@maven//:com_google_protobuf_protobuf_java",
],
Expand Down
1 change: 0 additions & 1 deletion daml-lf/validation/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ da_scala_library(
deps = [
"//daml-lf/data",
"//daml-lf/language",
"//libs-scala/logging-entries",
],
)

Expand Down
1 change: 0 additions & 1 deletion daml-script/runner/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ da_scala_library(
"//ledger/participant-state",
"//ledger/sandbox-classic:sandbox-classic-{}".format(edition),
"//ledger/sandbox-common",
"//libs-scala/logging-entries",
"//libs-scala/ports",
"//libs-scala/resources",
],
Expand Down
1 change: 1 addition & 0 deletions daml-script/test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ da_scala_test_suite(
"//ledger/sandbox-common:sandbox-common-scala-tests-lib",
"//ledger/test-common",
"//libs-scala/contextualized-logging",
"//libs-scala/logging-entries",
"//libs-scala/ports",
"//libs-scala/resources",
"//libs-scala/resources-akka",
Expand Down
3 changes: 0 additions & 3 deletions extractor/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,6 @@ da_scala_library(
scala_runtime_deps = [
"@maven//:org_tpolecat_doobie_postgres",
],
versioned_deps = {
"2.13": ["//libs-scala/logging-entries"],
},
visibility = ["//visibility:public"],
runtime_deps = [
"@maven//:ch_qos_logback_logback_classic",
Expand Down
1 change: 0 additions & 1 deletion language-support/scala/codegen/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ da_scala_library(
"//daml-lf/archive:daml_lf_dev_archive_proto_java",
"//daml-lf/data",
"//daml-lf/interface",
"//libs-scala/logging-entries",
"@maven//:com_google_protobuf_protobuf_java",
"@maven//:org_slf4j_slf4j_api",
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,12 @@ object domain {
implicit val `Absolute Ordering`: Ordering[LedgerOffset.Absolute] =
Ordering.by[LedgerOffset.Absolute, String](_.value)

implicit object `LedgerOffset to LoggingValue` extends ToLoggingValue[LedgerOffset] {
override def apply(value: LedgerOffset): LoggingValue =
LoggingValue.OfString(value match {
case LedgerOffset.Absolute(absolute) => absolute
case LedgerOffset.LedgerBegin => "%begin%"
case LedgerOffset.LedgerEnd => "%end%"
})
}

implicit val `LedgerOffset to LoggingValue`: ToLoggingValue[LedgerOffset] = value =>
LoggingValue.OfString(value match {
case LedgerOffset.Absolute(absolute) => absolute
case LedgerOffset.LedgerBegin => "%begin%"
case LedgerOffset.LedgerEnd => "%end%"
})
}

sealed trait Event extends Product with Serializable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ private[apiserver] final class ApiCommandService private (
): Future[Completion] =
withEnrichedLoggingContext(
logging.commandId(request.getCommands.commandId),
logging.party(request.getCommands.party),
logging.actAs(request.getCommands.actAs),
logging.readAs(request.getCommands.readAs),
logging.partyString(request.getCommands.party),
logging.actAsStrings(request.getCommands.actAs),
logging.readAsStrings(request.getCommands.readAs),
) { implicit loggingContext =>
if (running) {
ledgerConfigProvider.latestConfiguration.fold[Future[Completion]](
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ private[apiserver] final class ApiPartyManagementService private (
PartyDetails(details.party, details.displayName.getOrElse(""), details.isLocal)

override def getParties(request: GetPartiesRequest): Future[GetPartiesResponse] =
withEnrichedLoggingContext(logging.parties(request.parties)) { implicit loggingContext =>
withEnrichedLoggingContext(logging.partyStrings(request.parties)) { implicit loggingContext =>
logger.info("Getting parties")
partyManagementService
.getParties(request.parties.map(Ref.Party.assertFromString))
Expand All @@ -92,44 +92,45 @@ private[apiserver] final class ApiPartyManagementService private (
}

override def allocateParty(request: AllocatePartyRequest): Future[AllocatePartyResponse] =
withEnrichedLoggingContext(logging.party(request.partyIdHint)) { implicit loggingContext =>
logger.info("Allocating party")
implicit val telemetryContext: TelemetryContext =
DefaultTelemetry.contextFromGrpcThreadLocalContext()
val validatedPartyIdentifier =
if (request.partyIdHint.isEmpty) {
Future.successful(None)
} else {
Ref.Party
.fromString(request.partyIdHint)
.fold(
error =>
Future.failed(
ValidationLogger
.logFailureWithContext(request, ErrorFactories.invalidArgument(error))
),
party => Future.successful(Some(party)),
)
}

validatedPartyIdentifier
.flatMap(party => {
val displayName = if (request.displayName.isEmpty) None else Some(request.displayName)
synchronousResponse
.submitAndWait(submissionIdGenerator(party), (party, displayName))
.map { case PartyEntry.AllocationAccepted(_, partyDetails) =>
AllocatePartyResponse(
Some(
PartyDetails(
partyDetails.party,
partyDetails.displayName.getOrElse(""),
partyDetails.isLocal,
withEnrichedLoggingContext(logging.partyString(request.partyIdHint)) {
implicit loggingContext =>
logger.info("Allocating party")
implicit val telemetryContext: TelemetryContext =
DefaultTelemetry.contextFromGrpcThreadLocalContext()
val validatedPartyIdentifier =
if (request.partyIdHint.isEmpty) {
Future.successful(None)
} else {
Ref.Party
.fromString(request.partyIdHint)
.fold(
error =>
Future.failed(
ValidationLogger
.logFailureWithContext(request, ErrorFactories.invalidArgument(error))
),
party => Future.successful(Some(party)),
)
}

validatedPartyIdentifier
.flatMap(party => {
val displayName = if (request.displayName.isEmpty) None else Some(request.displayName)
synchronousResponse
.submitAndWait(submissionIdGenerator(party), (party, displayName))
.map { case PartyEntry.AllocationAccepted(_, partyDetails) =>
AllocatePartyResponse(
Some(
PartyDetails(
partyDetails.party,
partyDetails.displayName.getOrElse(""),
partyDetails.isLocal,
)
)
)
)
}
})
.andThen(logger.logErrorsOnCall[AllocatePartyResponse])
}
})
.andThen(logger.logErrorsOnCall[AllocatePartyResponse])
}

}
Expand Down
Loading