Skip to content
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

Bump OpenTelemetry version to 1.0 and fix SpanData Resource #178

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 44 additions & 0 deletions money-api/src/main/java/com/comcast/money/api/EventInfo.java
@@ -0,0 +1,44 @@
/*
* Copyright 2012 Comcast Cable Communications Management, LLC
*
* 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 com.comcast.money.api;

import io.opentelemetry.api.common.Attributes;

/**
* An event that was recorded on a {@link Span}.
*/
public interface EventInfo {
/**
* @return the name of the event
*/
String name();

/**
* @return the attributes recorded on the event
*/
Attributes attributes();

/**
* @return the timestamp of when the event occurred in nanoseconds since the epoch
*/
long timestamp();

/**
* @return an exception if one was recorded with the event; otherwise {@code null}
*/
Throwable exception();
}
Expand Up @@ -22,7 +22,7 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class IdGenerator {
public final class IdGenerator {
private IdGenerator() { }

public static final String INVALID_TRACE_ID = "00000000-0000-0000-0000-000000000000";
Expand Down
Expand Up @@ -18,7 +18,7 @@

import java.util.Objects;

public class InstrumentationLibrary {
public final class InstrumentationLibrary {
public static final InstrumentationLibrary UNKNOWN = new InstrumentationLibrary("unknown");

private final String name;
Expand Down
38 changes: 38 additions & 0 deletions money-api/src/main/java/com/comcast/money/api/LinkInfo.java
@@ -0,0 +1,38 @@
/*
* Copyright 2012 Comcast Cable Communications Management, LLC
*
* 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 com.comcast.money.api;

import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanContext;

/**
* A reference to another {@link Span} by span context.
* <p>
* Can be used to associate multiple traces as a part of a batch operation.
*/
public interface LinkInfo {
/**
* @return the context of the linked span
*/
SpanContext spanContext();

/**
* @return the attributes associated with the link between the spans
*/
Attributes attributes();
}
2 changes: 1 addition & 1 deletion money-api/src/main/java/com/comcast/money/api/Note.java
Expand Up @@ -26,7 +26,7 @@
*
* @param <T> The type of Note. This is currently limited to Long, String, Boolean and Double
*/
public class Note<T> {
public final class Note<T> {

private final AttributeKey<T> key;
private final T value;
Expand Down
7 changes: 1 addition & 6 deletions money-api/src/main/java/com/comcast/money/api/Span.java
Expand Up @@ -32,16 +32,11 @@
*/
public interface Span extends io.opentelemetry.api.trace.Span, Scope {

/**
* Stops the span asserts a successful result
*/
void stop();

/**
* Ends a span, moving it to a Stopped state
* @param result The result of the span (success or failure)
*/
void stop(Boolean result);
void end(boolean result);

@Override
Span setAttribute(String key, String value);
Expand Down
48 changes: 2 additions & 46 deletions money-api/src/main/java/com/comcast/money/api/SpanInfo.java
Expand Up @@ -21,11 +21,8 @@
import java.util.Map;
import java.util.concurrent.TimeUnit;

import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.api.trace.StatusCode;
import io.opentelemetry.api.trace.Span;

public interface SpanInfo {

Expand All @@ -38,14 +35,14 @@ public interface SpanInfo {
/**
* @return a list of all of the events that were recorded on the span.
*/
default List<Event> events() {
default List<EventInfo> events() {
return Collections.emptyList();
}

/**
* @return a list of the spans linked to the span
*/
default List<Link> links() {
default List<LinkInfo> links() {
return Collections.emptyList();
}

Expand Down Expand Up @@ -165,45 +162,4 @@ default long durationMicros() {
*/
String host();

/**
* An event that was recorded on a {@link com.comcast.money.api.Span}.
*/
interface Event {
/**
* @return the name of the event
*/
String name();

/**
* @return the attributes recorded on the event
*/
Attributes attributes();

/**
* @return the timestamp of when the event occurred in nanoseconds since the epoch
*/
long timestamp();

/**
* @return an exception if one was recorded with the event; otherwise {@code null}
*/
Throwable exception();
}

/**
* A reference to another {@link Span} by span context.
*
* Can be used to associate multiple traces as a part of a batch operation.
*/
interface Link {
/**
* @return the context of the linked span
*/
SpanContext spanContext();

/**
* @return the attributes associated with the link between the spans
*/
Attributes attributes();
}
}
Expand Up @@ -17,15 +17,15 @@
package com.comcast.money.core

import java.io.{ PrintWriter, StringWriter }
import com.comcast.money.api.SpanInfo
import com.comcast.money.api.{ EventInfo, SpanInfo }
import io.opentelemetry.api.common.Attributes
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes

private[core] case class CoreEvent(
private[core] case class CoreEventInfo(
name: String,
eventAttributes: Attributes,
timestamp: Long,
exception: Throwable) extends SpanInfo.Event {
exception: Throwable) extends EventInfo {

lazy val attributes: Attributes = initializeAttributes()

Expand Down
Expand Up @@ -16,10 +16,10 @@

package com.comcast.money.core

import com.comcast.money.api.SpanInfo
import com.comcast.money.api.{ SpanInfo, LinkInfo }
import io.opentelemetry.api.common.Attributes
import io.opentelemetry.api.trace.SpanContext

private[core] final case class CoreLink(
private[core] final case class CoreLinkInfo(
spanContext: SpanContext,
attributes: Attributes = Attributes.empty()) extends SpanInfo.Link
attributes: Attributes = Attributes.empty()) extends LinkInfo
57 changes: 27 additions & 30 deletions money-core/src/main/scala/com/comcast/money/core/CoreSpan.scala
Expand Up @@ -22,11 +22,12 @@ import com.comcast.money.api._

import scala.collection.JavaConverters._
import scala.collection.concurrent.TrieMap
import io.opentelemetry.api.trace.{ SpanContext, SpanKind, StatusCode, Span => OtelSpan }
import io.opentelemetry.api.trace.{ SpanContext, SpanKind, StatusCode }
import io.opentelemetry.api.common.{ AttributeKey, Attributes }
import io.opentelemetry.context.Scope
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes

import java.util.concurrent.atomic.AtomicBoolean
import scala.collection.mutable.ListBuffer

/**
Expand All @@ -40,7 +41,7 @@ private[core] case class CoreSpan(
id: SpanId,
var name: String,
kind: SpanKind = SpanKind.INTERNAL,
links: List[SpanInfo.Link] = Nil,
links: List[LinkInfo] = Nil,
startTimeNanos: Long = SystemClock.now,
library: InstrumentationLibrary = Money.InstrumentationLibrary,
clock: Clock = SystemClock,
Expand All @@ -51,40 +52,39 @@ private[core] case class CoreSpan(
private var description: String = _

// use concurrent maps
private val ended = new AtomicBoolean(false)
private val timers = new TrieMap[String, Long]()
private val noted = new TrieMap[String, Note[_]]()
private val events = new ListBuffer[SpanInfo.Event]()
private val events = new ListBuffer[EventInfo]()
private var scopes: List[Scope] = Nil

override def stop(): Unit = stop(clock.now, StatusCode.UNSET)
override def end(): Unit = end(clock.now, StatusCode.UNSET)
override def end(endTimeStamp: Long, unit: TimeUnit): Unit = end(unit.toNanos(endTimeStamp), StatusCode.UNSET)
override def end(result: Boolean): Unit = end(clock.now, if (result) StatusCode.OK else StatusCode.ERROR)

override def stop(result: java.lang.Boolean): Unit =
if (result == null) {
stop(clock.now, StatusCode.UNSET)
} else {
stop(clock.now, if (result) StatusCode.OK else StatusCode.ERROR)
}
private def end(endTimeNanos: Long, status: StatusCode): Unit =
if (ended.compareAndSet(false, true)) {
this.endTimeNanos = endTimeNanos

private def stop(endTimeNanos: Long, status: StatusCode): Unit = {
this.endTimeNanos = endTimeNanos
// process any hanging timers
val openTimers = timers.keys
openTimers.foreach(stopTimer)

// process any hanging timers
val openTimers = timers.keys
openTimers.foreach(stopTimer)
scopes.foreach {
_.close()
}
scopes = Nil

scopes.foreach { _.close() }
scopes = Nil
this.status = (this.status, status) match {
case (StatusCode.UNSET, StatusCode.UNSET) => StatusCode.OK
case (StatusCode.UNSET, other) => other
case (other, StatusCode.UNSET) => other
case (_, other) => other
}

this.status = (this.status, status) match {
case (StatusCode.UNSET, StatusCode.UNSET) => StatusCode.OK
case (StatusCode.UNSET, other) => other
case (other, StatusCode.UNSET) => other
case (_, other) => other
handler.handle(info())
}

handler.handle(info())
}

override def stopTimer(timerKey: String): Unit =
timers.remove(timerKey) foreach {
timerStartInstant =>
Expand Down Expand Up @@ -121,7 +121,7 @@ private[core] case class CoreSpan(
events = events.asJava,
links = links.asJava)

override def close(): Unit = stop()
override def close(): Unit = end()

override def setAttribute(attributeName: String, value: String): Span = record(Note.of(attributeName, value))
override def setAttribute(attributeName: String, value: scala.Long): Span = record(Note.of(attributeName, value))
Expand All @@ -144,7 +144,7 @@ private[core] case class CoreSpan(
}

private def addEventInternal(eventName: String, eventAttributes: Attributes, timestampNanos: scala.Long, exception: Throwable = null): Span = {
events += CoreEvent(eventName, eventAttributes, timestampNanos, exception)
events += CoreEventInfo(eventName, eventAttributes, timestampNanos, exception)
this
}

Expand All @@ -165,9 +165,6 @@ private[core] case class CoreSpan(
this
}

override def end(): Unit = stop()
override def `end`(endTimeStamp: Long, unit: TimeUnit): Unit = stop(unit.toNanos(endTimeStamp), StatusCode.UNSET)

override def getSpanContext: SpanContext = id.toSpanContext

override def isRecording: Boolean = startTimeNanos > 0 && endTimeNanos <= 0
Expand Down
Expand Up @@ -18,7 +18,7 @@ package com.comcast.money.core

import java.time.Instant
import java.util.concurrent.TimeUnit
import com.comcast.money.api.{ InstrumentationLibrary, Note, Span, SpanBuilder, SpanHandler, SpanId, SpanInfo }
import com.comcast.money.api.{ InstrumentationLibrary, Note, Span, SpanBuilder, SpanHandler, SpanId, SpanInfo, LinkInfo }
import com.comcast.money.core.samplers.{ DropResult, RecordResult, Sampler }
import io.opentelemetry.api.common.{ AttributeKey, Attributes }
import io.opentelemetry.context.Context
Expand All @@ -40,7 +40,7 @@ private[core] class CoreSpanBuilder(
var spanKind: SpanKind = SpanKind.INTERNAL
var startTimeNanos: Long = 0L
var notes: List[Note[_]] = List()
var links: List[SpanInfo.Link] = List()
var links: List[LinkInfo] = List()

override def setParent(context: Context): SpanBuilder = {
parentSpan = Option(context)
Expand Down Expand Up @@ -75,7 +75,7 @@ private[core] class CoreSpanBuilder(
override def addLink(spanContext: SpanContext): SpanBuilder = addLink(spanContext, Attributes.empty)

override def addLink(spanContext: SpanContext, attributes: Attributes): SpanBuilder = {
links = CoreLink(spanContext, attributes) :: links
links = CoreLinkInfo(spanContext, attributes) :: links
this
}

Expand Down