-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #240 from kovstas/sdk-resource-class
Add Resource entity
- Loading branch information
Showing
13 changed files
with
684 additions
and
47 deletions.
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 { | ||
|
||
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) | ||
} | ||
} | ||
} |
125 changes: 125 additions & 0 deletions
125
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,125 @@ | ||
/* | ||
* 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._ | ||
|
||
/** [[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 throws an exception if the | ||
* merge fails. | ||
*/ | ||
private def mergeIntoUnsafe(other: Resource): Resource = | ||
mergeInto(other).fold( | ||
throw _, | ||
identity | ||
) | ||
} | ||
|
||
object Resource { | ||
sealed abstract class ResourceInitiationError extends Throwable | ||
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 [[Resource]]. | ||
*/ | ||
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 [[Resource]]. | ||
*/ | ||
val Default: Resource = TelemetrySdk.mergeIntoUnsafe(Mandatory) | ||
|
||
implicit val showResource: Show[Resource] = | ||
r => show"Resource(${r.attributes}, ${r.schemaUrl})" | ||
|
||
} |
Oops, something went wrong.