-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
InstrumentedStreams for input & output streams #1314
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3f592b1
InstrumentedStreams for input & output streams
schlosna b9ccdcb
Update InstrumentedStreamsTest.java
schlosna bf9ce7c
Add generated changelog entries
svc-changelog fab5bbc
Merge develop into ds/instrumented-streams
bulldozer-bot[bot] 2faec14
Merge refs/heads/develop into ds/instrumented-streams
bulldozer-bot[bot] e036504
Merge refs/heads/develop into ds/instrumented-streams
bulldozer-bot[bot] 883de34
I/O metrics use tagged metric schema
schlosna 6332695
test copied bytes
schlosna b9505c9
Merge refs/heads/develop into ds/instrumented-streams
bulldozer-bot[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
type: improvement | ||
improvement: | ||
description: |- | ||
InstrumentedStreams for input & output streams | ||
|
||
Track bytes read/written via meter and throughput histogram | ||
links: | ||
- https://github.com/palantir/tritium/pull/1314 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
tritium-lib/src/main/java/com/palantir/tritium/io/ForwardingInputStream.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* (c) Copyright 2022 Palantir Technologies Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.palantir.tritium.io; | ||
|
||
import com.palantir.logsafe.SafeArg; | ||
import com.palantir.logsafe.logger.SafeLogger; | ||
import com.palantir.logsafe.logger.SafeLoggerFactory; | ||
import java.io.FilterInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.Objects; | ||
|
||
abstract class ForwardingInputStream extends FilterInputStream { | ||
private static final SafeLogger log = SafeLoggerFactory.get(ForwardingInputStream.class); | ||
|
||
protected ForwardingInputStream(InputStream in) { | ||
super(in); | ||
} | ||
|
||
protected final InputStream input() { | ||
return super.in; | ||
} | ||
|
||
/** | ||
* Hook to handle number of bytes that will attempt to read. | ||
* Default implementation logs the number of bytes that will attempt to read. | ||
* @param bytesToRead number of bytes that will attempt to read | ||
*/ | ||
protected void before(long bytesToRead) { | ||
if (log.isTraceEnabled()) { | ||
log.trace("Attempting to read", SafeArg.of("bytesToRead", bytesToRead)); | ||
} | ||
} | ||
|
||
/** | ||
* Hook to handle number of bytes that were actually read, or -1 if end-of-stream. | ||
* Default implementation logs the number of bytes read. | ||
* @param bytesRead number of bytes that were read, or -1 if end-of-stream | ||
*/ | ||
protected void after(long bytesRead) { | ||
if (log.isDebugEnabled()) { | ||
log.debug("Read", SafeArg.of("bytesRead", bytesRead)); | ||
} | ||
} | ||
|
||
@Override | ||
public int read(byte[] bytes, int off, int len) throws IOException { | ||
Objects.checkFromIndexSize(off, len, bytes.length); | ||
before(len); | ||
int bytesRead = input().read(bytes, off, len); | ||
after(bytesRead); | ||
return bytesRead; | ||
} | ||
|
||
@Override | ||
public int read(byte[] bytes) throws IOException { | ||
before(bytes.length); | ||
int bytesRead = input().read(bytes); | ||
after(bytesRead); | ||
return bytesRead; | ||
} | ||
|
||
@Override | ||
public int read() throws IOException { | ||
before(1); | ||
int bytesRead = input().read(); | ||
after(bytesRead); | ||
return bytesRead; | ||
} | ||
|
||
@Override | ||
public int readNBytes(byte[] bytes, int off, int len) throws IOException { | ||
before(len); | ||
int bytesRead = input().readNBytes(bytes, off, len); | ||
after(bytesRead); | ||
return bytesRead; | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
tritium-lib/src/main/java/com/palantir/tritium/io/ForwardingOutputStream.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* (c) Copyright 2022 Palantir Technologies Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.palantir.tritium.io; | ||
|
||
import com.palantir.logsafe.SafeArg; | ||
import com.palantir.logsafe.logger.SafeLogger; | ||
import com.palantir.logsafe.logger.SafeLoggerFactory; | ||
import java.io.FilterOutputStream; | ||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.util.Objects; | ||
|
||
abstract class ForwardingOutputStream extends FilterOutputStream { | ||
private static final SafeLogger log = SafeLoggerFactory.get(ForwardingOutputStream.class); | ||
|
||
protected ForwardingOutputStream(OutputStream in) { | ||
super(in); | ||
} | ||
|
||
protected final OutputStream output() { | ||
return super.out; | ||
} | ||
|
||
/** | ||
* Hook to handle number of bytes that will attempt to write. | ||
* Default implementation logs the number of bytes that will attempt to write. | ||
* @param bytesToWrite number of bytes that will attempt to write | ||
*/ | ||
protected void before(long bytesToWrite) { | ||
if (log.isTraceEnabled()) { | ||
log.trace("Attempting to write", SafeArg.of("bytesToWrite", bytesToWrite)); | ||
} | ||
} | ||
|
||
/** | ||
* Hook to handle number of bytes that were actually written. | ||
* Default implementation logs the number of bytes written. | ||
* @param bytesWritten number of bytes that were written | ||
*/ | ||
protected void after(long bytesWritten) { | ||
if (log.isDebugEnabled()) { | ||
log.debug("Wrote", SafeArg.of("bytesWritten", bytesWritten)); | ||
} | ||
} | ||
|
||
@Override | ||
public void write(byte[] bytes, int off, int len) throws IOException { | ||
Objects.checkFromIndexSize(off, len, bytes.length); | ||
before(len); | ||
output().write(bytes, off, len); | ||
after(len); | ||
} | ||
|
||
@Override | ||
public void write(byte[] bytes) throws IOException { | ||
before(bytes.length); | ||
output().write(bytes); | ||
after(bytes.length); | ||
} | ||
|
||
@Override | ||
public void write(int value) throws IOException { | ||
before(1); | ||
output().write(value); | ||
after(1); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
tritium-lib/src/main/java/com/palantir/tritium/io/InstrumentedInputStream.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* (c) Copyright 2022 Palantir Technologies Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.palantir.tritium.io; | ||
|
||
import com.codahale.metrics.Meter; | ||
import com.palantir.logsafe.Preconditions; | ||
import java.io.InputStream; | ||
|
||
final class InstrumentedInputStream extends ForwardingInputStream { | ||
private final Meter bytes; | ||
|
||
InstrumentedInputStream(InputStream in, Meter bytes) { | ||
super(in); | ||
this.bytes = Preconditions.checkNotNull(bytes, "bytes"); | ||
} | ||
|
||
@Override | ||
protected void after(long bytesRead) { | ||
if (bytesRead > -1) { | ||
bytes.mark(bytesRead); | ||
} | ||
super.after(bytesRead); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
tritium-lib/src/main/java/com/palantir/tritium/io/InstrumentedOutputStream.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* (c) Copyright 2022 Palantir Technologies Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.palantir.tritium.io; | ||
|
||
import com.codahale.metrics.Meter; | ||
import com.palantir.logsafe.Preconditions; | ||
import java.io.OutputStream; | ||
|
||
final class InstrumentedOutputStream extends ForwardingOutputStream { | ||
private final Meter bytes; | ||
|
||
InstrumentedOutputStream(OutputStream in, Meter bytes) { | ||
super(in); | ||
this.bytes = Preconditions.checkNotNull(bytes, "bytes"); | ||
} | ||
|
||
@Override | ||
protected void after(long bytesWritten) { | ||
this.bytes.mark(bytesWritten); | ||
super.after(bytesWritten); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
tritium-lib/src/main/java/com/palantir/tritium/io/InstrumentedStreams.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* (c) Copyright 2022 Palantir Technologies Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.palantir.tritium.io; | ||
|
||
import com.google.errorprone.annotations.CompileTimeConstant; | ||
import com.palantir.logsafe.Safe; | ||
import com.palantir.tritium.metrics.registry.TaggedMetricRegistry; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
|
||
public final class InstrumentedStreams { | ||
private InstrumentedStreams() {} | ||
|
||
/** | ||
* Instruments the provided stream to provide a meter tracking bytes read. | ||
* | ||
* @param in input | ||
* @param metrics metric registry | ||
* @param type type of stream being instrumented, must be compile-time safe tag | ||
* @return instrumented input stream | ||
*/ | ||
public static InputStream input( | ||
InputStream in, TaggedMetricRegistry metrics, @Safe @CompileTimeConstant String type) { | ||
return new InstrumentedInputStream(in, IoStreamMetrics.of(metrics).read(type)); | ||
} | ||
|
||
/** | ||
* Instruments the provided stream to provide a meter tracking bytes written. | ||
* | ||
* @param out output | ||
* @param type type of stream being instrumented, must be compile-time safe tag | ||
* @return instrumented output stream | ||
*/ | ||
public static OutputStream output( | ||
OutputStream out, TaggedMetricRegistry metrics, @Safe @CompileTimeConstant String type) { | ||
return new InstrumentedOutputStream(out, IoStreamMetrics.of(metrics).write(type)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
options: | ||
javaPackage: com.palantir.tritium.io | ||
javaVisibility: packagePrivate | ||
namespaces: | ||
io.stream: | ||
docs: Input/Output stream metrics. | ||
metrics: | ||
read: | ||
type: meter | ||
tags: [type] | ||
docs: Measures the rate of bytes read from an InputStream for a specified type. | ||
write: | ||
type: meter | ||
tags: [type] | ||
docs: Measures the rate of bytes written to an OutputStream for a specified type. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I left these
Forwarding*Stream
as package private for now. If one really wants, Apache commons-io'sProxy*Stream
provides similar framework