-
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
sdk-trace: add EventData
#369
Merged
Merged
Changes from all commits
Commits
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
158 changes: 158 additions & 0 deletions
158
sdk/trace/src/main/scala/org/typelevel/otel4s/sdk/trace/data/EventData.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,158 @@ | ||
/* | ||
* 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 | ||
package trace.data | ||
|
||
import cats.Hash | ||
import cats.Show | ||
import cats.syntax.show._ | ||
import org.typelevel.otel4s.Attribute | ||
import org.typelevel.otel4s.semconv.trace.attributes.SemanticAttributes | ||
|
||
import java.io.PrintWriter | ||
import java.io.StringWriter | ||
import scala.concurrent.duration.FiniteDuration | ||
|
||
/** Data representation of an event. | ||
* | ||
* @see | ||
* [[https://opentelemetry.io/docs/specs/otel/trace/api/#add-events]] | ||
*/ | ||
sealed trait EventData { | ||
|
||
/** The name of the event. | ||
*/ | ||
def name: String | ||
|
||
/** The timestamp of the event. | ||
*/ | ||
def timestamp: FiniteDuration | ||
|
||
/** The attributes of the event. | ||
*/ | ||
def attributes: Attributes | ||
|
||
override final def hashCode(): Int = | ||
Hash[EventData].hash(this) | ||
|
||
override final def equals(obj: Any): Boolean = | ||
obj match { | ||
case other: EventData => Hash[EventData].eqv(this, other) | ||
case _ => false | ||
} | ||
|
||
override final def toString: String = | ||
Show[EventData].show(this) | ||
} | ||
|
||
object EventData { | ||
private final val ExceptionEventName = "exception" | ||
|
||
/** Creates [[EventData]] with the given arguments. | ||
* | ||
* @param name | ||
* the name of the event | ||
* | ||
* @param timestamp | ||
* the timestamp of the event | ||
* | ||
* @param attributes | ||
* the attributes to associate with the event | ||
*/ | ||
def apply( | ||
name: String, | ||
timestamp: FiniteDuration, | ||
attributes: Attributes | ||
): EventData = | ||
Impl(name, timestamp, attributes) | ||
|
||
/** Creates [[EventData]] from the given exception. | ||
* | ||
* The name of the even will be `exception`. | ||
* | ||
* Exception details (name, message, and stacktrace) will be added to the | ||
* attributes. | ||
* | ||
* @param timestamp | ||
* the timestamp of the event | ||
* | ||
* @param exception | ||
* the exception to associate with the event | ||
* | ||
* @param attributes | ||
* the attributes to associate with the event | ||
* | ||
* @param escaped | ||
* should be set to true if the exception is recorded at a point where it | ||
* is known that the exception is escaping the scope of the span | ||
*/ | ||
def fromException( | ||
timestamp: FiniteDuration, | ||
exception: Throwable, | ||
attributes: Attributes, | ||
escaped: Boolean | ||
): EventData = { | ||
val allAttributes = { | ||
val builder = Vector.newBuilder[Attribute[_]] | ||
|
||
builder.addOne( | ||
Attribute(SemanticAttributes.ExceptionType, exception.getClass.getName) | ||
) | ||
|
||
val message = exception.getMessage | ||
if (message != null) { | ||
builder.addOne(Attribute(SemanticAttributes.ExceptionMessage, message)) | ||
} | ||
|
||
if (exception.getStackTrace.nonEmpty) { | ||
val stringWriter = new StringWriter() | ||
val printWriter = new PrintWriter(stringWriter) | ||
exception.printStackTrace(printWriter) | ||
|
||
builder.addOne( | ||
Attribute( | ||
SemanticAttributes.ExceptionStacktrace, | ||
stringWriter.toString | ||
) | ||
) | ||
} | ||
|
||
builder.addOne(Attribute(SemanticAttributes.ExceptionEscaped, escaped)) | ||
|
||
builder.addAll(attributes.toList) | ||
|
||
Attributes(builder.result(): _*) | ||
} | ||
|
||
Impl(ExceptionEventName, timestamp, allAttributes) | ||
} | ||
|
||
implicit val eventDataHash: Hash[EventData] = | ||
Hash.by(data => (data.name, data.timestamp, data.attributes)) | ||
|
||
implicit val eventDataShow: Show[EventData] = | ||
Show.show { data => | ||
show"EventData{name=${data.name}, timestamp=${data.timestamp}, attributes=${data.attributes}}" | ||
} | ||
|
||
private final case class Impl( | ||
name: String, | ||
timestamp: FiniteDuration, | ||
attributes: Attributes | ||
) extends EventData | ||
|
||
} |
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
101 changes: 101 additions & 0 deletions
101
sdk/trace/src/test/scala/org/typelevel/otel4s/sdk/trace/data/EventDataSuite.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,101 @@ | ||
/* | ||
* 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.trace | ||
package data | ||
|
||
import cats.Show | ||
import cats.kernel.laws.discipline.HashTests | ||
import cats.syntax.monoid._ | ||
import cats.syntax.show._ | ||
import munit.DisciplineSuite | ||
import org.scalacheck.Arbitrary | ||
import org.scalacheck.Gen | ||
import org.scalacheck.Prop | ||
import org.typelevel.otel4s.Attribute | ||
import org.typelevel.otel4s.sdk.Attributes | ||
|
||
import java.io.PrintWriter | ||
import java.io.StringWriter | ||
import scala.util.control.NoStackTrace | ||
|
||
class EventDataSuite extends DisciplineSuite { | ||
import Cogens.eventDataCogen | ||
|
||
private implicit val eventDataArbitrary: Arbitrary[EventData] = | ||
Arbitrary(Gens.eventData) | ||
|
||
checkAll("EventData.HashLaws", HashTests[EventData].hash) | ||
|
||
test("Show[EventData]") { | ||
Prop.forAll(Gens.eventData) { data => | ||
val expected = | ||
show"EventData{name=${data.name}, timestamp=${data.timestamp}, attributes=${data.attributes}}" | ||
|
||
assertEquals(Show[EventData].show(data), expected) | ||
} | ||
} | ||
|
||
test("create EventData with given arguments") { | ||
Prop.forAll(Gens.eventData) { data => | ||
assertEquals(EventData(data.name, data.timestamp, data.attributes), data) | ||
} | ||
} | ||
|
||
test("create EventData from an exception") { | ||
Prop.forAll(Gen.finiteDuration, Gens.attributes) { (ts, attributes) => | ||
val exception = new RuntimeException("This is fine") | ||
|
||
val stringWriter = new StringWriter() | ||
val printWriter = new PrintWriter(stringWriter) | ||
exception.printStackTrace(printWriter) | ||
|
||
val expectedAttributes = Attributes( | ||
Attribute("exception.type", exception.getClass.getName), | ||
Attribute("exception.message", exception.getMessage), | ||
Attribute("exception.stacktrace", stringWriter.toString), | ||
Attribute("exception.escaped", true) | ||
) |+| attributes | ||
|
||
val data = EventData.fromException(ts, exception, attributes, true) | ||
|
||
assertEquals(data.name, "exception") | ||
assertEquals(data.timestamp, ts) | ||
assertEquals(data.attributes, expectedAttributes) | ||
} | ||
} | ||
|
||
test("create EventData from an exception (no message, no stack trace)") { | ||
Prop.forAll(Gen.finiteDuration, Gens.attributes) { (ts, attributes) => | ||
val exception = new RuntimeException with NoStackTrace | ||
|
||
val stringWriter = new StringWriter() | ||
val printWriter = new PrintWriter(stringWriter) | ||
exception.printStackTrace(printWriter) | ||
|
||
val expectedAttributes = Attributes( | ||
Attribute("exception.type", exception.getClass.getName), | ||
Attribute("exception.escaped", false) | ||
) |+| attributes | ||
|
||
val data = EventData.fromException(ts, exception, attributes, false) | ||
|
||
assertEquals(data.name, "exception") | ||
assertEquals(data.timestamp, ts) | ||
assertEquals(data.attributes, expectedAttributes) | ||
} | ||
} | ||
} |
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.
I feel like there's a lot of inefficient conversions here:
toList
,.result()
,: _*
...It can be a follow-up, but I wonder if we can either have a mutable
AttributesBuilder
that is backed by aMapBuilder
or we could haveAttributes.unsafeFromMap
style builder.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.
Yeah, we should explore the alternatives.