Skip to content

Use weaver to generate OSHI metrics #14346

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions instrumentation/oshi/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# From where to resolve the containers (e.g. "otel/weaver").
WEAVER_CONTAINER_REPOSITORY=docker.io
# Versioned, non-qualified references to containers used in this Makefile.
# These are parsed from dependencies.Dockerfile so dependabot will autoupdate
# the versions of docker files we use.
VERSIONED_WEAVER_CONTAINER_NO_REPO=$(shell cat weaver.Dockerfile | awk '$$4=="weaver" {print $$2}')
# Versioned, non-qualified references to containers used in this Makefile.
WEAVER_CONTAINER=$(WEAVER_CONTAINER_REPOSITORY)/$(VERSIONED_WEAVER_CONTAINER_NO_REPO)

# Next - we want to run docker as our local file user, so generated code is not
# owned by root, and we don't give unnecessary access.
#
# Determine if "docker" is actually podman
DOCKER_VERSION_OUTPUT := $(shell docker --version 2>&1)
DOCKER_IS_PODMAN := $(shell echo $(DOCKER_VERSION_OUTPUT) | grep -c podman)
ifeq ($(DOCKER_IS_PODMAN),0)
DOCKER_COMMAND := docker
else
DOCKER_COMMAND := podman
endif
DOCKER_RUN=$(DOCKER_COMMAND) run
DOCKER_USER=$(shell id -u):$(shell id -g)
DOCKER_USER_IS_HOST_USER_ARG=-u $(DOCKER_USER)
ifeq ($(DOCKER_COMMAND),podman)
# On podman, additional arguments are needed to make "-u" work
# correctly with the host user ID and host group ID.
#
# Error: OCI runtime error: crun: setgroups: Invalid argument
DOCKER_USER_IS_HOST_USER_ARG=--userns=keep-id -u $(DOCKER_USER)
endif

.PHONY: generate-docs
generate-docs:
mkdir -p docs
$(DOCKER_RUN) --rm \
$(DOCKER_USER_IS_HOST_USER_ARG) \
--mount 'type=bind,source=$(PWD)/model,target=/home/weaver/model,readonly' \
--mount 'type=bind,source=$(PWD)/templates,target=/home/weaver/templates,readonly' \
--mount 'type=bind,source=$(PWD)/docs,target=/home/weaver/target' \
${WEAVER_CONTAINER} registry generate \
--registry=/home/weaver/model \
markdown \
--future \
/home/weaver/target

.PHONY: check
check:
$(DOCKER_RUN) --rm \
$(DOCKER_USER_IS_HOST_USER_ARG) \
--mount 'type=bind,source=$(PWD)/model,target=/home/weaver/model,readonly' \
--mount 'type=bind,source=$(PWD)/templates,target=/home/weaver/templates,readonly' \
--mount 'type=bind,source=$(PWD)/docs,target=/home/weaver/target' \
${WEAVER_CONTAINER} registry check \
--registry=/home/weaver/model

.PHONY: generate-java
generate-java:
mkdir -p library/src/main/java/io/opentelemetry/oshi
$(DOCKER_RUN) --rm \
$(DOCKER_USER_IS_HOST_USER_ARG) \
--mount 'type=bind,source=$(PWD)/model,target=/home/weaver/model,readonly' \
--mount 'type=bind,source=$(PWD)/templates,target=/home/weaver/templates,readonly' \
--mount 'type=bind,source=$(PWD)/library/src/main/java/io/opentelemetry/instrumentation/oshi,target=/home/weaver/target' \
${WEAVER_CONTAINER} registry generate \
--registry=/home/weaver/model \
java \
--future \
/home/weaver/target

.PHONY: generate
generate: generate-docs generate-java
15 changes: 15 additions & 0 deletions instrumentation/oshi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,18 @@
# Using OSHI with OpenTelemetry Java agent

Download oshi-core jar from https://search.maven.org/search?q=g:com.github.oshi%20AND%20a:oshi-core and place it on the class path. OpenTelemetry Java agent uses system class loader to load classes from the oshi-core jar that are used for the metrics.


## Development with weaver

To regenerate the code (run these from within this `oshi` directory):

```bash
make generate-java
```

To regenerate the documentation:

```bash
make generate-docs
```
140 changes: 140 additions & 0 deletions instrumentation/oshi/docs/metrics.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
# Produced Metrics


## Metric `system.memory.utilization`

| Name | Instrument Type | Unit (UCUM) | Description | Stability |
| -------- | --------------- | ----------- | -------------- | --------- |
| `system.memory.utilization` | Gauge | `1` | System memory utilization | ![Development](https://img.shields.io/badge/-development-blue) |


### `system.memory.utilization` Attributes

| Attribute | Type | Description | Examples | [Requirement Level](https://opentelemetry.io/docs/specs/semconv/general/attribute-requirement-level/) | Stability |
|---|---|---|---|---|---|
| `state` | string | The type of memory being measured. | `used`; `free`; `cached`; `buffered` | `Required` | ![Development](https://img.shields.io/badge/-development-blue) |



## Metric `system.memory.usage`

| Name | Instrument Type | Unit (UCUM) | Description | Stability |
| -------- | --------------- | ----------- | -------------- | --------- |
| `system.memory.usage` | UpDownCounter | `By` | System memory usage | ![Development](https://img.shields.io/badge/-development-blue) |


### `system.memory.usage` Attributes

| Attribute | Type | Description | Examples | [Requirement Level](https://opentelemetry.io/docs/specs/semconv/general/attribute-requirement-level/) | Stability |
|---|---|---|---|---|---|
| `state` | string | The type of memory being measured. | `used`; `free`; `cached`; `buffered` | `Required` | ![Development](https://img.shields.io/badge/-development-blue) |



## Metric `system.network.io`

| Name | Instrument Type | Unit (UCUM) | Description | Stability |
| -------- | --------------- | ----------- | -------------- | --------- |
| `system.network.io` | Counter | `By` | System network IO | ![Development](https://img.shields.io/badge/-development-blue) |


### `system.network.io` Attributes

| Attribute | Type | Description | Examples | [Requirement Level](https://opentelemetry.io/docs/specs/semconv/general/attribute-requirement-level/) | Stability |
|---|---|---|---|---|---|
| `device` | string | The name of the network device. | `eth0` | `Required` | ![Development](https://img.shields.io/badge/-development-blue) |
| `direction` | string | The direction of the flow of data being measured. | `receive`; `transmit`; `read`; `write` | `Required` | ![Development](https://img.shields.io/badge/-development-blue) |



## Metric `system.network.packets`

| Name | Instrument Type | Unit (UCUM) | Description | Stability |
| -------- | --------------- | ----------- | -------------- | --------- |
| `system.network.packets` | Counter | `{packets}` | System network packets | ![Development](https://img.shields.io/badge/-development-blue) |


### `system.network.packets` Attributes

| Attribute | Type | Description | Examples | [Requirement Level](https://opentelemetry.io/docs/specs/semconv/general/attribute-requirement-level/) | Stability |
|---|---|---|---|---|---|
| `device` | string | The name of the network device. | `eth0` | `Required` | ![Development](https://img.shields.io/badge/-development-blue) |
| `direction` | string | The direction of the flow of data being measured. | `receive`; `transmit`; `read`; `write` | `Required` | ![Development](https://img.shields.io/badge/-development-blue) |



## Metric `system.network.errors`

| Name | Instrument Type | Unit (UCUM) | Description | Stability |
| -------- | --------------- | ----------- | -------------- | --------- |
| `system.network.errors` | Counter | `{errors}` | System network errors | ![Development](https://img.shields.io/badge/-development-blue) |


### `system.network.errors` Attributes

| Attribute | Type | Description | Examples | [Requirement Level](https://opentelemetry.io/docs/specs/semconv/general/attribute-requirement-level/) | Stability |
|---|---|---|---|---|---|
| `device` | string | The name of the network device. | `eth0` | `Required` | ![Development](https://img.shields.io/badge/-development-blue) |
| `direction` | string | The direction of the flow of data being measured. | `receive`; `transmit`; `read`; `write` | `Required` | ![Development](https://img.shields.io/badge/-development-blue) |



## Metric `system.disk.io`

| Name | Instrument Type | Unit (UCUM) | Description | Stability |
| -------- | --------------- | ----------- | -------------- | --------- |
| `system.disk.io` | Counter | `By` | System disk IO | ![Development](https://img.shields.io/badge/-development-blue) |


### `system.disk.io` Attributes

| Attribute | Type | Description | Examples | [Requirement Level](https://opentelemetry.io/docs/specs/semconv/general/attribute-requirement-level/) | Stability |
|---|---|---|---|---|---|
| `device` | string | The name of the network device. | `eth0` | `Required` | ![Development](https://img.shields.io/badge/-development-blue) |
| `direction` | string | The direction of the flow of data being measured. | `receive`; `transmit`; `read`; `write` | `Required` | ![Development](https://img.shields.io/badge/-development-blue) |



## Metric `system.disk.operations`

| Name | Instrument Type | Unit (UCUM) | Description | Stability |
| -------- | --------------- | ----------- | -------------- | --------- |
| `system.disk.operations` | Counter | `{operations}` | System disk operations | ![Development](https://img.shields.io/badge/-development-blue) |


### `system.disk.operations` Attributes

| Attribute | Type | Description | Examples | [Requirement Level](https://opentelemetry.io/docs/specs/semconv/general/attribute-requirement-level/) | Stability |
|---|---|---|---|---|---|
| `device` | string | The name of the network device. | `eth0` | `Required` | ![Development](https://img.shields.io/badge/-development-blue) |
| `direction` | string | The direction of the flow of data being measured. | `receive`; `transmit`; `read`; `write` | `Required` | ![Development](https://img.shields.io/badge/-development-blue) |



## Metric `runtime.java.memory`

| Name | Instrument Type | Unit (UCUM) | Description | Stability |
| -------- | --------------- | ----------- | -------------- | --------- |
| `runtime.java.memory` | UpDownCounter | `By` | Runtime Java memory | ![Development](https://img.shields.io/badge/-development-blue) |


### `runtime.java.memory` Attributes

| Attribute | Type | Description | Examples | [Requirement Level](https://opentelemetry.io/docs/specs/semconv/general/attribute-requirement-level/) | Stability |
|---|---|---|---|---|---|
| `type` | string | The type of memory measurement | `rss`; `vms` | `Recommended` | ![Development](https://img.shields.io/badge/-development-blue) |



## Metric `runtime.java.cpu_time`

| Name | Instrument Type | Unit (UCUM) | Description | Stability |
| -------- | --------------- | ----------- | -------------- | --------- |
| `runtime.java.cpu_time` | Gauge | `ms` | Runtime Java CPU time | ![Development](https://img.shields.io/badge/-development-blue) |


### `runtime.java.cpu_time` Attributes

| Attribute | Type | Description | Examples | [Requirement Level](https://opentelemetry.io/docs/specs/semconv/general/attribute-requirement-level/) | Stability |
|---|---|---|---|---|---|
| `type` | string | The type of CPU time measurement | `user`; `system` | `Recommended` | ![Development](https://img.shields.io/badge/-development-blue) |
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.oshi;

import static io.opentelemetry.api.common.AttributeKey.stringKey;

import io.opentelemetry.api.common.AttributeKey;

// This file is generated using weaver. Do not edit manually.

/** Attribute definitions generated from a Weaver model. Do not edit manually. */
public final class CustomAttributes {

/** The name of the network device. */
public static final AttributeKey<String> DEVICE = stringKey("device");

/** The direction of the flow of data being measured. */
public static final AttributeKey<String> DIRECTION = stringKey("direction");

/** The type of memory being measured. */
public static final AttributeKey<String> STATE = stringKey("state");

/** The type of measurement being taken. */
public static final AttributeKey<String> TYPE = stringKey("type");

private CustomAttributes() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.oshi;

import io.opentelemetry.api.metrics.Meter;
import io.opentelemetry.api.metrics.ObservableDoubleGauge;
import io.opentelemetry.api.metrics.ObservableDoubleMeasurement;
import io.opentelemetry.api.metrics.ObservableLongCounter;
import io.opentelemetry.api.metrics.ObservableLongGauge;
import io.opentelemetry.api.metrics.ObservableLongMeasurement;
import io.opentelemetry.api.metrics.ObservableLongUpDownCounter;
import java.util.function.Consumer;

// This file is generated using weaver. Do not edit manually.

/** Metric definitions generated from a Weaver model. Do not edit manually. */
public final class Metrics {

public static ObservableDoubleGauge createSystemMemoryUtilization(
Meter meter, Consumer<ObservableDoubleMeasurement> callback) {
return meter
.gaugeBuilder("system.memory.utilization")
.setUnit("1")
.setDescription("System memory utilization")
.buildWithCallback(callback);
}

public static ObservableLongUpDownCounter createSystemMemoryUsage(
Meter meter, Consumer<ObservableLongMeasurement> callback) {
return meter
.upDownCounterBuilder("system.memory.usage")
.setUnit("By")
.setDescription("System memory usage")
.buildWithCallback(callback);
}

public static ObservableLongCounter createSystemNetworkIo(
Meter meter, Consumer<ObservableLongMeasurement> callback) {
return meter
.counterBuilder("system.network.io")
.setUnit("By")
.setDescription("System network IO")
.buildWithCallback(callback);
}

public static ObservableLongCounter createSystemNetworkPackets(
Meter meter, Consumer<ObservableLongMeasurement> callback) {
return meter
.counterBuilder("system.network.packets")
.setUnit("{packets}")
.setDescription("System network packets")
.buildWithCallback(callback);
}

public static ObservableLongCounter createSystemNetworkErrors(
Meter meter, Consumer<ObservableLongMeasurement> callback) {
return meter
.counterBuilder("system.network.errors")
.setUnit("{errors}")
.setDescription("System network errors")
.buildWithCallback(callback);
}

public static ObservableLongCounter createSystemDiskIo(
Meter meter, Consumer<ObservableLongMeasurement> callback) {
return meter
.counterBuilder("system.disk.io")
.setUnit("By")
.setDescription("System disk IO")
.buildWithCallback(callback);
}

public static ObservableLongCounter createSystemDiskOperations(
Meter meter, Consumer<ObservableLongMeasurement> callback) {
return meter
.counterBuilder("system.disk.operations")
.setUnit("{operations}")
.setDescription("System disk operations")
.buildWithCallback(callback);
}

public static ObservableLongUpDownCounter createRuntimeJavaMemory(
Meter meter, Consumer<ObservableLongMeasurement> callback) {
return meter
.upDownCounterBuilder("runtime.java.memory")
.setUnit("By")
.setDescription("Runtime Java memory")
.buildWithCallback(callback);
}

public static ObservableLongGauge createRuntimeJavaCpuTime(
Meter meter, Consumer<ObservableLongMeasurement> callback) {
return meter
.gaugeBuilder("runtime.java.cpu_time")
.ofLongs()
.setUnit("ms")
.setDescription("Runtime Java CPU time")
.buildWithCallback(callback);
}

private Metrics() {}
}
Loading
Loading