Provides factories for StreamTracer that records stats to Census. On the client-side, a factory is created for each call, and the factory creates a stream tracer for each attempt. If there is no stream created when the call is ended, we still create a tracer. It's the tracer that reports
| 77 | * it's the tracer that reports the summary to Census. |
| 78 | */ |
| 79 | final class CensusStatsModule { |
| 80 | private static final Logger logger = Logger.getLogger(CensusStatsModule.class.getName()); |
| 81 | private static final double NANOS_PER_MILLI = TimeUnit.MILLISECONDS.toNanos(1); |
| 82 | |
| 83 | private final Tagger tagger; |
| 84 | private final StatsRecorder statsRecorder; |
| 85 | private final Supplier<Stopwatch> stopwatchSupplier; |
| 86 | @VisibleForTesting |
| 87 | final Metadata.Key<TagContext> statsHeader; |
| 88 | private final boolean propagateTags; |
| 89 | private final boolean recordStartedRpcs; |
| 90 | private final boolean recordFinishedRpcs; |
| 91 | private final boolean recordRealTimeMetrics; |
| 92 | private final boolean recordRetryMetrics; |
| 93 | |
| 94 | /** |
| 95 | * Creates a {@link CensusStatsModule} with the default OpenCensus implementation. |
| 96 | */ |
| 97 | CensusStatsModule(Supplier<Stopwatch> stopwatchSupplier, |
| 98 | boolean propagateTags, boolean recordStartedRpcs, boolean recordFinishedRpcs, |
| 99 | boolean recordRealTimeMetrics, boolean recordRetryMetrics) { |
| 100 | this( |
| 101 | Tags.getTagger(), |
| 102 | Tags.getTagPropagationComponent().getBinarySerializer(), |
| 103 | Stats.getStatsRecorder(), |
| 104 | stopwatchSupplier, |
| 105 | propagateTags, recordStartedRpcs, recordFinishedRpcs, recordRealTimeMetrics, |
| 106 | recordRetryMetrics); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Creates a {@link CensusStatsModule} with the given OpenCensus implementation. |
| 111 | */ |
| 112 | CensusStatsModule( |
| 113 | final Tagger tagger, |
| 114 | final TagContextBinarySerializer tagCtxSerializer, |
| 115 | StatsRecorder statsRecorder, Supplier<Stopwatch> stopwatchSupplier, |
| 116 | boolean propagateTags, boolean recordStartedRpcs, boolean recordFinishedRpcs, |
| 117 | boolean recordRealTimeMetrics, boolean recordRetryMetrics) { |
| 118 | this.tagger = checkNotNull(tagger, "tagger"); |
| 119 | this.statsRecorder = checkNotNull(statsRecorder, "statsRecorder"); |
| 120 | checkNotNull(tagCtxSerializer, "tagCtxSerializer"); |
| 121 | this.stopwatchSupplier = checkNotNull(stopwatchSupplier, "stopwatchSupplier"); |
| 122 | this.propagateTags = propagateTags; |
| 123 | this.recordStartedRpcs = recordStartedRpcs; |
| 124 | this.recordFinishedRpcs = recordFinishedRpcs; |
| 125 | this.recordRealTimeMetrics = recordRealTimeMetrics; |
| 126 | this.recordRetryMetrics = recordRetryMetrics; |
| 127 | this.statsHeader = |
| 128 | Metadata.Key.of("grpc-tags-bin", new Metadata.BinaryMarshaller<TagContext>() { |
| 129 | @Override |
| 130 | public byte[] toBytes(TagContext context) { |
| 131 | // TODO(carl-mastrangelo): currently we only make sure the correctness. We may need to |
| 132 | // optimize out the allocation and copy in the future. |
| 133 | try { |
| 134 | return tagCtxSerializer.toByteArray(context); |
| 135 | } catch (TagContextSerializationException e) { |
| 136 | throw new RuntimeException(e); |