|
| 1 | +/* |
| 2 | + * Copyright (c) 2022, FusionAuth, All Rights Reserved |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, |
| 11 | + * software distributed under the License is distributed on an |
| 12 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, |
| 13 | + * either express or implied. See the License for the specific |
| 14 | + * language governing permissions and limitations under the License. |
| 15 | + */ |
| 16 | +package io.fusionauth.http.server; |
| 17 | + |
| 18 | +import java.util.concurrent.atomic.AtomicLong; |
| 19 | + |
| 20 | +/** |
| 21 | + * A thread safe counting instrumenter for the HTTPServer, that ensures accurate data but could impact performance. |
| 22 | + * |
| 23 | + * @author Brian Pontarelli |
| 24 | + */ |
| 25 | +@SuppressWarnings("unused") |
| 26 | +public class ThreadSafeCountingInstrumenter implements Instrumenter { |
| 27 | + private final AtomicLong badRequests = new AtomicLong(); |
| 28 | + |
| 29 | + private final AtomicLong bytesRead = new AtomicLong(); |
| 30 | + |
| 31 | + private final AtomicLong bytesWritten = new AtomicLong(); |
| 32 | + |
| 33 | + private final AtomicLong chunkedRequests = new AtomicLong(); |
| 34 | + |
| 35 | + private final AtomicLong chunkedResponses = new AtomicLong(); |
| 36 | + |
| 37 | + private final AtomicLong closedConnections = new AtomicLong(); |
| 38 | + |
| 39 | + private final AtomicLong connections = new AtomicLong(); |
| 40 | + |
| 41 | + private final AtomicLong startedCount = new AtomicLong(); |
| 42 | + |
| 43 | + private final AtomicLong threadCount = new AtomicLong(); |
| 44 | + |
| 45 | + @Override |
| 46 | + public void acceptedConnection() { |
| 47 | + connections.incrementAndGet(); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public void badRequest() { |
| 52 | + badRequests.incrementAndGet(); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public void chunkedRequest() { |
| 57 | + chunkedRequests.incrementAndGet(); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public void chunkedResponse() { |
| 62 | + chunkedResponses.incrementAndGet(); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public void connectionClosed() { |
| 67 | + closedConnections.incrementAndGet(); |
| 68 | + } |
| 69 | + |
| 70 | + public long getBadRequests() { |
| 71 | + return badRequests.get(); |
| 72 | + } |
| 73 | + |
| 74 | + public long getBytesRead() { |
| 75 | + return bytesRead.get(); |
| 76 | + } |
| 77 | + |
| 78 | + public long getBytesWritten() { |
| 79 | + return bytesWritten.get(); |
| 80 | + } |
| 81 | + |
| 82 | + public long getChunkedRequests() { |
| 83 | + return chunkedRequests.get(); |
| 84 | + } |
| 85 | + |
| 86 | + public long getChunkedResponses() { |
| 87 | + return chunkedResponses.get(); |
| 88 | + } |
| 89 | + |
| 90 | + public long getClosedConnections() { |
| 91 | + return closedConnections.get(); |
| 92 | + } |
| 93 | + |
| 94 | + public long getConnections() { |
| 95 | + return connections.get(); |
| 96 | + } |
| 97 | + |
| 98 | + public long getStartedCount() { |
| 99 | + return startedCount.get(); |
| 100 | + } |
| 101 | + |
| 102 | + public long getThreadCount() { |
| 103 | + return threadCount.get(); |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public void readFromClient(long bytes) { |
| 108 | + bytesRead.addAndGet(bytes); |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public void serverStarted() { |
| 113 | + startedCount.incrementAndGet(); |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public void threadExited() { |
| 118 | + threadCount.decrementAndGet(); |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public void threadStarted() { |
| 123 | + threadCount.incrementAndGet(); |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public void wroteToClient(long bytes) { |
| 128 | + bytesWritten.addAndGet(bytes); |
| 129 | + } |
| 130 | +} |
0 commit comments