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

Support OTel resource via config and plumb through OtelSpanHandler adapter #196

Open
wants to merge 4 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
45 changes: 45 additions & 0 deletions money-api/src/main/java/com/comcast/money/api/Resource.java
@@ -0,0 +1,45 @@
/*
* 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;

/**
* Represents a resource, which capture identifying information about the entities
* for which traces are reported.
*/
public interface Resource {
/**
* @return the current application name
*/
String applicationName();

/**
* @return the host name or ip
*/
String hostName();

/**
* @return the tracing library
*/
InstrumentationLibrary library();

/**
* @return a map of attributes that describe the resource
*/
Attributes attributes();
}
7 changes: 6 additions & 1 deletion money-api/src/main/java/com/comcast/money/api/SpanInfo.java
Expand Up @@ -30,7 +30,7 @@
public interface SpanInfo {

/**
* @return a map of all of the notes that were recorded on the span. Implementers should enforce
* @return a map of all the notes that were recorded on the span. Implementers should enforce
* that the map returned is a copy of the notes
*/
Map<String, Note<?>> notes();
Expand All @@ -49,6 +49,11 @@ default List<Link> links() {
return Collections.emptyList();
}

/**
* @return the resource for this span
*/
Resource resource();

/**
* @return the time in milliseconds when this span was started
*/
Expand Down
Expand Up @@ -17,12 +17,11 @@
package com.comcast.money.api;

import io.opentelemetry.api.trace.SpanBuilder;
import io.opentelemetry.api.trace.Tracer;

/**
* OpenTelemetry compatible API to be used for tracing
*/
public interface MoneyTracer extends Tracer {
public interface Tracer extends io.opentelemetry.api.trace.Tracer {

/**
* {@inheritDoc}
Expand Down
@@ -0,0 +1,26 @@
/*
* 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.core

import com.comcast.money.api.{ InstrumentationLibrary, Resource }
import io.opentelemetry.api.common.Attributes

private[core] case class CoreResource(
applicationName: String,
hostName: String,
library: InstrumentationLibrary = InstrumentationLibrary.UNKNOWN,
attributes: Attributes = Attributes.empty()) extends Resource {}
Expand Up @@ -38,12 +38,12 @@ import scala.collection.mutable.ListBuffer
* @param handler The [[SpanHandler]] responsible for processing the span once it is stopped
*/
private[core] case class CoreSpan(
resource: Resource,
id: SpanId,
var name: String,
kind: SpanKind = SpanKind.INTERNAL,
links: List[SpanInfo.Link] = Nil,
startTimeNanos: Long = SystemClock.now,
library: InstrumentationLibrary = Money.InstrumentationLibrary,
clock: Clock = SystemClock,
handler: SpanHandler = DisabledSpanHandler) extends Span {

Expand Down Expand Up @@ -109,10 +109,10 @@ private[core] case class CoreSpan(

override def info(): SpanInfo =
CoreSpanInfo(
resource = resource,
id = id,
name = name,
kind = kind,
library = library,
startTimeNanos = startTimeNanos,
endTimeNanos = endTimeNanos,
durationNanos = calculateDurationNanos,
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, Resource, Span, SpanBuilder, SpanHandler, SpanId, SpanInfo }
import com.comcast.money.core.samplers.{ DropResult, RecordResult, Sampler }
import io.opentelemetry.api.common.{ AttributeKey, Attributes }
import io.opentelemetry.context.Context
Expand All @@ -28,13 +28,13 @@ import java.util.Optional
import scala.collection.JavaConverters._

private[core] class CoreSpanBuilder(
resource: Resource,
spanId: Option[SpanId],
var parentSpan: Option[Span],
spanName: String,
clock: Clock,
handler: SpanHandler,
sampler: Sampler,
library: InstrumentationLibrary) extends SpanBuilder {
sampler: Sampler) extends SpanBuilder {

var sticky: Boolean = true
var spanKind: SpanKind = SpanKind.INTERNAL
Expand Down Expand Up @@ -112,12 +112,12 @@ private[core] class CoreSpanBuilder(
}

private[core] def createSpan(id: SpanId, name: String, kind: SpanKind, startTimeNanos: Long): Span = CoreSpan(
resource = resource,
id = id,
name = name,
startTimeNanos = startTimeNanos,
kind = kind,
links = links,
library = library,
clock = clock,
handler = handler)

Expand All @@ -131,7 +131,7 @@ private[core] class CoreSpanBuilder(
}

sampler.shouldSample(spanId, parentSpanId, spanName) match {
case DropResult => UnrecordedSpan(spanId, spanName)
case DropResult => UnrecordedSpan(spanId, spanName, resource)
case RecordResult(sample, notes) =>
val traceFlags = if (sample) TraceFlags.getSampled else TraceFlags.getDefault

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

package com.comcast.money.core

import com.comcast.money.api.{ InstrumentationLibrary, Span, SpanBuilder, SpanFactory, SpanHandler, SpanId }
import com.comcast.money.api.{ InstrumentationLibrary, Resource, Span, SpanBuilder, SpanFactory, SpanHandler, SpanId }
import com.comcast.money.core.formatters.Formatter
import com.comcast.money.core.internal.SpanContext
import com.comcast.money.core.samplers.Sampler
Expand All @@ -27,7 +27,7 @@ private[core] final case class CoreSpanFactory(
handler: SpanHandler,
formatter: Formatter,
sampler: Sampler,
library: InstrumentationLibrary) extends SpanFactory {
resource: Resource) extends SpanFactory {

override def spanBuilder(spanName: String): SpanBuilder =
spanBuilder(spanName, None, spanContext.current)
Expand All @@ -53,5 +53,5 @@ private[core] final case class CoreSpanFactory(
clock = clock,
handler = handler,
sampler = sampler,
library = library)
resource = resource)
}
Expand Up @@ -17,7 +17,8 @@
package com.comcast.money.core

import java.util.Collections
import com.comcast.money.api.{ InstrumentationLibrary, Note, SpanId, SpanInfo }
import com.comcast.money.api.{ InstrumentationLibrary, Note, Resource, SpanId, SpanInfo }
import io.opentelemetry.api.common.Attributes
import io.opentelemetry.api.trace.{ Span, SpanKind, StatusCode }

private[core] case class CoreSpanInfo(
Expand All @@ -32,6 +33,9 @@ private[core] case class CoreSpanInfo(
notes: java.util.Map[String, Note[_]] = Collections.emptyMap(),
override val events: java.util.List[SpanInfo.Event] = Collections.emptyList(),
override val links: java.util.List[SpanInfo.Link] = Collections.emptyList(),
library: InstrumentationLibrary = Money.InstrumentationLibrary,
appName: String = Money.Environment.applicationName,
host: String = Money.Environment.hostName) extends SpanInfo
override val resource: Resource) extends SpanInfo {

override def library(): InstrumentationLibrary = resource.library()
override def appName(): String = resource.applicationName()
override def host(): String = resource.hostName()
}
7 changes: 4 additions & 3 deletions money-core/src/main/scala/com/comcast/money/core/Money.scala
Expand Up @@ -41,8 +41,8 @@ case class Money(
asyncNotifier: AsyncNotifier = new AsyncNotifier(Seq()))

object Money {

val InstrumentationLibrary = new InstrumentationLibrary("money-core", "0.10.0")
private lazy val MoneyVersion: String = getClass.getPackage.getImplementationVersion
lazy val InstrumentationLibrary = new InstrumentationLibrary("money-core", MoneyVersion)
lazy val Environment: Money = apply(ConfigFactory.load().getConfig("money"))

def apply(conf: Config): Money = {
Expand All @@ -56,7 +56,8 @@ object Money {
configureContextFilters(conf)
val formatter = configureFormatter(conf)
val sampler = configureSampler(conf)
val factory: SpanFactory = CoreSpanFactory(SpanLocal, clock, handler, formatter, sampler, Money.InstrumentationLibrary)
val resource = ResourceFactory.create(applicationName, hostName, InstrumentationLibrary, conf)
val factory: SpanFactory = CoreSpanFactory(SpanLocal, clock, handler, formatter, sampler, resource)
val tracer = new Tracer {
override val spanFactory: SpanFactory = factory
}
Expand Down

This file was deleted.

@@ -0,0 +1,56 @@
/*
* 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.core

import com.comcast.money.api.{ InstrumentationLibrary, Resource }
import com.typesafe.config.Config
import io.opentelemetry.api.common.{ AttributeKey, Attributes }
import io.opentelemetry.semconv.resource.attributes.ResourceAttributes

import scala.collection.JavaConverters._

private[core] object ResourceFactory {
def create(
applicationName: String,
hostName: String,
library: InstrumentationLibrary,
conf: Config): Resource = {

val attributesBuilder = Attributes.builder()
.put(ResourceAttributes.SERVICE_NAME, applicationName)
.put(ResourceAttributes.TELEMETRY_SDK_LANGUAGE, "scala")
.put(ResourceAttributes.TELEMETRY_SDK_NAME, library.name())
.put(ResourceAttributes.TELEMETRY_SDK_VERSION, library.version())
.put(ResourceAttributes.HOST_NAME, hostName)

val ResourceKey = "resource"
if (conf.hasPath(ResourceKey)) {
val resourceConf = conf.getConfig(ResourceKey)
for (entry <- resourceConf.entrySet().asScala) {
val key = entry.getKey
val value = resourceConf.getString(key)
attributesBuilder.put(AttributeKey.stringKey(key), value)
}
}

CoreResource(
applicationName = applicationName,
hostName = hostName,
library = library,
attributes = attributesBuilder.build())
}
}
9 changes: 5 additions & 4 deletions money-core/src/main/scala/com/comcast/money/core/Tracer.scala
Expand Up @@ -16,17 +16,18 @@

package com.comcast.money.core

import java.io.Closeable
import com.comcast.money.api

import com.comcast.money.api.{ MoneyTracer, Note, Span, SpanBuilder, SpanFactory }
import java.io.Closeable
import com.comcast.money.api.{ Note, Span, SpanBuilder, SpanFactory }
import com.comcast.money.core.internal.{ SpanContext, SpanLocal }
import io.opentelemetry.context.Scope
import io.opentelemetry.api.trace.{ StatusCode, Span => OtelSpan }
import io.opentelemetry.api.trace.StatusCode

/**
* Primary API to be used for tracing
*/
trait Tracer extends MoneyTracer with Closeable {
trait Tracer extends api.Tracer with Closeable {

val spanFactory: SpanFactory

Expand Down