robertwb commented on code in PR #23978: URL: https://github.com/apache/beam/pull/23978#discussion_r1014238089
########## 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: Ah, good catch. Nothing should be done with a metric that hasn't been updated at least once. I've added some logic to only actually report those counters that are actually updated. In addition, as a precaution, I've also updated the distribution merge function. -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
