You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by GitBox <gi...@apache.org> on 2022/11/04 13:34:51 UTC

[GitHub] [beam] damccorm commented on a diff in pull request #23978: Add basic counter support to the typescript SDK.

damccorm commented on code in PR #23978:
URL: https://github.com/apache/beam/pull/23978#discussion_r1013984800


##########
sdks/typescript/src/apache_beam/worker/metrics.ts:
##########
@@ -0,0 +1,288 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import * as protobufjs from "protobufjs";
+
+import { MonitoringInfo } from "../proto/metrics";
+import { Coder, Context } from "../coders/coders";
+import { IterableCoder } from "../coders/required_coders";
+import { VarIntCoder } from "../coders/standard_coders";
+
+export interface MetricSpec {
+  metricType: string;
+  name: string;
+}
+
+const metricTypes = new Map<
+  string,
+  (spec: MetricSpec) => MetricCell<unknown>
+>();
+
+/**
+ * A MetricCell holds the concrete value of a metric at runtime.
+ *
+ * Different types of metrics will have different types of MetricCells,
+ * with the relationship being stored in the metricTypes map.
+ */
+interface MetricCell<T> {
+  // This is the publicly available function at execution time.
+  update: (value: T) => void;
+
+  // Rather than clear and re-create counters every time, we reset them
+  // between bundles.
+  reset: () => void;
+
+  // These are used to pass counters back from the worker.
+  toEmptyMonitoringInfo: () => MonitoringInfo;
+  payload: () => Uint8Array;
+
+  // These are used for reading counters.
+  loadFromPayload: (paylaod: Uint8Array) => void;
+  merge(other: MetricCell<T>);
+  extract(): any;
+}
+
+class Counter implements MetricCell<number> {
+  private value = 0;
+
+  constructor(spec: MetricSpec) {}
+
+  update(value: number) {
+    this.value += value;
+  }
+
+  reset(): void {
+    this.value = 0;
+  }
+
+  toEmptyMonitoringInfo(): MonitoringInfo {
+    return MonitoringInfo.create({
+      urn: "beam:metric:user:sum_int64:v1",
+      type: "beam:metrics:sum_int64:v1",
+    });
+  }
+
+  payload(): Uint8Array {
+    return encode(this.value, new VarIntCoder());
+  }
+
+  loadFromPayload(payload: Uint8Array) {
+    this.value = decode(payload, new VarIntCoder());
+  }
+
+  merge(other) {
+    this.value += other.value;
+  }
+
+  extract() {
+    return this.value;
+  }
+}
+
+metricTypes.set("beam:metric:user:sum_int64:v1", (spec) => new Counter(spec));
+
+class Distribution implements MetricCell<number> {
+  private count = 0;
+  private sum = 0;
+  private min = 0;
+  private max = 0;
+
+  private coder = new IterableCoder(new VarIntCoder());
+
+  constructor(spec: MetricSpec) {}
+
+  update(value: number) {
+    if (this.count == 0) {
+      this.count = 1;
+      this.sum = this.min = this.max = value;
+    } else {
+      this.count += 1;
+      this.sum += value;
+      this.min = Math.min(value, this.min);
+      this.max = Math.max(value, this.max);
+    }
+  }
+
+  reset(): void {
+    this.count = 0;

Review Comment:
   Should we be resetting the rest of the metrics here? I know functionally that they'll be reset on the first update, but this might lead to weird/unexpected behavior if any operation is done with an unupdated metric.
   
   I guess maybe a better question is: is it ever valid to do anything with a metric that hasn't been updated at least once? If so, things like merge won't ever really work and we'd be better off initializing min to MAX_INT, max to MIN_INT, and fully resetting them here.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org