Skip to content

Commit

Permalink
Addressing PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
velo committed Mar 4, 2020
1 parent 56d3139 commit b6d48e8
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 24 deletions.
29 changes: 25 additions & 4 deletions core/src/main/java/feign/Capability.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,35 @@
import feign.codec.Decoder;
import feign.codec.Encoder;

/**
* Capabilities expose core feign artifacts to implementations so parts of core can be customized
* around the time the client being built.
*
* For instance, capabilities take the {@link Client}, make changes to it and feed the modified
* version back to feign.
*
* @see Metrics5Capability
*/
public interface Capability {

static <E> E enrich(E target, List<Capability> capabilities) {

static <E> E enrich(E componentToEnrich, List<Capability> capabilities) {
return capabilities.stream()
// invoke each individual capability and feed the result to the next one.
// This is equivalent to:
// Capability cap1 = ...;
// Capability cap2 = ...;
// Capability cap2 = ...;
// Contract contract = ...;
// Contract contract1 = cap1.enrich(contract);
// Contract contract2 = cap2.enrich(contract1);
// Contract contract3 = cap3.enrich(contract2);
// or in a more compact version
// Contract enrichedContract = cap3.enrich(cap2.enrich(cap1.enrich(contract)));
.reduce(
target,
(c, capability) -> invoke(c, capability),
(a, b) -> b);
componentToEnrich,
(component, capability) -> invoke(component, capability),
(component, enrichedComponent) -> enrichedComponent);
}

static <E> E invoke(E target, Capability capability) {
Expand Down
5 changes: 0 additions & 5 deletions metrics5/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,6 @@
<artifactId>metrics-core</artifactId>
<version>5.0.0</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>28.0-jre</version>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>java-hamcrest</artifactId>
Expand Down
105 changes: 105 additions & 0 deletions metrics5/src/main/java/feign/metrics5/CountingInputStream.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/**
* Copyright 2012-2020 The Feign Authors
*
* 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.
*/
/*
* Copyright (C) 2007 The Guava Authors
*
* 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 feign.metrics5;

import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import feign.Util;

/**
* Copy from guava CountingInputStream
*
* An {@link InputStream} that counts the number of bytes read.
*
* @author Chris Nokleberg
* @since 1.0
*/
public final class CountingInputStream extends FilterInputStream {

private long count;
private long mark = -1;

/**
* Wraps another input stream, counting the number of bytes read.
*
* @param in the input stream to be wrapped
*/
public CountingInputStream(InputStream in) {
super(Util.checkNotNull(in, "InputStream must not be null"));
}

/** Returns the number of bytes read. */
public long getCount() {
return count;
}

@Override
public int read() throws IOException {
final int result = in.read();
if (result != -1) {
count++;
}
return result;
}

@Override
public int read(byte[] b, int off, int len) throws IOException {
final int result = in.read(b, off, len);
if (result != -1) {
count += result;
}
return result;
}

@Override
public long skip(long n) throws IOException {
final long result = in.skip(n);
count += result;
return result;
}

@Override
public synchronized void mark(int readlimit) {
in.mark(readlimit);
mark = count;
// it's okay to mark even if mark isn't supported, as reset won't work
}

@Override
public synchronized void reset() throws IOException {
if (!in.markSupported()) {
throw new IOException("Mark not supported");
}
if (mark == -1) {
throw new IOException("Mark not set");
}

in.reset();
count = mark;
}
}
10 changes: 5 additions & 5 deletions metrics5/src/main/java/feign/metrics5/FeignMetricName.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,26 @@
import io.dropwizard.metrics5.MetricName;
import io.dropwizard.metrics5.MetricRegistry;

final class FeignMetricName {
public final class FeignMetricName {

private final Class<?> meteredComponent;


FeignMetricName(Class<?> meteredComponent) {
public FeignMetricName(Class<?> meteredComponent) {
this.meteredComponent = meteredComponent;
}


MetricName metricName(MethodMetadata methodMetadata, Target<?> target, String suffix) {
public MetricName metricName(MethodMetadata methodMetadata, Target<?> target, String suffix) {
return metricName(methodMetadata, target)
.resolve(suffix);
}

MetricName metricName(MethodMetadata methodMetadata, Target<?> target) {
public MetricName metricName(MethodMetadata methodMetadata, Target<?> target) {
return metricName(methodMetadata.targetType(), methodMetadata.method(), target.url());
}

MetricName metricName(Class<?> targetType, Method method, String url) {
public MetricName metricName(Class<?> targetType, Method method, String url) {
return MetricRegistry.name(meteredComponent)
.tagged("client", targetType.getName())
.tagged("method", method.getName())
Expand Down
5 changes: 2 additions & 3 deletions metrics5/src/main/java/feign/metrics5/MeteredBody.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
package feign.metrics5;

import static feign.Util.UTF_8;
import com.google.common.io.CountingInputStream;
import java.io.*;
import java.nio.charset.Charset;
import java.util.function.Supplier;
Expand All @@ -23,12 +22,12 @@
/**
* {@link Body} implementation that keeps track of how many bytes are read.
*/
final class MeteredBody implements Body {
public final class MeteredBody implements Body {

private final Body delegate;
private Supplier<Long> count;

MeteredBody(Body body) {
public MeteredBody(Body body) {
this.delegate = body;
count = () -> 0L;
}
Expand Down
2 changes: 1 addition & 1 deletion metrics5/src/main/java/feign/metrics5/MeteredClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
/**
* Warp feign {@link Client} with metrics.
*/
final class MeteredClient implements Client {
public class MeteredClient implements Client {

private final Client client;
private final MetricRegistry metricRegistry;
Expand Down
5 changes: 2 additions & 3 deletions metrics5/src/main/java/feign/metrics5/MeteredDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,18 @@

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.concurrent.TimeUnit;
import feign.FeignException;
import feign.RequestTemplate;
import feign.Response;
import feign.codec.DecodeException;
import feign.codec.Decoder;
import io.dropwizard.metrics5.*;
import io.dropwizard.metrics5.MetricRegistry;
import io.dropwizard.metrics5.Timer.Context;

/**
* Warp feign {@link Decoder} with metrics.
*/
final class MeteredDecoder implements Decoder {
public class MeteredDecoder implements Decoder {

private final Decoder decoder;
private final MetricRegistry metricRegistry;
Expand Down
2 changes: 1 addition & 1 deletion metrics5/src/main/java/feign/metrics5/MeteredEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
/**
* Warp feign {@link Encoder} with metrics.
*/
final class MeteredEncoder implements Encoder {
public class MeteredEncoder implements Encoder {

private final Encoder encoder;
private final MetricRegistry metricRegistry;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
/**
* Warp feign {@link InvocationHandler} with metrics.
*/
final class MeteredInvocationHandleFactory implements InvocationHandlerFactory {
public class MeteredInvocationHandleFactory implements InvocationHandlerFactory {

private static final Logger LOG = LoggerFactory.getLogger(MeteredInvocationHandleFactory.class);

Expand All @@ -47,7 +47,7 @@ final class MeteredInvocationHandleFactory implements InvocationHandlerFactory {

private final MetricSuppliers metricSuppliers;

MeteredInvocationHandleFactory(InvocationHandlerFactory invocationHandler,
public MeteredInvocationHandleFactory(InvocationHandlerFactory invocationHandler,
MetricRegistry metricRegistry, MetricSuppliers metricSuppliers) {
this.invocationHandler = invocationHandler;
this.metricRegistry = metricRegistry;
Expand Down

0 comments on commit b6d48e8

Please sign in to comment.