Skip to content

Commit

Permalink
Merge pull request #882 from iRevive/sdk/system-properties
Browse files Browse the repository at this point in the history
Use `SystemProperties`
  • Loading branch information
iRevive authored Jan 1, 2025
2 parents 363d595 + ce7b180 commit 5181f98
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import cats.effect.Async
import cats.effect.Resource
import cats.effect.std.Console
import cats.effect.std.Random
import cats.effect.std.SystemProperties
import cats.syntax.apply._
import cats.syntax.flatMap._
import cats.syntax.functor._
Expand Down Expand Up @@ -86,7 +87,7 @@ object OpenTelemetrySdk {
* @param customize
* a function for customizing the auto-configured SDK builder
*/
def autoConfigured[F[_]: Async: Parallel: Console: LocalContextProvider](
def autoConfigured[F[_]: Async: Parallel: SystemProperties: Console: LocalContextProvider](
customize: AutoConfigured.Builder[F] => AutoConfigured.Builder[F] = (a: AutoConfigured.Builder[F]) => a
): Resource[F, AutoConfigured[F]] =
customize(AutoConfigured.builder[F]).build
Expand Down Expand Up @@ -297,9 +298,7 @@ object OpenTelemetrySdk {

/** Creates a [[Builder]].
*/
def builder[
F[_]: Async: Parallel: Console: LocalContextProvider
]: Builder[F] =
def builder[F[_]: Async: Parallel: SystemProperties: Console: LocalContextProvider]: Builder[F] =
BuilderImpl(
customConfig = None,
propertiesLoader = Async[F].pure(Map.empty),
Expand All @@ -314,9 +313,7 @@ object OpenTelemetrySdk {
textMapPropagatorConfigurers = Set.empty
)

private final case class BuilderImpl[
F[_]: Async: Parallel: Console: LocalContextProvider
](
private final case class BuilderImpl[F[_]: Async: Parallel: SystemProperties: Console: LocalContextProvider](
customConfig: Option[Config],
propertiesLoader: F[Map[String, String]],
propertiesCustomizers: List[Config => Map[String, String]],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@

package org.typelevel.otel4s.sdk.resource

import cats.effect.Sync
import cats.Monad
import cats.effect.std.SystemProperties
import cats.syntax.flatMap._
import cats.syntax.functor._
import org.typelevel.otel4s.Attributes
Expand All @@ -27,16 +28,16 @@ import java.util.Locale

private[resource] trait OSDetectorPlatform { self: OSDetector.type =>

def apply[F[_]: Sync]: TelemetryResourceDetector[F] =
def apply[F[_]: Monad: SystemProperties]: TelemetryResourceDetector[F] =
new Detector[F]

private class Detector[F[_]: Sync] extends TelemetryResourceDetector[F] {
private class Detector[F[_]: Monad: SystemProperties] extends TelemetryResourceDetector[F] {
def name: String = Const.Name

def detect: F[Option[TelemetryResource]] =
for {
nameOpt <- Sync[F].delay(sys.props.get("os.name"))
versionOpt <- Sync[F].delay(sys.props.get("os.version"))
nameOpt <- SystemProperties[F].get("os.name")
versionOpt <- SystemProperties[F].get("os.version")
} yield {
val builder = Attributes.newBuilder

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.typelevel.otel4s.sdk.resource

import cats.effect.Sync
import cats.effect.std.SystemProperties
import cats.syntax.applicativeError._
import cats.syntax.flatMap._
import cats.syntax.functor._
Expand All @@ -28,16 +29,16 @@ import java.net.InetAddress

private[resource] trait HostDetectorPlatform { self: HostDetector.type =>

def apply[F[_]: Sync]: TelemetryResourceDetector[F] =
def apply[F[_]: Sync: SystemProperties]: TelemetryResourceDetector[F] =
new Detector[F]

private class Detector[F[_]: Sync] extends TelemetryResourceDetector[F] {
private class Detector[F[_]: Sync: SystemProperties] extends TelemetryResourceDetector[F] {
def name: String = Const.Name

def detect: F[Option[TelemetryResource]] =
for {
host <- Sync[F].blocking(InetAddress.getLocalHost.getHostName).redeem(_ => None, Some(_))
arch <- Sync[F].delay(sys.props.get("os.arch"))
arch <- SystemProperties[F].get("os.arch")
} yield {
val builder = Attributes.newBuilder

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.typelevel.otel4s.sdk.resource

import cats.effect.Sync
import cats.effect.std.SystemProperties
import cats.syntax.flatMap._
import cats.syntax.functor._
import org.typelevel.otel4s.Attributes
Expand All @@ -34,18 +35,18 @@ private[resource] trait ProcessDetectorPlatform { self: ProcessDetector.type =>
private val JarFilePattern =
Pattern.compile("^\\S+\\.(jar|war)", Pattern.CASE_INSENSITIVE)

def apply[F[_]: Sync]: TelemetryResourceDetector[F] =
def apply[F[_]: Sync: SystemProperties]: TelemetryResourceDetector[F] =
new Detector[F]

private class Detector[F[_]: Sync] extends TelemetryResourceDetector[F] {
private class Detector[F[_]: Sync: SystemProperties] extends TelemetryResourceDetector[F] {
def name: String = Const.Name

def detect: F[Option[TelemetryResource]] =
for {
runtime <- Sync[F].delay(ManagementFactory.getRuntimeMXBean)
javaHomeOpt <- Sync[F].delay(sys.props.get("java.home"))
osNameOpt <- Sync[F].delay(sys.props.get("os.name"))
javaCommandOpt <- Sync[F].delay(sys.props.get("sun.java.command"))
javaHomeOpt <- SystemProperties[F].get("java.home")
osNameOpt <- SystemProperties[F].get("os.name")
javaCommandOpt <- SystemProperties[F].get("sun.java.command")
} yield {
val builder = Attributes.newBuilder

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@

package org.typelevel.otel4s.sdk.resource

import cats.effect.Sync
import cats.Monad
import cats.effect.std.SystemProperties
import cats.syntax.apply._
import cats.syntax.flatMap._
import cats.syntax.functor._
Expand All @@ -27,19 +28,19 @@ import org.typelevel.otel4s.semconv.SchemaUrls
private[resource] trait ProcessRuntimeDetectorPlatform {
self: ProcessRuntimeDetector.type =>

def apply[F[_]: Sync]: TelemetryResourceDetector[F] =
def apply[F[_]: Monad: SystemProperties]: TelemetryResourceDetector[F] =
new Detector[F]

private class Detector[F[_]: Sync] extends TelemetryResourceDetector[F] {
private class Detector[F[_]: Monad: SystemProperties] extends TelemetryResourceDetector[F] {
def name: String = Const.Name

def detect: F[Option[TelemetryResource]] =
for {
runtimeName <- Sync[F].delay(sys.props.get("java.runtime.name"))
runtimeVersion <- Sync[F].delay(sys.props.get("java.runtime.version"))
vmVendor <- Sync[F].delay(sys.props.get("java.vm.vendor"))
vmName <- Sync[F].delay(sys.props.get("java.vm.name"))
vmVersion <- Sync[F].delay(sys.props.get("java.vm.version"))
runtimeName <- SystemProperties[F].get("java.runtime.name")
runtimeVersion <- SystemProperties[F].get("java.runtime.version")
vmVendor <- SystemProperties[F].get("java.vm.vendor")
vmName <- SystemProperties[F].get("java.vm.name")
vmVersion <- SystemProperties[F].get("java.vm.version")
} yield {
val attributes = Attributes.newBuilder

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.typelevel.otel4s.sdk.resource

import cats.effect.Sync
import cats.effect.std.SystemProperties
import cats.syntax.applicativeError._
import cats.syntax.flatMap._
import cats.syntax.functor._
Expand All @@ -30,16 +31,16 @@ import scala.scalanative.unsigned._

private[resource] trait HostDetectorPlatform { self: HostDetector.type =>

def apply[F[_]: Sync]: TelemetryResourceDetector[F] =
def apply[F[_]: Sync: SystemProperties]: TelemetryResourceDetector[F] =
new Detector[F]

private class Detector[F[_]: Sync] extends TelemetryResourceDetector[F] {
private class Detector[F[_]: Sync: SystemProperties] extends TelemetryResourceDetector[F] {
def name: String = Const.Name

def detect: F[Option[TelemetryResource]] =
for {
host <- Sync[F].delay(detectHost).handleError(_ => None)
arch <- Sync[F].delay(sys.props.get("os.arch"))
arch <- SystemProperties[F].get("os.arch")
} yield {
val builder = Attributes.newBuilder

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package autoconfigure
import cats.effect.Resource
import cats.effect.Sync
import cats.effect.std.Console
import cats.effect.std.SystemProperties
import cats.syntax.applicativeError._
import cats.syntax.either._
import cats.syntax.functor._
Expand Down Expand Up @@ -50,7 +51,7 @@ import java.nio.charset.StandardCharsets
* @param extraDetectors
* the extra detectors to use
*/
private final class TelemetryResourceAutoConfigure[F[_]: Sync: Console](
private final class TelemetryResourceAutoConfigure[F[_]: Sync: SystemProperties: Console](
extraDetectors: Set[TelemetryResourceDetector[F]]
) extends AutoConfigure.WithHint[F, TelemetryResource](
"TelemetryResource",
Expand Down Expand Up @@ -248,7 +249,7 @@ private[sdk] object TelemetryResourceAutoConfigure {
* @param extraDetectors
* the extra detectors to use
*/
def apply[F[_]: Sync: Console](
def apply[F[_]: Sync: SystemProperties: Console](
extraDetectors: Set[TelemetryResourceDetector[F]]
): AutoConfigure[F, TelemetryResource] =
new TelemetryResourceAutoConfigure[F](extraDetectors)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package sdk
package resource

import cats.effect.Sync
import cats.effect.std.SystemProperties

/** A detector creates a resource with environment(platform)-specific attributes.
*
Expand Down Expand Up @@ -58,12 +59,14 @@ object TelemetryResourceDetector {
* @tparam F
* the higher-kinded type of a polymorphic effect
*/
def default[F[_]: Sync]: Set[TelemetryResourceDetector[F]] =
def default[F[_]: Sync: SystemProperties]: Set[TelemetryResourceDetector[F]] = {
val _ = SystemProperties[F]
Set(
HostDetector[F],
OSDetector[F],
ProcessDetector[F],
ProcessRuntimeDetector[F]
)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import cats.effect.Async
import cats.effect.Resource
import cats.effect.std.Console
import cats.effect.std.Random
import cats.effect.std.SystemProperties
import cats.mtl.Ask
import cats.syntax.apply._
import cats.syntax.flatMap._
Expand Down Expand Up @@ -70,7 +71,7 @@ object SdkMetrics {
* @param customize
* a function for customizing the auto-configured SDK builder
*/
def autoConfigured[F[_]: Async: Console](
def autoConfigured[F[_]: Async: SystemProperties: Console](
customize: AutoConfigured.Builder[F] => AutoConfigured.Builder[F] = (a: AutoConfigured.Builder[F]) => a
): Resource[F, SdkMetrics[F]] =
customize(AutoConfigured.builder[F]).build
Expand Down Expand Up @@ -181,7 +182,7 @@ object SdkMetrics {

/** Creates a [[Builder]].
*/
def builder[F[_]: Async: Console]: Builder[F] =
def builder[F[_]: Async: SystemProperties: Console]: Builder[F] =
BuilderImpl(
customConfig = None,
propertiesLoader = Async[F].pure(Map.empty),
Expand All @@ -192,7 +193,7 @@ object SdkMetrics {
exporterConfigurers = Set.empty
)

private final case class BuilderImpl[F[_]: Async: Console](
private final case class BuilderImpl[F[_]: Async: SystemProperties: Console](
customConfig: Option[Config],
propertiesLoader: F[Map[String, String]],
propertiesCustomizers: List[Config => Map[String, String]],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import cats.effect.Async
import cats.effect.Resource
import cats.effect.std.Console
import cats.effect.std.Random
import cats.effect.std.SystemProperties
import cats.mtl.Local
import cats.syntax.apply._
import cats.syntax.flatMap._
Expand Down Expand Up @@ -86,7 +87,7 @@ object SdkTraces {
* @param customize
* a function for customizing the auto-configured SDK builder
*/
def autoConfigured[F[_]: Async: Parallel: Console: LocalContextProvider](
def autoConfigured[F[_]: Async: Parallel: SystemProperties: Console: LocalContextProvider](
customize: AutoConfigured.Builder[F] => AutoConfigured.Builder[F] = (a: AutoConfigured.Builder[F]) => a
): Resource[F, SdkTraces[F]] =
customize(AutoConfigured.builder[F]).build
Expand Down Expand Up @@ -217,9 +218,7 @@ object SdkTraces {

/** Creates a [[Builder]].
*/
def builder[
F[_]: Async: Parallel: Console: LocalContextProvider
]: Builder[F] =
def builder[F[_]: Async: Parallel: SystemProperties: Console: LocalContextProvider]: Builder[F] =
BuilderImpl(
customConfig = None,
propertiesLoader = Async[F].pure(Map.empty),
Expand All @@ -232,9 +231,7 @@ object SdkTraces {
textMapPropagatorConfigurers = Set.empty
)

private final case class BuilderImpl[
F[_]: Async: Parallel: Console: LocalContextProvider
](
private final case class BuilderImpl[F[_]: Async: Parallel: SystemProperties: Console: LocalContextProvider](
customConfig: Option[Config],
propertiesLoader: F[Map[String, String]],
propertiesCustomizers: List[Config => Map[String, String]],
Expand Down

0 comments on commit 5181f98

Please sign in to comment.