Tracks a collections of channel tracing events for a channel/subchannel.
| 37 | * Tracks a collections of channel tracing events for a channel/subchannel. |
| 38 | */ |
| 39 | final class ChannelTracer { |
| 40 | // The logs go to ChannelLogger's logger so that user can control the logging level on that public |
| 41 | // class rather than on this internal class. |
| 42 | static final Logger logger = Logger.getLogger(ChannelLogger.class.getName()); |
| 43 | private final Object lock = new Object(); |
| 44 | private final InternalLogId logId; |
| 45 | @GuardedBy("lock") |
| 46 | @Nullable |
| 47 | private final Collection<Event> events; |
| 48 | private final long channelCreationTimeNanos; |
| 49 | |
| 50 | @GuardedBy("lock") |
| 51 | private int eventsLogged; |
| 52 | |
| 53 | /** |
| 54 | * Creates a channel tracer and log the creation event of the underlying channel. |
| 55 | * |
| 56 | * @param logId logId will be prepended to the logs logged to Java logger |
| 57 | * @param maxEvents maximum number of events that are retained in memory. If not a positive |
| 58 | * number no events will be retained, but they will still be sent to the Java logger. |
| 59 | * @param channelCreationTimeNanos the creation time of the entity being traced |
| 60 | * @param description a description of the entity being traced |
| 61 | */ |
| 62 | ChannelTracer( |
| 63 | InternalLogId logId, final int maxEvents, long channelCreationTimeNanos, String description) { |
| 64 | checkNotNull(description, "description"); |
| 65 | this.logId = checkNotNull(logId, "logId"); |
| 66 | if (maxEvents > 0) { |
| 67 | events = new ArrayDeque<Event>() { |
| 68 | @GuardedBy("lock") |
| 69 | @Override |
| 70 | public boolean add(Event event) { |
| 71 | if (size() == maxEvents) { |
| 72 | removeFirst(); |
| 73 | } |
| 74 | eventsLogged++; |
| 75 | return super.add(event); |
| 76 | } |
| 77 | }; |
| 78 | } else { |
| 79 | events = null; |
| 80 | } |
| 81 | this.channelCreationTimeNanos = channelCreationTimeNanos; |
| 82 | |
| 83 | reportEvent(new ChannelTrace.Event.Builder() |
| 84 | .setDescription(description + " created") |
| 85 | .setSeverity(ChannelTrace.Event.Severity.CT_INFO) |
| 86 | // passing the timestamp in as a parameter instead of computing it right here because when |
| 87 | // parent channel and subchannel both report the same event of the subchannel (e.g. creation |
| 88 | // event of the subchannel) we want the timestamps to be exactly the same. |
| 89 | .setTimestampNanos(channelCreationTimeNanos) |
| 90 | .build()); |
| 91 | } |
| 92 | |
| 93 | void reportEvent(Event event) { |
| 94 | Level logLevel; |
| 95 | switch (event.severity) { |
| 96 | case CT_ERROR: |