-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
SimpleMonitoringInfoBuilder.java
177 lines (156 loc) · 6.87 KB
/
SimpleMonitoringInfoBuilder.java
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
* 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.
*/
package org.apache.beam.runners.core.metrics;
import java.time.Instant;
import java.util.HashMap;
import javax.annotation.Nullable;
import org.apache.beam.model.fnexecution.v1.BeamFnApi;
import org.apache.beam.model.fnexecution.v1.BeamFnApi.MonitoringInfo;
import org.apache.beam.model.fnexecution.v1.BeamFnApi.MonitoringInfoSpec;
import org.apache.beam.model.fnexecution.v1.BeamFnApi.MonitoringInfoSpecs;
import org.apache.beam.model.fnexecution.v1.BeamFnApi.MonitoringInfoTypeUrns;
import org.apache.beam.model.fnexecution.v1.BeamFnApi.MonitoringInfoUrns;
import org.apache.beam.runners.core.construction.BeamUrns;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Simplified building of MonitoringInfo fields, allows setting one field at a time with simpler
* method calls, without needing to dive into the details of the nested protos.
*
* <p>There is no need to set the type field, by setting the appropriate value field: (i.e.
* setInt64Value), the typeUrn field is automatically set.
*
* <p>Additionally, if validateAndDropInvalid is set to true in the ctor, then MonitoringInfos will
* be returned as null when build() is called if any fields are not properly set. This is based on
* comparing the fields which are set to the MonitoringInfoSpec in beam_fn_api.proto.
*
* <p>Example Usage (ElementCount counter):
*
* <p>SimpleMonitoringInfoBuilder builder = new SimpleMonitoringInfoBuilder();
* builder.setUrn(SimpleMonitoringInfoBuilder.ELEMENT_COUNT_URN); builder.setInt64Value(1);
* builder.setPTransformLabel("myTransform"); builder.setPCollectionLabel("myPcollection");
* MonitoringInfo mi = builder.build();
*
* <p>Example Usage (ElementCount counter):
*
* <p>SimpleMonitoringInfoBuilder builder = new SimpleMonitoringInfoBuilder();
* builder.setUrn(SimpleMonitoringInfoBuilder.setUrnForUserMetric("myNamespace", "myName"));
* builder.setInt64Value(1); MonitoringInfo mi = builder.build();
*/
public class SimpleMonitoringInfoBuilder {
public static final String ELEMENT_COUNT_URN =
BeamUrns.getUrn(MonitoringInfoUrns.Enum.ELEMENT_COUNT);
public static final String USER_COUNTER_URN_PREFIX =
BeamUrns.getUrn(MonitoringInfoUrns.Enum.USER_COUNTER_URN_PREFIX);
public static final String SUM_INT64_TYPE_URN =
BeamUrns.getUrn(MonitoringInfoTypeUrns.Enum.SUM_INT64_TYPE);
private static final HashMap<String, MonitoringInfoSpec> specs =
new HashMap<String, MonitoringInfoSpec>();
private final boolean validateAndDropInvalid;
private static final Logger LOG = LoggerFactory.getLogger(SimpleMonitoringInfoBuilder.class);
private MonitoringInfo.Builder builder;
private SpecMonitoringInfoValidator validator = new SpecMonitoringInfoValidator();
static {
for (MonitoringInfoSpecs.Enum val : MonitoringInfoSpecs.Enum.values()) {
// The enum iterator inserts an UNRECOGNIZED = -1 value which isn't explicitly added in
// the proto files.
if (!((Enum) val).name().equals("UNRECOGNIZED")) {
MonitoringInfoSpec spec =
val.getValueDescriptor().getOptions().getExtension(BeamFnApi.monitoringInfoSpec);
SimpleMonitoringInfoBuilder.specs.put(spec.getUrn(), spec);
}
}
}
public SimpleMonitoringInfoBuilder() {
this(true);
}
public SimpleMonitoringInfoBuilder(boolean validateAndDropInvalid) {
this.builder = MonitoringInfo.newBuilder();
this.validateAndDropInvalid = validateAndDropInvalid;
}
/** @return The metric URN for a user metric, with a proper URN prefix. */
private static String userMetricUrn(String metricNamespace, String metricName) {
String fixedMetricNamespace = metricNamespace.replace(':', '_');
String fixedMetricName = metricName.replace(':', '_');
StringBuilder sb = new StringBuilder();
sb.append(USER_COUNTER_URN_PREFIX);
sb.append(fixedMetricNamespace);
sb.append(':');
sb.append(fixedMetricName);
return sb.toString();
}
/**
* Sets the urn of the MonitoringInfo.
*
* @param urn The urn of the MonitoringInfo
*/
public SimpleMonitoringInfoBuilder setUrn(String urn) {
this.builder.setUrn(urn);
return this;
}
/**
* Sets the urn of the MonitoringInfo to a proper user metric URN for the given params.
*
* @param namespace
* @param name
*/
public SimpleMonitoringInfoBuilder setUrnForUserMetric(String namespace, String name) {
this.builder.setUrn(userMetricUrn(namespace, name));
return this;
}
/** Sets the timestamp of the MonitoringInfo to the current time. */
public SimpleMonitoringInfoBuilder setTimestampToNow() {
Instant time = Instant.now();
this.builder.getTimestampBuilder().setSeconds(time.getEpochSecond()).setNanos(time.getNano());
return this;
}
/** Sets the int64Value of the CounterData in the MonitoringInfo, and the appropraite type URN. */
public SimpleMonitoringInfoBuilder setInt64Value(long value) {
this.builder.getMetricBuilder().getCounterDataBuilder().setInt64Value(value);
this.builder.setType(SUM_INT64_TYPE_URN);
return this;
}
/** Sets the PTRANSFORM MonitoringInfo label to the given param. */
public SimpleMonitoringInfoBuilder setPTransformLabel(String pTransform) {
// TODO(ajamato): Add validation that it is a valid pTransform name in the bundle descriptor.
setLabel("PTRANSFORM", pTransform);
return this;
}
/** Sets the PCOLLECTION MonitoringInfo label to the given param. */
public SimpleMonitoringInfoBuilder setPCollectionLabel(String pCollection) {
setLabel("PCOLLECTION", pCollection);
return this;
}
/** Sets the MonitoringInfo label to the given name and value. */
public SimpleMonitoringInfoBuilder setLabel(String labelName, String labelValue) {
this.builder.putLabels(labelName, labelValue);
return this;
}
/**
* Builds the provided MonitoringInfo. Returns null if validateAndDropInvalid set and fields do
* not match respecting MonitoringInfoSpec based on urn.
*/
@Nullable
public MonitoringInfo build() {
final MonitoringInfo result = this.builder.build();
if (validateAndDropInvalid && this.validator.validate(result).isPresent()) {
return null;
}
return result;
}
}