Skip to content

Commit

Permalink
Merge pull request #411 from NthPortal/core-attributes/PR
Browse files Browse the repository at this point in the history
Move `Attributes` to core-common
  • Loading branch information
NthPortal authored Dec 26, 2023
2 parents 65702c5 + 6552412 commit 30aae3c
Show file tree
Hide file tree
Showing 25 changed files with 177 additions and 155 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@
* limitations under the License.
*/

package org.typelevel.otel4s.benchmarks
package org.typelevel.otel4s
package benchmarks

import io.opentelemetry.api.common.{AttributeKey => JAttributeKey}
import io.opentelemetry.api.common.{Attributes => JAttributes}
import org.openjdk.jmh.annotations._
import org.typelevel.otel4s.Attribute
import org.typelevel.otel4s.AttributeKey
import org.typelevel.otel4s.sdk.Attributes

import java.util.concurrent.TimeUnit

Expand Down
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ lazy val `sdk-common` = crossProject(JVMPlatform, JSPlatform, NativePlatform)
.enablePlugins(BuildInfoPlugin)
.enablePlugins(NoPublishPlugin)
.in(file("sdk/common"))
.dependsOn(`core-common`, semconv)
.dependsOn(`core-common` % "test->test", semconv)
.settings(
name := "otel4s-sdk-common",
startYear := Some(2023),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 Typelevel
* Copyright 2022 Typelevel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -14,15 +14,13 @@
* limitations under the License.
*/

package org.typelevel.otel4s.sdk
package org.typelevel.otel4s

import cats.Hash
import cats.Monoid
import cats.Show
import cats.syntax.show._
import org.typelevel.otel4s.Attribute
import org.typelevel.otel4s.Attribute.KeySelect
import org.typelevel.otel4s.AttributeKey

import scala.collection.IterableOps
import scala.collection.SpecificIterableFactory
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
* Copyright 2022 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

import org.scalacheck.Arbitrary
import org.scalacheck.Cogen
import org.scalacheck.Gen
import org.scalacheck.Gen.listOf
import org.scalacheck.Gen.nonEmptyListOf
import org.scalacheck.rng.Seed

object arbitrary extends ArbitraryInstances
trait ArbitraryInstances {

val nonEmptyString: Gen[String] =
Arbitrary.arbitrary[String].suchThat(_.nonEmpty)

implicit val booleanAttribute: Gen[Attribute[Boolean]] = for {
b <- Arbitrary.arbitrary[Boolean]
k <- nonEmptyString
} yield Attribute(k, b)

implicit val stringAttribute: Gen[Attribute[String]] = for {
s <- nonEmptyString
k <- nonEmptyString
} yield Attribute(k, s)

implicit val longAttribute: Gen[Attribute[Long]] = for {
l <- Arbitrary.arbitrary[Long]
k <- nonEmptyString
} yield Attribute(k, l)

implicit val doubleAttribute: Gen[Attribute[Double]] = for {
d <- Arbitrary.arbitrary[Double]
k <- nonEmptyString
} yield Attribute(k, d)

implicit val stringListAttribute: Gen[Attribute[List[String]]] = for {
l <- nonEmptyListOf(nonEmptyString)
k <- nonEmptyString
} yield Attribute(k, l)

implicit val booleanListAttribute: Gen[Attribute[List[Boolean]]] = for {
l <- nonEmptyListOf(Arbitrary.arbitrary[Boolean])
k <- nonEmptyString
} yield Attribute(k, l)

implicit val longListAttribute: Gen[Attribute[List[Long]]] = for {
l <- nonEmptyListOf(Arbitrary.arbitrary[Long])
k <- nonEmptyString
} yield Attribute(k, l)

implicit val doubleListAttribute: Gen[Attribute[List[Double]]] = for {
l <- nonEmptyListOf(Arbitrary.arbitrary[Double])
k <- nonEmptyString
} yield Attribute(k, l)

implicit val attribute: Arbitrary[Attribute[_]] = Arbitrary(
Gen.oneOf(
booleanAttribute,
stringAttribute,
longAttribute,
doubleAttribute,
stringListAttribute,
booleanListAttribute,
longListAttribute,
doubleListAttribute
)
)

implicit val attributes: Arbitrary[Attributes] = Arbitrary(
listOf(attribute.arbitrary).map(Attributes(_: _*))
)

implicit val attributeTypeCogen: Cogen[AttributeType[_]] =
Cogen[String].contramap(_.toString)

implicit def attributeKeyCogen[A]: Cogen[AttributeKey[A]] =
Cogen[(String, String)].contramap[AttributeKey[A]] { attribute =>
(attribute.name, attribute.`type`.toString)
}

implicit def attributeCogen[A: Cogen]: Cogen[Attribute[A]] =
Cogen[(AttributeKey[A], A)].contramap(a => (a.key, a.value))

implicit val attributeExistentialCogen: Cogen[Attribute[_]] =
Cogen { (seed, attr) =>
def primitive[A: Cogen](seed: Seed): Seed =
Cogen[A].perturb(seed, attr.value.asInstanceOf[A])

def list[A: Cogen](seed: Seed): Seed =
Cogen[List[A]].perturb(seed, attr.value.asInstanceOf[List[A]])

val valueCogen: Seed => Seed = attr.key.`type` match {
case AttributeType.Boolean => primitive[Boolean]
case AttributeType.Double => primitive[Double]
case AttributeType.String => primitive[String]
case AttributeType.Long => primitive[Long]
case AttributeType.BooleanList => list[Boolean]
case AttributeType.DoubleList => list[Double]
case AttributeType.StringList => list[String]
case AttributeType.LongList => list[Long]
}

valueCogen(attributeKeyCogen.perturb(seed, attr.key))
}

implicit val attributesCogen: Cogen[Attributes] =
Cogen[List[Attribute[_]]].contramap(_.toList)
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 Typelevel
* Copyright 2022 Typelevel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -14,12 +14,12 @@
* limitations under the License.
*/

package org.typelevel.otel4s.sdk
package org.typelevel.otel4s

import cats.kernel.laws.discipline.HashTests
import cats.kernel.laws.discipline.MonoidTests
import munit.DisciplineSuite
import org.typelevel.otel4s.sdk.arbitrary._
import org.typelevel.otel4s.arbitrary._

class AttributesLawTests extends DisciplineSuite {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 Typelevel
* Copyright 2022 Typelevel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -14,17 +14,16 @@
* limitations under the License.
*/

package org.typelevel.otel4s.sdk
package org.typelevel.otel4s

import cats.Show
import cats.syntax.semigroup._
import munit.ScalaCheckSuite
import org.scalacheck.Arbitrary
import org.scalacheck.Gen
import org.scalacheck.Prop.forAll
import org.typelevel.otel4s.Attribute
import org.typelevel.otel4s.sdk.arbitrary.attribute
import org.typelevel.otel4s.sdk.arbitrary.attributes
import org.typelevel.otel4s.arbitrary.attribute
import org.typelevel.otel4s.arbitrary.attributes

class AttributesProps extends ScalaCheckSuite {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
* limitations under the License.
*/

package org.typelevel.otel4s.sdk
package org.typelevel.otel4s
package sdk

import cats.Hash
import cats.Show
import cats.syntax.all._
import org.typelevel.otel4s.Attribute
import org.typelevel.otel4s.sdk.Resource.ResourceInitializationError
import org.typelevel.otel4s.semconv.resource.attributes.ResourceAttributes._

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
* limitations under the License.
*/

package org.typelevel.otel4s.sdk
package org.typelevel.otel4s
package sdk
package common

import cats.Hash
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,122 +14,22 @@
* limitations under the License.
*/

package org.typelevel.otel4s.sdk
package org.typelevel
package otel4s
package sdk

import org.scalacheck.Arbitrary
import org.scalacheck.Cogen
import org.scalacheck.Gen
import org.scalacheck.Gen.listOf
import org.scalacheck.Gen.nonEmptyListOf
import org.scalacheck.rng.Seed
import org.typelevel.otel4s.Attribute
import org.typelevel.otel4s.AttributeKey
import org.typelevel.otel4s.AttributeType

object arbitrary extends ArbitraryInstances
trait ArbitraryInstances {

val nonEmptyString: Gen[String] =
Arbitrary.arbitrary[String].suchThat(_.nonEmpty)

implicit val booleanAttribute: Gen[Attribute[Boolean]] = for {
b <- Arbitrary.arbitrary[Boolean]
k <- nonEmptyString
} yield Attribute(k, b)

implicit val stringAttribute: Gen[Attribute[String]] = for {
s <- nonEmptyString
k <- nonEmptyString
} yield Attribute(k, s)

implicit val longAttribute: Gen[Attribute[Long]] = for {
l <- Arbitrary.arbitrary[Long]
k <- nonEmptyString
} yield Attribute(k, l)

implicit val doubleAttribute: Gen[Attribute[Double]] = for {
d <- Arbitrary.arbitrary[Double]
k <- nonEmptyString
} yield Attribute(k, d)

implicit val stringListAttribute: Gen[Attribute[List[String]]] = for {
l <- nonEmptyListOf(nonEmptyString)
k <- nonEmptyString
} yield Attribute(k, l)

implicit val booleanListAttribute: Gen[Attribute[List[Boolean]]] = for {
l <- nonEmptyListOf(Arbitrary.arbitrary[Boolean])
k <- nonEmptyString
} yield Attribute(k, l)

implicit val longListAttribute: Gen[Attribute[List[Long]]] = for {
l <- nonEmptyListOf(Arbitrary.arbitrary[Long])
k <- nonEmptyString
} yield Attribute(k, l)

implicit val doubleListAttribute: Gen[Attribute[List[Double]]] = for {
l <- nonEmptyListOf(Arbitrary.arbitrary[Double])
k <- nonEmptyString
} yield Attribute(k, l)

implicit val attribute: Arbitrary[Attribute[_]] = Arbitrary(
Gen.oneOf(
booleanAttribute,
stringAttribute,
longAttribute,
doubleAttribute,
stringListAttribute,
booleanListAttribute,
longListAttribute,
doubleListAttribute
)
)

implicit val attributes: Arbitrary[Attributes] = Arbitrary(
listOf(attribute.arbitrary).map(Attributes(_: _*))
)
trait ArbitraryInstances extends otel4s.ArbitraryInstances {

implicit val resource: Arbitrary[Resource] = Arbitrary(for {
attrs <- attributes.arbitrary
schemaUrl <- Gen.option(nonEmptyString)
} yield Resource(attrs, schemaUrl))

implicit val attributeTypeCogen: Cogen[AttributeType[_]] =
Cogen[String].contramap(_.toString)

implicit def attributeKeyCogen[A]: Cogen[AttributeKey[A]] =
Cogen[(String, String)].contramap[AttributeKey[A]] { attribute =>
(attribute.name, attribute.`type`.toString)
}

implicit def attributeCogen[A: Cogen]: Cogen[Attribute[A]] =
Cogen[(AttributeKey[A], A)].contramap(a => (a.key, a.value))

implicit val attributeExistentialCogen: Cogen[Attribute[_]] =
Cogen { (seed, attr) =>
def primitive[A: Cogen](seed: Seed): Seed =
Cogen[A].perturb(seed, attr.value.asInstanceOf[A])

def list[A: Cogen](seed: Seed): Seed =
Cogen[List[A]].perturb(seed, attr.value.asInstanceOf[List[A]])

val valueCogen: Seed => Seed = attr.key.`type` match {
case AttributeType.Boolean => primitive[Boolean]
case AttributeType.Double => primitive[Double]
case AttributeType.String => primitive[String]
case AttributeType.Long => primitive[Long]
case AttributeType.BooleanList => list[Boolean]
case AttributeType.DoubleList => list[Double]
case AttributeType.StringList => list[String]
case AttributeType.LongList => list[Long]
}

valueCogen(attributeKeyCogen.perturb(seed, attr.key))
}

implicit val attributesCogen: Cogen[Attributes] =
Cogen[List[Attribute[_]]].contramap(_.toList)

implicit val resourceCogen: Cogen[Resource] =
Cogen[(Attributes, Option[String])].contramap { r =>
(r.attributes, r.schemaUrl)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
* limitations under the License.
*/

package org.typelevel.otel4s.sdk.common
package org.typelevel.otel4s
package sdk.common

import cats.Show
import cats.kernel.laws.discipline.HashTests
Expand All @@ -24,7 +25,6 @@ import org.scalacheck.Arbitrary
import org.scalacheck.Cogen
import org.scalacheck.Gen
import org.scalacheck.Prop
import org.typelevel.otel4s.sdk.Attributes
import org.typelevel.otel4s.sdk.arbitrary.{attributes => attributesArbitrary}
import org.typelevel.otel4s.sdk.arbitrary.attributesCogen

Expand Down
Loading

0 comments on commit 30aae3c

Please sign in to comment.