Skip to content

Commit c5b804c

Browse files
committed
sdk-common: add ProcessRuntimeDetector
1 parent 452454e commit c5b804c

File tree

14 files changed

+274
-6
lines changed

14 files changed

+274
-6
lines changed

docs/sdk/configuration.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ If not specified, SDK defaults the service name to `unknown_service:scala`.
5050
| otel.resource.attributes | OTEL\\_RESOURCE\\_ATTRIBUTES | Specify resource attributes in the following format: `key1=val1,key2=val2,key3=val3`. |
5151
| otel.service.name | OTEL\\_SERVICE\\_NAME | Specify logical service name. Takes precedence over `service.name` defined with `otel.resource.attributes`. |
5252
| otel.experimental.resource.disabled-keys | OTEL\\_EXPERIMENTAL\\_RESOURCE\\_DISABLED\\_KEYS | Specify resource attribute keys that are filtered. |
53-
| otel.otel4s.resource.detectors | OTEL\\_OTEL4S\\_RESOURCE\\_DETECTORS | Specify resource detectors to use. Defaults to `host,os`. |
53+
| otel.otel4s.resource.detectors | OTEL\\_OTEL4S\\_RESOURCE\\_DETECTORS | Specify resource detectors to use. Defaults to `host,os,process_runtime`. |
5454

5555
## Metrics
5656

sdk/all/src/main/scala/org/typelevel/otel4s/sdk/OpenTelemetrySdk.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@ object OpenTelemetrySdk {
198198
* By default, the following detectors are enabled:
199199
* - host: `host.arch`, `host.name`
200200
* - os: `os.type`, `os.description`
201+
* - process_runtime: `process.runtime.name`,
202+
* `process.runtime.version`, `process.runtime.description`
201203
*
202204
* @param detector
203205
* the detector to add

sdk/common/js/src/main/scala/org/typelevel/otel4s/sdk/resource/OS.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ package org.typelevel.otel4s.sdk.resource
1919
import scala.scalajs.js
2020
import scala.scalajs.js.annotation.JSImport
2121

22+
/** A mapping of the Node.js OS API.
23+
*
24+
* @see
25+
* [[https://nodejs.org/api/os.html]]
26+
*/
2227
private object OS {
2328

2429
@js.native
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2023 Typelevel
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.typelevel.otel4s.sdk.resource
18+
19+
import scala.scalajs.js
20+
import scala.scalajs.js.annotation.JSImport
21+
22+
/** A mapping of the Node.js process API.
23+
*
24+
* @see
25+
* [[https://nodejs.org/api/process.html]]
26+
*/
27+
private object Process {
28+
29+
@js.native
30+
@JSImport("process", "versions")
31+
def versions: Versions = js.native
32+
33+
@js.native
34+
trait Versions extends js.Object {
35+
def node: String = js.native
36+
}
37+
38+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2023 Typelevel
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.typelevel.otel4s.sdk.resource
18+
19+
import cats.effect.Sync
20+
import org.typelevel.otel4s.Attributes
21+
import org.typelevel.otel4s.sdk.TelemetryResource
22+
import org.typelevel.otel4s.semconv.SchemaUrls
23+
24+
private[resource] trait ProcessRuntimeDetectorPlatform {
25+
self: ProcessRuntimeDetector.type =>
26+
27+
def apply[F[_]: Sync]: TelemetryResourceDetector[F] =
28+
new Detector[F]
29+
30+
private class Detector[F[_]: Sync] extends TelemetryResourceDetector[F] {
31+
def name: String = Const.Name
32+
33+
def detect: F[Option[TelemetryResource]] = Sync[F].delay {
34+
val attributes = Attributes(
35+
Keys.Name("nodejs"),
36+
Keys.Version(Process.versions.node),
37+
Keys.Description("Node.js")
38+
)
39+
40+
Some(TelemetryResource(attributes, Some(SchemaUrls.Current)))
41+
}
42+
}
43+
44+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2023 Typelevel
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.typelevel.otel4s.sdk.resource
18+
19+
import cats.effect.Sync
20+
import cats.syntax.apply._
21+
import cats.syntax.flatMap._
22+
import cats.syntax.functor._
23+
import org.typelevel.otel4s.Attributes
24+
import org.typelevel.otel4s.sdk.TelemetryResource
25+
import org.typelevel.otel4s.semconv.SchemaUrls
26+
27+
private[resource] trait ProcessRuntimeDetectorPlatform {
28+
self: ProcessRuntimeDetector.type =>
29+
30+
def apply[F[_]: Sync]: TelemetryResourceDetector[F] =
31+
new Detector[F]
32+
33+
private class Detector[F[_]: Sync] extends TelemetryResourceDetector[F] {
34+
def name: String = Const.Name
35+
36+
def detect: F[Option[TelemetryResource]] =
37+
for {
38+
runtimeName <- Sync[F].delay(sys.props.get("java.runtime.name"))
39+
runtimeVersion <- Sync[F].delay(sys.props.get("java.runtime.version"))
40+
vmVendor <- Sync[F].delay(sys.props.get("java.vm.vendor"))
41+
vmName <- Sync[F].delay(sys.props.get("java.vm.name"))
42+
vmVersion <- Sync[F].delay(sys.props.get("java.vm.version"))
43+
} yield {
44+
val attributes = Attributes.newBuilder
45+
46+
runtimeName.foreach(name => attributes.addOne(Keys.Name(name)))
47+
runtimeVersion.foreach(name => attributes.addOne(Keys.Version(name)))
48+
(vmVendor, vmName, vmVersion).mapN { (vendor, name, version) =>
49+
attributes.addOne(Keys.Description(s"$vendor $name $version"))
50+
}
51+
52+
Some(TelemetryResource(attributes.result(), Some(SchemaUrls.Current)))
53+
}
54+
}
55+
56+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2023 Typelevel
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.typelevel.otel4s.sdk.resource
18+
19+
import cats.effect.Sync
20+
import org.typelevel.otel4s.Attributes
21+
import org.typelevel.otel4s.sdk.TelemetryResource
22+
import org.typelevel.otel4s.semconv.SchemaUrls
23+
24+
private[resource] trait ProcessRuntimeDetectorPlatform {
25+
self: ProcessRuntimeDetector.type =>
26+
27+
def apply[F[_]: Sync]: TelemetryResourceDetector[F] =
28+
new Detector[F]
29+
30+
private class Detector[F[_]: Sync] extends TelemetryResourceDetector[F] {
31+
def name: String = Const.Name
32+
33+
def detect: F[Option[TelemetryResource]] =
34+
Sync[F].delay {
35+
val attributes = Attributes(
36+
Keys.Name("scalanative"),
37+
// Keys.Version(""), not available yet
38+
Keys.Description("Scala Native")
39+
)
40+
41+
Some(TelemetryResource(attributes, Some(SchemaUrls.Current)))
42+
}
43+
}
44+
45+
}

sdk/common/shared/src/main/scala/org/typelevel/otel4s/sdk/autoconfigure/TelemetryResourceAutoConfigure.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import java.nio.charset.StandardCharsets
4040
* | otel.resource.attributes | OTEL_RESOURCE_ATTRIBUTES | Specify resource attributes in the following format: key1=val1,key2=val2,key3=val3 |
4141
* | otel.service.name | OTEL_SERVICE_NAME | Specify logical service name. Takes precedence over `service.name` defined with `otel.resource.attributes` |
4242
* | otel.experimental.resource.disabled-keys | OTEL_EXPERIMENTAL_RESOURCE_DISABLED_KEYS | Specify resource attribute keys that are filtered. |
43-
* | otel.otel4s.resource.detectors | OTEL_OTEL4S_RESOURCE_DETECTORS | Specify resource detectors to use. Defaults to `host,os`. |
43+
* | otel.otel4s.resource.detectors | OTEL_OTEL4S_RESOURCE_DETECTORS | Specify resource detectors to use. Defaults to `host,os,process_runtime`. |
4444
* }}}
4545
*
4646
* @see
@@ -206,6 +206,7 @@ private[sdk] object TelemetryResourceAutoConfigure {
206206
val Detectors: Set[String] = Set(
207207
HostDetector.Const.Name,
208208
OSDetector.Const.Name
209+
ProcessRuntimeDetector.Const.Name
209210
)
210211
}
211212

@@ -218,7 +219,7 @@ private[sdk] object TelemetryResourceAutoConfigure {
218219
* | otel.resource.attributes | OTEL_RESOURCE_ATTRIBUTES | Specify resource attributes in the following format: key1=val1,key2=val2,key3=val3 |
219220
* | otel.service.name | OTEL_SERVICE_NAME | Specify logical service name. Takes precedence over `service.name` defined with `otel.resource.attributes` |
220221
* | otel.experimental.resource.disabled-keys | OTEL_EXPERIMENTAL_RESOURCE_DISABLED_KEYS | Specify resource attribute keys that are filtered. |
221-
* | otel.otel4s.resource.detectors | OTEL_OTEL4S_RESOURCE_DETECTORS | Specify resource detectors to use. Defaults to `host,os`. |
222+
* | otel.otel4s.resource.detectors | OTEL_OTEL4S_RESOURCE_DETECTORS | Specify resource detectors to use. Defaults to `host,os,process_runtime`. |
222223
* }}}
223224
*
224225
* @see
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2023 Typelevel
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.typelevel.otel4s.sdk.resource
18+
19+
import org.typelevel.otel4s.AttributeKey
20+
21+
/** Detects runtime details such as name, version, and description.
22+
*
23+
* @see
24+
* https://opentelemetry.io/docs/specs/semconv/resource/process/#process-runtimes
25+
*/
26+
object ProcessRuntimeDetector extends ProcessRuntimeDetectorPlatform {
27+
28+
private[sdk] object Const {
29+
val Name = "process_runtime"
30+
}
31+
32+
private[resource] object Keys {
33+
val Name: AttributeKey[String] =
34+
AttributeKey("process.runtime.name")
35+
36+
val Version: AttributeKey[String] =
37+
AttributeKey("process.runtime.version")
38+
39+
val Description: AttributeKey[String] =
40+
AttributeKey("process.runtime.description")
41+
}
42+
43+
}

sdk/common/shared/src/main/scala/org/typelevel/otel4s/sdk/resource/TelemetryResourceDetector.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,12 @@ object TelemetryResourceDetector {
5454
* Includes:
5555
* - host detector
5656
* - os detector
57+
* - process runtime detector
5758
*
5859
* @tparam F
5960
* the higher-kinded type of a polymorphic effect
6061
*/
6162
def default[F[_]: Sync]: Set[TelemetryResourceDetector[F]] =
62-
Set(HostDetector[F], OSDetector[F])
63+
Set(HostDetector[F], OSDetector[F], ProcessRuntimeDetector[F])
6364

6465
}

0 commit comments

Comments
 (0)