summaryrefslogtreecommitdiff
path: root/js/metrics.ts
blob: 92ed5b52e8c9985695d26b5e80da1f1eaa739740 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import * as msg from "gen/cli/msg_generated";
import * as flatbuffers from "./flatbuffers";
import { assert } from "./util";
import * as dispatch from "./dispatch";

export interface Metrics {
  opsDispatched: number;
  opsCompleted: number;
  bytesSentControl: number;
  bytesSentData: number;
  bytesReceived: number;
}

function req(): [flatbuffers.Builder, msg.Any, flatbuffers.Offset] {
  const builder = flatbuffers.createBuilder();
  const inner = msg.Metrics.createMetrics(builder);
  return [builder, msg.Any.Metrics, inner];
}

function res(baseRes: null | msg.Base): Metrics {
  assert(baseRes !== null);
  assert(msg.Any.MetricsRes === baseRes!.innerType());
  const res = new msg.MetricsRes();
  assert(baseRes!.inner(res) !== null);

  return {
    opsDispatched: res.opsDispatched().toFloat64(),
    opsCompleted: res.opsCompleted().toFloat64(),
    bytesSentControl: res.bytesSentControl().toFloat64(),
    bytesSentData: res.bytesSentData().toFloat64(),
    bytesReceived: res.bytesReceived().toFloat64()
  };
}

/** Receive metrics from the privileged side of Deno.
 *
 *      > console.table(Deno.metrics())
 *      ┌──────────────────┬────────┐
 *      │     (index)      │ Values │
 *      ├──────────────────┼────────┤
 *      │  opsDispatched   │   9    │
 *      │   opsCompleted   │   9    │
 *      │ bytesSentControl │  504   │
 *      │  bytesSentData   │   0    │
 *      │  bytesReceived   │  856   │
 *      └──────────────────┴────────┘
 */
export function metrics(): Metrics {
  return res(dispatch.sendSync(...req()));
}