-
Notifications
You must be signed in to change notification settings - Fork 40
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
Add Resource entity #240
Merged
Merged
Add Resource entity #240
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c723f9b
Add Resource entity
kovstas 905e585
Add tests, show instances and build enhancements
kovstas 6edb828
Add Attributes monoid and update SDK version in build info
kovstas 8844cd0
Add NoPublishPlugin for sdk-common
kovstas c2a6dcf
Add law test for Attributes and refactored some Attributes and Resour…
kovstas a8dc89f
Add a header for the new law test
kovstas ecd40fb
Add ResourceInitiationError to the Resource.mergeInto signature
kovstas 85ebbb4
Merge branch 'typelevel:main' into sdk-resource-class
kovstas de74548
fix doc and add minor improvements for the Resource
kovstas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
sdk/common/src/main/scala/org/typelevel/otel4s/sdk/Attributes.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* Copyright 2023 Typelevel | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.typelevel.otel4s.sdk | ||
|
||
import cats.Applicative | ||
import cats.Monad | ||
import cats.Monoid | ||
import cats.Show | ||
import cats.implicits._ | ||
import org.typelevel.otel4s.Attribute | ||
import org.typelevel.otel4s.Attribute.KeySelect | ||
import org.typelevel.otel4s.AttributeKey | ||
|
||
/** An immutable collection of [[Attribute]]s. | ||
*/ | ||
final class Attributes private ( | ||
private val m: Map[AttributeKey[_], Attribute[_]] | ||
) { | ||
def get[T: KeySelect](name: String): Option[Attribute[T]] = { | ||
val key = KeySelect[T].make(name) | ||
m.get(key).map(_.asInstanceOf[Attribute[T]]) | ||
} | ||
def get[T](key: AttributeKey[T]): Option[Attribute[T]] = | ||
m.get(key).map(_.asInstanceOf[Attribute[T]]) | ||
|
||
def isEmpty: Boolean = m.isEmpty | ||
def size: Int = m.size | ||
def contains(key: AttributeKey[_]): Boolean = m.contains(key) | ||
def foldLeft[F[_]: Monad, B](z: B)(f: (B, Attribute[_]) => F[B]): F[B] = | ||
m.foldLeft(Monad[F].pure(z)) { (acc, v) => | ||
acc.flatMap { b => | ||
f(b, v._2) | ||
} | ||
} | ||
def forall[F[_]: Monad](p: Attribute[_] => F[Boolean]): F[Boolean] = | ||
foldLeft(true)((b, a) => { | ||
if (b) p(a).map(b && _) | ||
else Monad[F].pure(false) | ||
}) | ||
def foreach[F[_]: Applicative](f: Attribute[_] => F[Unit]): F[Unit] = | ||
m.foldLeft(Applicative[F].unit) { (acc, v) => | ||
acc *> f(v._2) | ||
} | ||
|
||
def toMap: Map[AttributeKey[_], Attribute[_]] = m | ||
def toList: List[Attribute[_]] = m.values.toList | ||
|
||
} | ||
|
||
object Attributes { | ||
kovstas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
val Empty = new Attributes(Map.empty) | ||
|
||
def apply(attributes: Attribute[_]*): Attributes = { | ||
val builder = Map.newBuilder[AttributeKey[_], Attribute[_]] | ||
attributes.foreach { a => | ||
builder += (a.key -> a) | ||
} | ||
new Attributes(builder.result()) | ||
} | ||
|
||
implicit val showAttributes: Show[Attributes] = Show.show { attributes => | ||
attributes.toList | ||
.map(a => show"$a") | ||
.mkString("Attributes(", ", ", ")") | ||
} | ||
|
||
implicit val monoidAttributes: Monoid[Attributes] = | ||
new Monoid[Attributes] { | ||
def empty: Attributes = Attributes.Empty | ||
def combine(x: Attributes, y: Attributes): Attributes = { | ||
if (y.isEmpty) x | ||
else if (x.isEmpty) y | ||
else new Attributes(x.m ++ y.m) | ||
} | ||
} | ||
} |
127 changes: 127 additions & 0 deletions
127
sdk/common/src/main/scala/org/typelevel/otel4s/sdk/Resource.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
/* | ||
* Copyright 2023 Typelevel | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.typelevel.otel4s.sdk | ||
|
||
import cats.Show | ||
import cats.implicits.catsSyntaxEitherId | ||
import cats.implicits.catsSyntaxOptionId | ||
import cats.implicits.catsSyntaxSemigroup | ||
import cats.implicits.showInterpolator | ||
import org.typelevel.otel4s.Attribute | ||
import org.typelevel.otel4s.sdk.BuildInfo | ||
import org.typelevel.otel4s.sdk.Resource.ResourceInitiationError | ||
import org.typelevel.otel4s.semconv.resource.attributes.ResourceAttributes._ | ||
|
||
import scala.util.control.NoStackTrace | ||
|
||
/** [[Resource]] serves as a representation of a resource that captures | ||
* essential identifying information regarding the entities associated with | ||
* reported signals, such as statistics or traces. | ||
* @param attributes | ||
* \- a collection of [[Attribute]]s | ||
* @param schemaUrl | ||
* \- an optional schema URL | ||
*/ | ||
final case class Resource( | ||
attributes: Attributes, | ||
schemaUrl: Option[String] | ||
) { | ||
|
||
/** Merges [[Resource]] into another [[Resource]]. If the same attribute | ||
* exists in both resources, the attribute in the other [[Resource]] will be | ||
* used. | ||
* @param other | ||
* \- the other [[Resource]] to merge into. | ||
* @return | ||
* a new [[Resource]] with the merged attributes. | ||
*/ | ||
def mergeInto(other: Resource): Either[ResourceInitiationError, Resource] = { | ||
if (other == Resource.Empty) this.asRight | ||
else { | ||
val schemaUrlOptEither = (other.schemaUrl, schemaUrl) match { | ||
case (Some(otherUrl), Some(url)) => | ||
if (otherUrl == url) | ||
url.some.asRight | ||
else | ||
ResourceInitiationError.SchemaUrlConflict.asLeft | ||
case (otherUrl, url) => | ||
otherUrl.orElse(url).asRight | ||
} | ||
|
||
schemaUrlOptEither.map( | ||
Resource( | ||
other.attributes |+| attributes, | ||
_ | ||
) | ||
) | ||
} | ||
} | ||
|
||
/** Unsafe version of [[Resource.mergeInto]] which trows an exception if the | ||
kovstas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* merge fails. | ||
*/ | ||
def mergeIntoUnsafe(other: Resource): Resource = | ||
kovstas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
mergeInto(other).fold( | ||
throw _, | ||
identity | ||
) | ||
} | ||
|
||
object Resource { | ||
sealed trait ResourceInitiationError extends NoStackTrace | ||
kovstas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
object ResourceInitiationError { | ||
case object SchemaUrlConflict extends ResourceInitiationError | ||
} | ||
|
||
def apply(attributes: Attributes): Resource = | ||
Resource(attributes, None) | ||
|
||
/** Returns an empty [[Resource]]. It is strongly recommended to start with | ||
* [[Resource.Default]] instead of this method to include SDK required | ||
* attributes. | ||
* | ||
* @return | ||
* an empty <pre>Resource</pre>. | ||
kovstas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
val Empty: Resource = Resource(Attributes.Empty) | ||
|
||
private val TelemetrySdk: Resource = Resource( | ||
Attributes( | ||
Attribute(TelemetrySdkName, "otel4s"), | ||
Attribute(TelemetrySdkLanguage, TelemetrySdkLanguageValue.Scala.value), | ||
Attribute(TelemetrySdkVersion, BuildInfo.version) | ||
) | ||
) | ||
|
||
private val Mandatory: Resource = Resource( | ||
Attributes( | ||
Attribute(ServiceName, "unknown_service:scala") | ||
) | ||
) | ||
|
||
/** Returns the default [[Resource]]. This resource contains the default | ||
* attributes provided by the SDK. | ||
* | ||
* @return | ||
* a <pre>Resource</pre>. | ||
*/ | ||
val Default: Resource = TelemetrySdk.mergeIntoUnsafe(Mandatory) | ||
|
||
implicit val showResource: Show[Resource] = | ||
r => show"Resource(${r.attributes}, ${r.schemaUrl})" | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently, most methods use varargs, e.g:
I wonder whether we should offer a new alternative too:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be honest, I would completely replace Attribute with Attributes. Attribute looks like an unnecessary wrapper to me because, from my perspective, users are more interested in using a collection that provides interfaces for accessing particular values by keys or iterating over all of them. I can imagine that It might be useful as a smart constructor which validates passed values, but the collection can do the same. Maybe I don't see a use-case for it.