-
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 SpanData
#372
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
228 changes: 228 additions & 0 deletions
228
sdk/trace/src/main/scala/org/typelevel/otel4s/sdk/trace/data/SpanData.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,228 @@ | ||
/* | ||
* 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 | ||
package data | ||
|
||
import cats.Hash | ||
import cats.Show | ||
import cats.syntax.foldable._ | ||
import cats.syntax.show._ | ||
import org.typelevel.otel4s.sdk.common.InstrumentationScope | ||
import org.typelevel.otel4s.trace.SpanContext | ||
import org.typelevel.otel4s.trace.SpanKind | ||
|
||
import scala.concurrent.duration.FiniteDuration | ||
|
||
/** Immutable representation of all data collected by the | ||
* [[org.typelevel.otel4s.trace.Span Span]]. | ||
* | ||
* @see | ||
* [[https://opentelemetry.io/docs/specs/otel/trace/api/#span]] | ||
*/ | ||
sealed trait SpanData { | ||
|
||
/** The name of the span. | ||
*/ | ||
def name: String | ||
|
||
/** The span context associated with this span. | ||
*/ | ||
def spanContext: SpanContext | ||
|
||
/** The parent span context, if any. | ||
*/ | ||
def parentSpanContext: Option[SpanContext] | ||
|
||
/** The kind of the span. | ||
*/ | ||
def kind: SpanKind | ||
|
||
/** The start timestamp of the span. | ||
*/ | ||
def startTimestamp: FiniteDuration | ||
|
||
/** The end time of the span. | ||
*/ | ||
def endTimestamp: Option[FiniteDuration] | ||
|
||
/** The status data of the span. | ||
*/ | ||
def status: StatusData | ||
|
||
/** The attributes associated with the span. | ||
*/ | ||
def attributes: Attributes | ||
|
||
/** The events associated with the span. | ||
*/ | ||
def events: List[EventData] | ||
|
||
/** The links associated with the span. | ||
*/ | ||
def links: List[LinkData] | ||
|
||
/** The instrumentation scope associated with the span. | ||
*/ | ||
def instrumentationScope: InstrumentationScope | ||
|
||
/** The resource associated with the span. | ||
*/ | ||
def resource: Resource | ||
|
||
/** Whether the span has ended. | ||
*/ | ||
final def hasEnded: Boolean = endTimestamp.isDefined | ||
|
||
override final def hashCode(): Int = | ||
Hash[SpanData].hash(this) | ||
|
||
override final def equals(obj: Any): Boolean = | ||
obj match { | ||
case other: SpanData => Hash[SpanData].eqv(this, other) | ||
case _ => false | ||
} | ||
|
||
override final def toString: String = | ||
Show[SpanData].show(this) | ||
} | ||
|
||
object SpanData { | ||
|
||
/** Creates [[SpanData]] with the given arguments. | ||
* | ||
* @param name | ||
* the name of the span | ||
* | ||
* @param spanContext | ||
* the span context associated with the span | ||
* | ||
* @param parentSpanContext | ||
* an optional parent span context. Use `None` if there is no parent span | ||
* context | ||
* | ||
* @param kind | ||
* the kind of the span | ||
* | ||
* @param startTimestamp | ||
* the start timestamp of the span | ||
* | ||
* @param endTimestamp | ||
* the end timestamp of the span. Use `None` is the span hasn't ended yet | ||
* | ||
* @param status | ||
* the status data of the span | ||
* | ||
* @param attributes | ||
* the attributes associated with the span | ||
* | ||
* @param events | ||
* the events associated with the span | ||
* | ||
* @param links | ||
* the links associated with the span | ||
* | ||
* @param instrumentationScope | ||
* the instrumentation scope associated with the span | ||
* | ||
* @param resource | ||
* the resource associated with the span | ||
*/ | ||
def apply( | ||
name: String, | ||
spanContext: SpanContext, | ||
parentSpanContext: Option[SpanContext], | ||
kind: SpanKind, | ||
startTimestamp: FiniteDuration, | ||
endTimestamp: Option[FiniteDuration], | ||
status: StatusData, | ||
attributes: Attributes, | ||
events: List[EventData], | ||
links: List[LinkData], | ||
instrumentationScope: InstrumentationScope, | ||
resource: Resource | ||
): SpanData = | ||
Impl( | ||
name = name, | ||
spanContext = spanContext, | ||
parentSpanContext = parentSpanContext, | ||
kind = kind, | ||
startTimestamp = startTimestamp, | ||
endTimestamp = endTimestamp, | ||
status = status, | ||
attributes = attributes, | ||
events = events, | ||
links = links, | ||
instrumentationScope = instrumentationScope, | ||
resource = resource | ||
) | ||
|
||
implicit val spanDataHash: Hash[SpanData] = | ||
Hash.by { data => | ||
( | ||
data.name, | ||
data.spanContext, | ||
data.parentSpanContext, | ||
data.kind, | ||
data.startTimestamp, | ||
data.endTimestamp, | ||
data.status, | ||
data.attributes, | ||
data.events, | ||
data.links, | ||
data.instrumentationScope, | ||
data.resource | ||
) | ||
} | ||
|
||
implicit val spanDataShow: Show[SpanData] = | ||
Show.show { data => | ||
val endTimestamp = | ||
data.endTimestamp.foldMap(ts => show"endTimestamp=$ts, ") | ||
|
||
show"SpanData{" + | ||
show"name=${data.name}, " + | ||
show"spanContext=${data.spanContext}, " + | ||
show"parentSpanContext=${data.parentSpanContext}, " + | ||
show"kind=${data.kind}, " + | ||
show"startTimestamp=${data.startTimestamp}, " + | ||
endTimestamp + | ||
show"hasEnded=${data.hasEnded}, " + | ||
show"status=${data.status}, " + | ||
show"attributes=${data.attributes}, " + | ||
show"events=${data.events}, " + | ||
show"links=${data.links}, " + | ||
show"instrumentationScope=${data.instrumentationScope}, " + | ||
show"resource=${data.resource}}" | ||
} | ||
|
||
private final case class Impl( | ||
name: String, | ||
spanContext: SpanContext, | ||
parentSpanContext: Option[SpanContext], | ||
kind: SpanKind, | ||
startTimestamp: FiniteDuration, | ||
endTimestamp: Option[FiniteDuration], | ||
status: StatusData, | ||
attributes: Attributes, | ||
events: List[EventData], | ||
links: List[LinkData], | ||
instrumentationScope: InstrumentationScope, | ||
resource: Resource | ||
) extends SpanData | ||
|
||
} |
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
Oops, something went wrong.
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.
We can use shorter alternatives:
WDYT?
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 isn't really user-facing tho right? I think I prefer the current names.