Skip to content
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 9 commits into from
Jan 13, 2022
8 changes: 8 additions & 0 deletions changelog/@unreleased/pr-1314.v2.yml
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
1 change: 1 addition & 0 deletions tritium-lib/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
apply plugin: 'com.palantir.external-publish-jar'
apply plugin: 'com.palantir.metric-schema'
apply plugin: 'com.palantir.revapi'

dependencies {
Expand Down
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;
}
}
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 {
Copy link
Contributor Author

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's Proxy*Stream provides similar framework

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);
}
}
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);
}
}
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);
}
}
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));
}
}
15 changes: 15 additions & 0 deletions tritium-lib/src/main/metrics/metrics.yml
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.
Loading