4141import org .apache .beam .sdk .metrics .MetricsSink ;
4242
4343/** HTTP Sink to push metrics in a POST HTTP request. */
44- @ SuppressWarnings ({
45- "rawtypes" , // TODO(https://github.com/apache/beam/issues/20447)
46- "nullness" // TODO(https://github.com/apache/beam/issues/20497)
47- })
4844public class MetricsHttpSink implements MetricsSink {
4945 private final String urlString ;
5046 private final ObjectMapper objectMapper = new ObjectMapper ();
@@ -112,6 +108,7 @@ public MetricKeySerializer(Class<MetricKey> t) {
112108 super (t );
113109 }
114110
111+ @ SuppressWarnings ("nullness" ) // gen.writeObjectField expects nulls but is unannotated
115112 public void inline (MetricKey value , JsonGenerator gen , SerializerProvider provider )
116113 throws IOException {
117114 gen .writeObjectField ("name" , value .metricName ());
@@ -131,32 +128,41 @@ public void serialize(MetricKey value, JsonGenerator gen, SerializerProvider pro
131128 * JSON serializer for {@link MetricResult}; conform to an older format where the {@link MetricKey
132129 * key's} {@link MetricName name} and "step" (ptransform) are inlined.
133130 */
134- public static class MetricResultSerializer extends StdSerializer <MetricResult > {
131+ public static class MetricResultSerializer extends StdSerializer <MetricResult <?> > {
135132 private final MetricKeySerializer keySerializer ;
136133
137- public MetricResultSerializer (Class <MetricResult > t ) {
134+ public MetricResultSerializer (Class <MetricResult <?> > t ) {
138135 super (t );
139136 keySerializer = new MetricKeySerializer (MetricKey .class );
140137 }
141138
142139 @ Override
143- public void serialize (MetricResult value , JsonGenerator gen , SerializerProvider provider )
140+ public void serialize (MetricResult <?> value , JsonGenerator gen , SerializerProvider provider )
144141 throws IOException {
145142 gen .writeStartObject ();
143+ writeAttemptedAndCommitted (value , gen );
144+ keySerializer .inline (value .getKey (), gen , provider );
145+ gen .writeEndObject ();
146+ }
147+
148+ @ SuppressWarnings ("nullness" ) // gen.writeObjectField expects nulls but is unannotated
149+ private void writeAttemptedAndCommitted (MetricResult <?> value , JsonGenerator gen )
150+ throws IOException {
146151 gen .writeObjectField ("attempted" , value .getAttempted ());
147152 if (value .hasCommitted ()) {
148153 gen .writeObjectField ("committed" , value .getCommitted ());
149154 }
150- keySerializer .inline (value .getKey (), gen , provider );
151- gen .writeEndObject ();
152155 }
153156 }
154157
155158 private String serializeMetrics (MetricQueryResults metricQueryResults ) throws Exception {
156159 SimpleModule module = new JodaModule ();
157160 module .addSerializer (new MetricNameSerializer (MetricName .class ));
158161 module .addSerializer (new MetricKeySerializer (MetricKey .class ));
159- module .addSerializer (new MetricResultSerializer (MetricResult .class ));
162+ // This odd cast converts from rawtype Class<MetricResult> to Class<MetricResult<?>>
163+ // so the rest of the file can be properly typed
164+ module .addSerializer (
165+ new MetricResultSerializer ((Class <MetricResult <?>>) (Object ) MetricResult .class ));
160166 objectMapper .registerModule (module );
161167 objectMapper .disable (SerializationFeature .WRITE_DATES_AS_TIMESTAMPS );
162168 objectMapper .configure (MapperFeature .SORT_PROPERTIES_ALPHABETICALLY , true );
@@ -171,6 +177,7 @@ private String serializeMetrics(MetricQueryResults metricQueryResults) throws Ex
171177 result = objectMapper .writeValueAsString (metricQueryResults );
172178 } catch (JsonMappingException exception ) {
173179 if ((exception .getCause () instanceof UnsupportedOperationException )
180+ && exception .getCause ().getMessage () != null
174181 && exception .getCause ().getMessage ().contains ("committed metrics" )) {
175182 filterProvider .removeFilter ("committedMetrics" );
176183 filter = SimpleBeanPropertyFilter .serializeAllExcept ("committed" );
0 commit comments