-
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d1e7376
core-common and sdk-common: Add `Hash` and `Show` instances
iRevive 282da32
Use cats-discipline to test `Hash` laws
iRevive 571900a
Add more tests
iRevive a7a78e2
Merge branch 'upstream-main' into hash-show-instances
iRevive 8e79f83
Use `import cats.syntax.all._`
iRevive 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
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
66 changes: 66 additions & 0 deletions
66
core/trace/src/test/scala/org/typelevel/otel4s/trace/SpanKindSuite.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,66 @@ | ||
/* | ||
* 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.Hash | ||
import cats.Show | ||
import munit._ | ||
import org.scalacheck.Gen | ||
import org.scalacheck.Prop | ||
|
||
class SpanKindSuite extends ScalaCheckSuite { | ||
|
||
private val spanKindGen: Gen[SpanKind] = | ||
Gen.oneOf( | ||
SpanKind.Internal, | ||
SpanKind.Server, | ||
SpanKind.Client, | ||
SpanKind.Producer, | ||
SpanKind.Consumer | ||
) | ||
|
||
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) | ||
} | ||
} | ||
|
||
test("Hash[SpanKind]") { | ||
iRevive marked this conversation as resolved.
Show resolved
Hide resolved
|
||
val allWithIndex = List( | ||
SpanKind.Internal, | ||
SpanKind.Server, | ||
SpanKind.Client, | ||
SpanKind.Producer, | ||
SpanKind.Consumer | ||
).zipWithIndex | ||
|
||
allWithIndex.foreach { case (that, thatIdx) => | ||
allWithIndex.foreach { case (other, otherIdx) => | ||
assertEquals(Hash[SpanKind].eqv(that, other), thatIdx == otherIdx) | ||
} | ||
} | ||
} | ||
|
||
} |
52 changes: 52 additions & 0 deletions
52
core/trace/src/test/scala/org/typelevel/otel4s/trace/StatusSuite.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,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.Hash | ||
import cats.Show | ||
import munit._ | ||
import org.scalacheck.Gen | ||
import org.scalacheck.Prop | ||
|
||
class StatusSuite extends ScalaCheckSuite { | ||
|
||
private val statusGen: Gen[Status] = | ||
Gen.oneOf(Status.Unset, Status.Ok, Status.Error) | ||
|
||
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) | ||
} | ||
} | ||
|
||
test("Hash[Status]") { | ||
val allWithIndex = List(Status.Unset, Status.Ok, Status.Error).zipWithIndex | ||
|
||
allWithIndex.foreach { case (that, thatIdx) => | ||
allWithIndex.foreach { case (other, otherIdx) => | ||
assertEquals(Hash[Status].eqv(that, other), thatIdx == otherIdx) | ||
} | ||
} | ||
} | ||
|
||
} |
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
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.
This is a bit weird, we definitely don't want to keep it gated in a testkit or behind an extra import?
Either way,
Any
is distinct from_
which I think is called "existential".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.
I guess we can keep it behind the certain import.
There are several places in (upcoming) Otel4s SDK where we don't know a concrete type of an Attritube
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.
Could you suggest the object's name where I can place this implicit, please?
Should the structure be similar to something like this?:
Extra / Existential / Unsafe / Internal?
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.
Hm, we already define some
Show[Attribute[_]]
instances in the public implicit lookup scope:otel4s/core/common/src/main/scala/org/typelevel/otel4s/Attribute.scala
Line 73 in 571900a
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.
@armanbilge what are the implications if we keep existential instances in the public scope?
We need existential instances for:
AttributeType[_]
- a sealed trait with case objects, universal hashcode is fine hereAttributeKey[_]
- created fromkey => (key.name, key.type)
, where thename
is aString
and thetype
is anAttributeType[_]
(a coproduct)Attribute[_]
- created froma => (a.key, a.value)
, where thekey
isAttributeKey
, thevalue
is one of Boolean, Double, String, Long, List[Boolean], List[Double], List[String], List[Long].The
Hash[Attribute[List[String]]]
should generate the same hashcode asHash[Attribute[_]]
.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.
If we are confident that universal hashing will work for all the possible types (now and future) then it seems fine. e.g. could there ever be an attribute for a
CIString
?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.
Spec says the following:
I don't think we should allow any other types other than these.
So, I doubt
Attribute[CIString]
should be a thing.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.
regardless,
CIString
has a case-insensitivehashCode
implementation, so universal hashing should still work anywayThere 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.
Yes, it does today, but I'm considering a future where we might want to make it an
opaque type
.