Skip to content

Commit

Permalink
fix: semconv alignment & listed in README (#2240)
Browse files Browse the repository at this point in the history
  • Loading branch information
Netail authored May 30, 2024
1 parent 27d6503 commit 040eaa6
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 11 deletions.
38 changes: 36 additions & 2 deletions packages/opentelemetry-host-metrics/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,47 @@ const exporter = new PrometheusExporter(
}
);

const meterProvider = new MeterProvider();
meterProvider.addMetricReader(exporter);
const meterProvider = new MeterProvider({
readers: [reader],
});

const hostMetrics = new HostMetrics({ meterProvider, name: 'example-host-metrics' });
hostMetrics.start();
```

## Semantic Conventions

This package uses Semantic Conventions [Version 1.25.0](https://github.com/open-telemetry/semantic-conventions/tree/v1.25.0/docs/system).
As for now the Semantic Conventions are bundled in this package but eventually will be imported from `@opentelemetry/semantic-conventions` package when it is updated to latest version.
Ref: [opentelemetry-js/issues/4235](https://github.com/open-telemetry/opentelemetry-js/issues/4235)

Metrics collected:

| Metric | Short Description |
| --------------------------- | --------------------------------------------------------- |
| `system.cpu.time` | Seconds each logical CPU spent on each mode |
| `system.cpu.utilization` | CPU usage time (0-1) |
| `system.memory.usage` | Reports memory in use by state |
| `system.memory.utilization` | Memory usage (0-1) |
| `system.network.dropped` | Count of packets that are dropped |
| `system.network.errors` | Count of network errors detected |
| `system.network.io` | Network flow direction |
| `process.cpu.time` | Total CPU seconds |
| `process.cpu.utilization` | Difference in process.cpu.time since the last measurement |
| `process.memory.usage` | The amount of physical memory in use |

Attributes collected:

| Metric | Short Description |
| --------------------------- | ---------------------------------- |
| `system.cpu.logical_number` | The logical CPU number |
| `system.cpu.state` | The state of the CPU |
| `system.memory.state` | The memory state |
| `system.device` | The device identifier |
| `network.io.direction` | The network IO operation direction |
| `system.network.state` | The network state |
| `process.cpu.state` | The CPU state |

## Useful links

- For more information on OpenTelemetry, visit: <https://opentelemetry.io/>
Expand Down
6 changes: 2 additions & 4 deletions packages/opentelemetry-host-metrics/src/enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,9 @@ export enum ATTRIBUTE_NAMES {
SYSTEM_CPU_STATE = 'system.cpu.state',
SYSTEM_MEMORY_STATE = 'system.memory.state',
SYSTEM_DEVICE = 'system.device',
SYSTEM_NETWORK_DIRECTION = 'system.network.direction',
NETWORK_IO_DIRECTION = 'network.io.direction',
SYSTEM_NETWORK_STATE = 'system.network.state',
// TODO: change value if semconv changes
// https://github.com/open-telemetry/opentelemetry-specification/issues/3776
PROCESS_CPU_STATE = 'state',
PROCESS_CPU_STATE = 'process.cpu.state',
}

export enum CPU_LABELS {
Expand Down
2 changes: 1 addition & 1 deletion packages/opentelemetry-host-metrics/src/metric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ export class HostMetrics extends BaseMetrics {
networkUsages: NetworkData[]
): void {
const deviceAttr = enums.ATTRIBUTE_NAMES.SYSTEM_DEVICE;
const directionAttr = enums.ATTRIBUTE_NAMES.SYSTEM_NETWORK_DIRECTION;
const directionAttr = enums.ATTRIBUTE_NAMES.NETWORK_IO_DIRECTION;

for (let i = 0, j = networkUsages.length; i < j; i++) {
const networkUsage = networkUsages[i];
Expand Down
2 changes: 1 addition & 1 deletion packages/opentelemetry-host-metrics/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
* limitations under the License.
*/

export function ObjectKeys<T>(t: T) {
export function ObjectKeys<T extends Record<string, unknown>>(t: T) {
return Object.keys(t) as (keyof T)[];
}
7 changes: 4 additions & 3 deletions packages/opentelemetry-host-metrics/test/metric.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,9 @@ describe('Host Metrics', () => {

reader = new TestMetricReader();

meterProvider = new MeterProvider();
meterProvider.addMetricReader(reader);
meterProvider = new MeterProvider({
readers: [reader],
});

hostMetrics = new HostMetrics({
meterProvider,
Expand All @@ -169,7 +170,7 @@ describe('Host Metrics', () => {
const sysCpuNumAttr = ATTRIBUTE_NAMES.SYSTEM_CPU_LOGICAL_NUMBER;
const sysMemStateAttr = ATTRIBUTE_NAMES.SYSTEM_MEMORY_STATE;
const sysDeviceAttr = ATTRIBUTE_NAMES.SYSTEM_DEVICE;
const sysNetDirAttr = ATTRIBUTE_NAMES.SYSTEM_NETWORK_DIRECTION;
const sysNetDirAttr = ATTRIBUTE_NAMES.NETWORK_IO_DIRECTION;
const procCpuStateAttr = ATTRIBUTE_NAMES.PROCESS_CPU_STATE;

it('should export CPU time metrics', async () => {
Expand Down

0 comments on commit 040eaa6

Please sign in to comment.