Skip to content

core: improve ergonomics of the attribute API (Scala 3)#1191

Open
iRevive wants to merge 1 commit intotypelevel:mainfrom
iRevive:topic/scala3-attribute-ergonomics
Open

core: improve ergonomics of the attribute API (Scala 3)#1191
iRevive wants to merge 1 commit intotypelevel:mainfrom
iRevive:topic/scala3-attribute-ergonomics

Conversation

@iRevive
Copy link
Contributor

@iRevive iRevive commented Mar 7, 2026

Note

The new API will be available only for Scala 3.

These changes should address the existing 'verbosity' problem:

The new varargs API allows using Attribute, Option[Attribute], and Iterable[Attribute] in the same call.


Let's imagine we have the following setup:

val userIdKey = AttributeKey[String]("user.id")
val planKey   = AttributeKey[String]("plan")

// per-request dynamic attributes
val userId: Option[String] = Some("u-123")
val plan: Option[String]   = None
val extra = Attributes(Attribute("region", "us"), Attribute("env", "prod"))
val base  = Attribute("endpoint", "/checkout")

Currently, to create a span or record a metric, we need to do the following:

// combined attributes
val attrs: Attributes = {
  val builder = Attributes.newBuilder
  builder.addAll(userIdKey.maybe(userId))
  builder.addAll(planKey.maybe(plan))
  builder.addAll(extra)
  builder.add(base)
  builder.result()
}

// record ops
tracer.span("http.request", attrs)

With the new API, these attributes can be used directly:

tracer.span(
  "http.request", 
  userIdKey.maybe(userId),
  planKey.maybe(plan),
  extra,
  Attribute("endpoint", "/checkout")
)

@mergify mergify bot added module:core Features and improvements to core module metrics Improvement to metrics module tracing Improvements to tracing module labels Mar 7, 2026
@iRevive iRevive changed the title core-common: improve ergonomics of the attribute API core-common: improve ergonomics of the attribute API (Scala 3) Mar 7, 2026
@iRevive iRevive changed the title core-common: improve ergonomics of the attribute API (Scala 3) core: improve ergonomics of the attribute API (Scala 3) Mar 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

metrics Improvement to metrics module module:core Features and improvements to core module tracing Improvements to tracing module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant