-
Notifications
You must be signed in to change notification settings - Fork 38
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 and sdk: Add Hash
and Show
instances
#331
Changes from 4 commits
d1e7376
282da32
571900a
a7a78e2
8e79f83
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* 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.trace | ||
|
||
import cats.Show | ||
import cats.kernel.laws.discipline.HashTests | ||
import munit._ | ||
import org.scalacheck.Arbitrary | ||
import org.scalacheck.Cogen | ||
import org.scalacheck.Gen | ||
import org.scalacheck.Prop | ||
|
||
class SpanKindSuite extends DisciplineSuite { | ||
|
||
private val spanKindGen: Gen[SpanKind] = | ||
Gen.oneOf( | ||
SpanKind.Internal, | ||
SpanKind.Server, | ||
SpanKind.Client, | ||
SpanKind.Producer, | ||
SpanKind.Consumer | ||
) | ||
|
||
private implicit val spanKindArbitrary: Arbitrary[SpanKind] = | ||
Arbitrary(spanKindGen) | ||
|
||
private implicit val spanKindCogen: Cogen[SpanKind] = | ||
Cogen[String].contramap(_.toString) | ||
|
||
checkAll("SpanKind.HashLaws", HashTests[SpanKind].hash) | ||
|
||
property("Show[SpanKind]") { | ||
Prop.forAll(spanKindGen) { spanKind => | ||
val expected = spanKind match { | ||
case SpanKind.Internal => "Internal" | ||
case SpanKind.Server => "Server" | ||
case SpanKind.Client => "Client" | ||
case SpanKind.Producer => "Producer" | ||
case SpanKind.Consumer => "Consumer" | ||
} | ||
|
||
assertEquals(Show[SpanKind].show(spanKind), expected) | ||
} | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* 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.trace | ||
|
||
import cats.Show | ||
import cats.kernel.laws.discipline.HashTests | ||
import munit._ | ||
import org.scalacheck.Arbitrary | ||
import org.scalacheck.Cogen | ||
import org.scalacheck.Gen | ||
import org.scalacheck.Prop | ||
|
||
class StatusSuite extends DisciplineSuite { | ||
|
||
private val statusGen: Gen[Status] = | ||
Gen.oneOf(Status.Unset, Status.Ok, Status.Error) | ||
|
||
private implicit val statusArbitrary: Arbitrary[Status] = | ||
Arbitrary(statusGen) | ||
|
||
private implicit val statusCogen: Cogen[Status] = | ||
Cogen[String].contramap(_.toString) | ||
|
||
checkAll("Status.HashLaws", HashTests[Status].hash) | ||
|
||
property("Show[Status]") { | ||
Prop.forAll(statusGen) { status => | ||
val expected = status match { | ||
case Status.Unset => "Unset" | ||
case Status.Ok => "Ok" | ||
case Status.Error => "Error" | ||
} | ||
|
||
assertEquals(Show[Status].show(status), expected) | ||
} | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,10 +17,14 @@ | |
package org.typelevel.otel4s.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 { | ||
|
@@ -90,4 +94,45 @@ trait ArbitraryInstances { | |
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)) | ||
} | ||
|
||
Comment on lines
+108
to
+129
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @armanbilge I didn't use Do I understand this in the right way:
Is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I think your implementation looks good, typically I try to avoid the manual plumbing of seeds and instead transform to some datatype for which Either[
Boolean,
Either[
Double,
Either[
String,
Either[
Long,
Either[...]
]
]
]
] |
||
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) | ||
} | ||
|
||
} |
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, the attribute value can exactly be one of:
Boolean
,Double
,String
,Long
,List[Boolean]
,List[Double]
,List[String]
,List[Long]
.So using a universal hashcode should be safe here.