|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * Copyright 2018-2020 The Feast Authors |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package feast.ingestion.transform.metrics; |
| 18 | + |
| 19 | +import static feast.ingestion.transform.metrics.WriteRowMetricsDoFn.FEATURE_SET_NAME_TAG_KEY; |
| 20 | +import static feast.ingestion.transform.metrics.WriteRowMetricsDoFn.FEATURE_SET_PROJECT_TAG_KEY; |
| 21 | +import static feast.ingestion.transform.metrics.WriteRowMetricsDoFn.FEATURE_SET_VERSION_TAG_KEY; |
| 22 | +import static feast.ingestion.transform.metrics.WriteRowMetricsDoFn.FEATURE_TAG_KEY; |
| 23 | +import static feast.ingestion.transform.metrics.WriteRowMetricsDoFn.INGESTION_JOB_NAME_KEY; |
| 24 | +import static feast.ingestion.transform.metrics.WriteRowMetricsDoFn.METRIC_PREFIX; |
| 25 | +import static feast.ingestion.transform.metrics.WriteRowMetricsDoFn.STORE_TAG_KEY; |
| 26 | + |
| 27 | +import com.google.auto.value.AutoValue; |
| 28 | +import com.timgroup.statsd.NonBlockingStatsDClient; |
| 29 | +import com.timgroup.statsd.StatsDClient; |
| 30 | +import feast.types.FeatureRowProto.FeatureRow; |
| 31 | +import feast.types.FieldProto.Field; |
| 32 | +import feast.types.ValueProto.Value; |
| 33 | +import java.util.ArrayList; |
| 34 | +import java.util.DoubleSummaryStatistics; |
| 35 | +import java.util.HashMap; |
| 36 | +import java.util.List; |
| 37 | +import java.util.Map; |
| 38 | +import java.util.Map.Entry; |
| 39 | +import org.apache.beam.sdk.transforms.DoFn; |
| 40 | +import org.apache.beam.sdk.values.KV; |
| 41 | +import org.apache.commons.math3.stat.descriptive.rank.Percentile; |
| 42 | +import org.slf4j.Logger; |
| 43 | + |
| 44 | +/** |
| 45 | + * WriteFeatureValueMetricsDoFn accepts key value of FeatureSetRef(str) to FeatureRow(List) and |
| 46 | + * writes a histogram of the numerical values of each feature to StatsD. |
| 47 | + * |
| 48 | + * <p>The histogram of the numerical values is represented as the following in StatsD: |
| 49 | + * |
| 50 | + * <ul> |
| 51 | + * <li>gauge of feature_value_min |
| 52 | + * <li>gauge of feature_value_max |
| 53 | + * <li>gauge of feature_value_mean |
| 54 | + * <li>gauge of feature_value_percentile_50 |
| 55 | + * <li>gauge of feature_value_percentile_90 |
| 56 | + * <li>gauge of feature_value_percentile_95 |
| 57 | + * </ul> |
| 58 | + * |
| 59 | + * <p>StatsD timing/histogram metric type is not used since it does not support negative values. |
| 60 | + */ |
| 61 | +@AutoValue |
| 62 | +public abstract class WriteFeatureValueMetricsDoFn |
| 63 | + extends DoFn<KV<String, Iterable<FeatureRow>>, Void> { |
| 64 | + |
| 65 | + abstract String getStoreName(); |
| 66 | + |
| 67 | + abstract String getStatsdHost(); |
| 68 | + |
| 69 | + abstract int getStatsdPort(); |
| 70 | + |
| 71 | + static Builder newBuilder() { |
| 72 | + return new AutoValue_WriteFeatureValueMetricsDoFn.Builder(); |
| 73 | + } |
| 74 | + |
| 75 | + @AutoValue.Builder |
| 76 | + abstract static class Builder { |
| 77 | + |
| 78 | + abstract Builder setStoreName(String storeName); |
| 79 | + |
| 80 | + abstract Builder setStatsdHost(String statsdHost); |
| 81 | + |
| 82 | + abstract Builder setStatsdPort(int statsdPort); |
| 83 | + |
| 84 | + abstract WriteFeatureValueMetricsDoFn build(); |
| 85 | + } |
| 86 | + |
| 87 | + private static final Logger log = |
| 88 | + org.slf4j.LoggerFactory.getLogger(WriteFeatureValueMetricsDoFn.class); |
| 89 | + private StatsDClient statsDClient; |
| 90 | + public static String GAUGE_NAME_FEATURE_VALUE_MIN = "feature_value_min"; |
| 91 | + public static String GAUGE_NAME_FEATURE_VALUE_MAX = "feature_value_max"; |
| 92 | + public static String GAUGE_NAME_FEATURE_VALUE_MEAN = "feature_value_mean"; |
| 93 | + public static String GAUGE_NAME_FEATURE_VALUE_PERCENTILE_50 = "feature_value_percentile_50"; |
| 94 | + public static String GAUGE_NAME_FEATURE_VALUE_PERCENTILE_90 = "feature_value_percentile_90"; |
| 95 | + public static String GAUGE_NAME_FEATURE_VALUE_PERCENTILE_95 = "feature_value_percentile_95"; |
| 96 | + |
| 97 | + @Setup |
| 98 | + public void setup() { |
| 99 | + // Note that exception may be thrown during StatsD client instantiation but no exception |
| 100 | + // will be thrown when sending metrics (mimicking the UDP protocol behaviour). |
| 101 | + // https://jar-download.com/artifacts/com.datadoghq/java-dogstatsd-client/2.1.1/documentation |
| 102 | + // https://github.com/DataDog/java-dogstatsd-client#unix-domain-socket-support |
| 103 | + try { |
| 104 | + statsDClient = new NonBlockingStatsDClient(METRIC_PREFIX, getStatsdHost(), getStatsdPort()); |
| 105 | + } catch (Exception e) { |
| 106 | + log.error("StatsD client cannot be started: " + e.getMessage()); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + @Teardown |
| 111 | + public void tearDown() { |
| 112 | + if (statsDClient != null) { |
| 113 | + statsDClient.close(); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + @ProcessElement |
| 118 | + public void processElement( |
| 119 | + ProcessContext context, |
| 120 | + @Element KV<String, Iterable<FeatureRow>> featureSetRefToFeatureRows) { |
| 121 | + if (statsDClient == null) { |
| 122 | + return; |
| 123 | + } |
| 124 | + |
| 125 | + String featureSetRef = featureSetRefToFeatureRows.getKey(); |
| 126 | + if (featureSetRef == null) { |
| 127 | + return; |
| 128 | + } |
| 129 | + String[] colonSplits = featureSetRef.split(":"); |
| 130 | + if (colonSplits.length != 2) { |
| 131 | + log.error( |
| 132 | + "Skip writing feature value metrics because the feature set reference '{}' does not" |
| 133 | + + "follow the required format <project>/<feature_set_name>:<version>", |
| 134 | + featureSetRef); |
| 135 | + return; |
| 136 | + } |
| 137 | + String[] slashSplits = colonSplits[0].split("/"); |
| 138 | + if (slashSplits.length != 2) { |
| 139 | + log.error( |
| 140 | + "Skip writing feature value metrics because the feature set reference '{}' does not" |
| 141 | + + "follow the required format <project>/<feature_set_name>:<version>", |
| 142 | + featureSetRef); |
| 143 | + return; |
| 144 | + } |
| 145 | + String projectName = slashSplits[0]; |
| 146 | + String featureSetName = slashSplits[1]; |
| 147 | + String version = colonSplits[1]; |
| 148 | + |
| 149 | + Map<String, DoubleSummaryStatistics> featureNameToStats = new HashMap<>(); |
| 150 | + Map<String, List<Double>> featureNameToValues = new HashMap<>(); |
| 151 | + for (FeatureRow featureRow : featureSetRefToFeatureRows.getValue()) { |
| 152 | + for (Field field : featureRow.getFieldsList()) { |
| 153 | + updateStats(featureNameToStats, featureNameToValues, field); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + for (Entry<String, DoubleSummaryStatistics> entry : featureNameToStats.entrySet()) { |
| 158 | + String featureName = entry.getKey(); |
| 159 | + DoubleSummaryStatistics stats = entry.getValue(); |
| 160 | + String[] tags = { |
| 161 | + STORE_TAG_KEY + ":" + getStoreName(), |
| 162 | + FEATURE_SET_PROJECT_TAG_KEY + ":" + projectName, |
| 163 | + FEATURE_SET_NAME_TAG_KEY + ":" + featureSetName, |
| 164 | + FEATURE_SET_VERSION_TAG_KEY + ":" + version, |
| 165 | + FEATURE_TAG_KEY + ":" + featureName, |
| 166 | + INGESTION_JOB_NAME_KEY + ":" + context.getPipelineOptions().getJobName() |
| 167 | + }; |
| 168 | + |
| 169 | + // stats can return non finite values when there is no element |
| 170 | + // or there is an element that is not a number. Metric should only be sent for finite values. |
| 171 | + if (Double.isFinite(stats.getMin())) { |
| 172 | + if (stats.getMin() < 0) { |
| 173 | + // StatsD gauge will asssign a delta instead of the actual value, if there is a sign in |
| 174 | + // the value. E.g. if the value is negative, a delta will be assigned. For this reason, |
| 175 | + // the gauge value is set to zero beforehand. |
| 176 | + // https://github.com/statsd/statsd/blob/master/docs/metric_types.md#gauges |
| 177 | + statsDClient.gauge(GAUGE_NAME_FEATURE_VALUE_MIN, 0, tags); |
| 178 | + } |
| 179 | + statsDClient.gauge(GAUGE_NAME_FEATURE_VALUE_MIN, stats.getMin(), tags); |
| 180 | + } |
| 181 | + if (Double.isFinite(stats.getMax())) { |
| 182 | + if (stats.getMax() < 0) { |
| 183 | + statsDClient.gauge(GAUGE_NAME_FEATURE_VALUE_MAX, 0, tags); |
| 184 | + } |
| 185 | + statsDClient.gauge(GAUGE_NAME_FEATURE_VALUE_MAX, stats.getMax(), tags); |
| 186 | + } |
| 187 | + if (Double.isFinite(stats.getAverage())) { |
| 188 | + if (stats.getAverage() < 0) { |
| 189 | + statsDClient.gauge(GAUGE_NAME_FEATURE_VALUE_MEAN, 0, tags); |
| 190 | + } |
| 191 | + statsDClient.gauge(GAUGE_NAME_FEATURE_VALUE_MEAN, stats.getAverage(), tags); |
| 192 | + } |
| 193 | + |
| 194 | + // For percentile calculation, Percentile class from commons-math3 from Apache is used. |
| 195 | + // Percentile requires double[], hence the conversion below. |
| 196 | + if (!featureNameToValues.containsKey(featureName)) { |
| 197 | + continue; |
| 198 | + } |
| 199 | + List<Double> valueList = featureNameToValues.get(featureName); |
| 200 | + if (valueList == null || valueList.size() < 1) { |
| 201 | + continue; |
| 202 | + } |
| 203 | + double[] values = new double[valueList.size()]; |
| 204 | + for (int i = 0; i < values.length; i++) { |
| 205 | + values[i] = valueList.get(i); |
| 206 | + } |
| 207 | + |
| 208 | + double p50 = new Percentile().evaluate(values, 50); |
| 209 | + if (p50 < 0) { |
| 210 | + statsDClient.gauge(GAUGE_NAME_FEATURE_VALUE_PERCENTILE_50, 0, tags); |
| 211 | + } |
| 212 | + statsDClient.gauge(GAUGE_NAME_FEATURE_VALUE_PERCENTILE_50, p50, tags); |
| 213 | + |
| 214 | + double p90 = new Percentile().evaluate(values, 90); |
| 215 | + if (p90 < 0) { |
| 216 | + statsDClient.gauge(GAUGE_NAME_FEATURE_VALUE_PERCENTILE_90, 0, tags); |
| 217 | + } |
| 218 | + statsDClient.gauge(GAUGE_NAME_FEATURE_VALUE_PERCENTILE_90, p90, tags); |
| 219 | + |
| 220 | + double p95 = new Percentile().evaluate(values, 95); |
| 221 | + if (p95 < 0) { |
| 222 | + statsDClient.gauge(GAUGE_NAME_FEATURE_VALUE_PERCENTILE_95, 0, tags); |
| 223 | + } |
| 224 | + statsDClient.gauge(GAUGE_NAME_FEATURE_VALUE_PERCENTILE_95, p95, tags); |
| 225 | + } |
| 226 | + } |
| 227 | + |
| 228 | + // Update stats and values array for the feature represented by the field. |
| 229 | + // If the field contains non-numerical or non-boolean value, the stats and values array |
| 230 | + // won't get updated because we are only concerned with numerical value in metrics data. |
| 231 | + // For boolean value, true and false are treated as numerical value of 1 of 0 respectively. |
| 232 | + private void updateStats( |
| 233 | + Map<String, DoubleSummaryStatistics> featureNameToStats, |
| 234 | + Map<String, List<Double>> featureNameToValues, |
| 235 | + Field field) { |
| 236 | + if (featureNameToStats == null || featureNameToValues == null || field == null) { |
| 237 | + return; |
| 238 | + } |
| 239 | + |
| 240 | + String featureName = field.getName(); |
| 241 | + if (!featureNameToStats.containsKey(featureName)) { |
| 242 | + featureNameToStats.put(featureName, new DoubleSummaryStatistics()); |
| 243 | + } |
| 244 | + if (!featureNameToValues.containsKey(featureName)) { |
| 245 | + featureNameToValues.put(featureName, new ArrayList<>()); |
| 246 | + } |
| 247 | + |
| 248 | + Value value = field.getValue(); |
| 249 | + DoubleSummaryStatistics stats = featureNameToStats.get(featureName); |
| 250 | + List<Double> values = featureNameToValues.get(featureName); |
| 251 | + |
| 252 | + switch (value.getValCase()) { |
| 253 | + case INT32_VAL: |
| 254 | + stats.accept(value.getInt32Val()); |
| 255 | + values.add(((double) value.getInt32Val())); |
| 256 | + break; |
| 257 | + case INT64_VAL: |
| 258 | + stats.accept(value.getInt64Val()); |
| 259 | + values.add((double) value.getInt64Val()); |
| 260 | + break; |
| 261 | + case DOUBLE_VAL: |
| 262 | + stats.accept(value.getDoubleVal()); |
| 263 | + values.add(value.getDoubleVal()); |
| 264 | + break; |
| 265 | + case FLOAT_VAL: |
| 266 | + stats.accept(value.getFloatVal()); |
| 267 | + values.add((double) value.getFloatVal()); |
| 268 | + break; |
| 269 | + case BOOL_VAL: |
| 270 | + stats.accept(value.getBoolVal() ? 1 : 0); |
| 271 | + values.add(value.getBoolVal() ? 1d : 0d); |
| 272 | + break; |
| 273 | + case INT32_LIST_VAL: |
| 274 | + for (Integer val : value.getInt32ListVal().getValList()) { |
| 275 | + stats.accept(val); |
| 276 | + values.add(((double) val)); |
| 277 | + } |
| 278 | + break; |
| 279 | + case INT64_LIST_VAL: |
| 280 | + for (Long val : value.getInt64ListVal().getValList()) { |
| 281 | + stats.accept(val); |
| 282 | + values.add(((double) val)); |
| 283 | + } |
| 284 | + break; |
| 285 | + case DOUBLE_LIST_VAL: |
| 286 | + for (Double val : value.getDoubleListVal().getValList()) { |
| 287 | + stats.accept(val); |
| 288 | + values.add(val); |
| 289 | + } |
| 290 | + break; |
| 291 | + case FLOAT_LIST_VAL: |
| 292 | + for (Float val : value.getFloatListVal().getValList()) { |
| 293 | + stats.accept(val); |
| 294 | + values.add(((double) val)); |
| 295 | + } |
| 296 | + break; |
| 297 | + case BOOL_LIST_VAL: |
| 298 | + for (Boolean val : value.getBoolListVal().getValList()) { |
| 299 | + stats.accept(val ? 1 : 0); |
| 300 | + values.add(val ? 1d : 0d); |
| 301 | + } |
| 302 | + break; |
| 303 | + case BYTES_VAL: |
| 304 | + case BYTES_LIST_VAL: |
| 305 | + case STRING_VAL: |
| 306 | + case STRING_LIST_VAL: |
| 307 | + case VAL_NOT_SET: |
| 308 | + default: |
| 309 | + } |
| 310 | + } |
| 311 | +} |
0 commit comments