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

core-common: add AttributeKey.Make #913

Closed
wants to merge 2 commits into from

Conversation

iRevive
Copy link
Contributor

@iRevive iRevive commented Feb 7, 2025

The PR is based on #911.

Motivation

Many codebases utilize newtype/opaque types for type safety. When converted to attributes, some entities must always use the same name.

With Attribute.Make the conversion process can be simplified.


Example

The model definition:

case class UserId(id: Int)

object UserId {
  implicit val userIdFocus: AttributeKey.Focus[UserId, Long] =
    _.id.toLong
    
  implicit val userIdAttribute: Attribute.Make[UserId] =
    Attribute.Make.named("user.id")
}

Before

def findUser(userId: UserId): F[Unit] = 
  Tracer[F].span("findUser", Attribute("user.id", userId.id.toLong))

After

def findUser(userId: UserId): F[Unit] = 
  Tracer[F].span("findUser", userId)

@mergify mergify bot added module:core Features and improvements to core module module:oteljava Features and improvements to the oteljava module metrics Improvement to metrics module labels Feb 7, 2025
Comment on lines +149 to +150
implicit def materialize[A](value: A)(implicit ev: Make[A]): Attribute[ev.Key] =
ev.make(value)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The implicit conversion happens here.

@iRevive iRevive closed this Feb 10, 2025
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 module:oteljava Features and improvements to the oteljava module
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant