42
42
import com .arpnetworking .http .RequestReply ;
43
43
import com .arpnetworking .metrics .common .parsers .Parser ;
44
44
import com .arpnetworking .metrics .common .parsers .exceptions .ParsingException ;
45
+ import com .arpnetworking .metrics .incubator .PeriodicMetrics ;
46
+ import com .arpnetworking .metrics .mad .model .Metric ;
45
47
import com .arpnetworking .metrics .mad .model .Record ;
48
+ import com .arpnetworking .metrics .mad .model .statistics .StatisticFactory ;
46
49
import com .arpnetworking .steno .Logger ;
47
50
import com .arpnetworking .steno .LoggerFactory ;
51
+ import com .arpnetworking .tsdcore .model .CalculatedValue ;
52
+ import com .fasterxml .jackson .annotation .JacksonInject ;
48
53
import com .google .common .collect .ImmutableMultimap ;
49
54
import net .sf .oval .constraint .NotNull ;
50
55
@@ -74,8 +79,10 @@ protected Props createProps() {
74
79
protected HttpSource (final Builder <?, ? extends HttpSource > builder ) {
75
80
super (builder );
76
81
_parser = builder ._parser ;
82
+ _periodicMetrics = builder ._periodicMetrics ;
77
83
}
78
84
85
+ private final PeriodicMetrics _periodicMetrics ;
79
86
private final Parser <List <Record >, com .arpnetworking .metrics .mad .model .HttpRequest > _parser ;
80
87
81
88
/**
@@ -98,6 +105,7 @@ public Receive createReceive() {
98
105
.match (RequestReply .class , requestReply -> {
99
106
// TODO(barp): Fix the ugly HttpRequest cast here due to java vs scala dsl
100
107
akka .stream .javadsl .Source .single (requestReply .getRequest ())
108
+ .log ("http source stream failure" )
101
109
.via (_processGraph )
102
110
.toMat (_sink , Keep .right ())
103
111
.run (_materializer )
@@ -127,6 +135,8 @@ public Receive createReceive() {
127
135
* @param source The {@link HttpSource} to send notifications through.
128
136
*/
129
137
/* package private */ Actor (final HttpSource source ) {
138
+ _periodicMetrics = source ._periodicMetrics ;
139
+ _metricSafeName = source .getMetricSafeName ();
130
140
_parser = source ._parser ;
131
141
_sink = Sink .foreach (source ::notify );
132
142
_materializer = ActorMaterializer .create (
@@ -193,14 +203,36 @@ private static com.arpnetworking.metrics.mad.model.HttpRequest mapModel(
193
203
194
204
private List <Record > parseRecords (final com .arpnetworking .metrics .mad .model .HttpRequest request )
195
205
throws ParsingException {
196
- return _parser .parse (request );
206
+ final List <Record > records = _parser .parse (request );
207
+ long samples = 0 ;
208
+ for (final Record record : records ) {
209
+ for (final Metric metric : record .getMetrics ().values ()) {
210
+ samples += metric .getValues ().size ();
211
+ final List <CalculatedValue <?>> countStatistic =
212
+ metric .getStatistics ().get (STATISTIC_FACTORY .getStatistic ("count" ));
213
+ if (countStatistic != null ) {
214
+ samples += countStatistic .stream ()
215
+ .map (s -> s .getValue ().getValue ())
216
+ .reduce (Double ::sum )
217
+ .orElse (0.0d );
218
+ }
219
+ }
220
+ }
221
+ _periodicMetrics .recordGauge (
222
+ String .format ("sources/http/%s/metric_samples" , _metricSafeName ),
223
+ samples );
224
+
225
+ return records ;
197
226
}
198
227
228
+ private final PeriodicMetrics _periodicMetrics ;
229
+ private final String _metricSafeName ;
199
230
private final Sink <Record , CompletionStage <Done >> _sink ;
200
231
private final Parser <List <Record >, com .arpnetworking .metrics .mad .model .HttpRequest > _parser ;
201
232
private final Materializer _materializer ;
202
233
private final Graph <FlowShape <HttpRequest , Record >, NotUsed > _processGraph ;
203
234
235
+ private static final StatisticFactory STATISTIC_FACTORY = new StatisticFactory ();
204
236
private static final Logger BAD_REQUEST_LOGGER =
205
237
LoggerFactory .getRateLimitLogger (HttpSource .class , Duration .ofSeconds (30 ));
206
238
}
@@ -235,7 +267,22 @@ public B setParser(final Parser<List<Record>, com.arpnetworking.metrics.mad.mode
235
267
return self ();
236
268
}
237
269
270
+ /**
271
+ * Sets the periodic metrics instance.
272
+ *
273
+ * @param value The periodic metrics.
274
+ * @return This instance of {@link Builder}
275
+ */
276
+ public final B setPeriodicMetrics (final PeriodicMetrics value ) {
277
+ _periodicMetrics = value ;
278
+ return self ();
279
+ }
280
+
238
281
@ NotNull
239
282
private Parser <List <Record >, com .arpnetworking .metrics .mad .model .HttpRequest > _parser ;
283
+
284
+ @ NotNull
285
+ @ JacksonInject
286
+ private PeriodicMetrics _periodicMetrics ;
240
287
}
241
288
}
0 commit comments