Skip to content

Commit

Permalink
Add HttpTracing(Client|Service)Builder
Browse files Browse the repository at this point in the history
Motivation:
A user currently has to build `Tracing` by him/herself and set `currentTraceContext` manually.
We could add `HttpTracing(Client|Service)Builder` which builds a `Tracing` for a user.

Modifications:
- Add `HttpTracing(Client|Service)`Builder
- May fix a bug where `decorateScope` is not called
- Deprecation
  - The consturctor of `HttpTracingService` is not public anymore. A user should use the builder or the static factory method.

Result:
- A user can easidy build an `HttpTracing(Client|Service)` using builders.
  • Loading branch information
minwoox committed Jun 18, 2019
1 parent a39407a commit 7fd864a
Show file tree
Hide file tree
Showing 14 changed files with 560 additions and 141 deletions.
Expand Up @@ -16,6 +16,9 @@

package com.linecorp.armeria.common;

import com.google.common.base.MoreObjects;
import com.google.common.base.MoreObjects.ToStringHelper;

final class DefaultAggregatedHttpRequest extends AbstractAggregatedHttpMessage
implements AggregatedHttpRequest {

Expand Down Expand Up @@ -51,4 +54,43 @@ public String scheme() {
public String authority() {
return headers.authority();
}

@Override
public int hashCode() {
int result = headers().hashCode();
result = 31 * result + content().hashCode();
result = 31 * result + trailers().hashCode();
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}

if (!(obj instanceof AggregatedHttpRequest)) {
return false;
}

final AggregatedHttpRequest that = (AggregatedHttpRequest) obj;

return headers().equals(that.headers()) &&
content().equals(that.content()) &&
trailers().equals(that.trailers());
}

@Override
public String toString() {
final ToStringHelper helper = MoreObjects.toStringHelper(this);

helper.add("headers", headers())
.add("content", content());

if (!trailers().isEmpty()) {
helper.add("trailers", trailers());
}

return helper.toString();
}
}
Expand Up @@ -50,21 +50,12 @@ public ResponseHeaders headers() {
}

@Override
public String toString() {
final ToStringHelper helper = MoreObjects.toStringHelper(this);

if (!informationals().isEmpty()) {
helper.add("informationals", informationals());
}

helper.add("headers", headers())
.add("content", content());

if (!trailers().isEmpty()) {
helper.add("trailers", trailers());
}

return helper.toString();
public int hashCode() {
int result = informationals().hashCode();
result = 31 * result + headers().hashCode();
result = 31 * result + content().hashCode();
result = 31 * result + trailers().hashCode();
return result;
}

@Override
Expand All @@ -73,11 +64,11 @@ public boolean equals(Object obj) {
return true;
}

if (!(obj instanceof DefaultAggregatedHttpResponse)) {
if (!(obj instanceof AggregatedHttpResponse)) {
return false;
}

final DefaultAggregatedHttpResponse that = (DefaultAggregatedHttpResponse) obj;
final AggregatedHttpResponse that = (AggregatedHttpResponse) obj;

return informationals().equals(that.informationals()) &&
headers().equals(that.headers()) &&
Expand All @@ -86,11 +77,20 @@ public boolean equals(Object obj) {
}

@Override
public int hashCode() {
int result = informationals().hashCode();
result = 31 * result + headers().hashCode();
result = 31 * result + content().hashCode();
result = 31 * result + trailers().hashCode();
return result;
public String toString() {
final ToStringHelper helper = MoreObjects.toStringHelper(this);

if (!informationals().isEmpty()) {
helper.add("informationals", informationals());
}

helper.add("headers", headers())
.add("content", content());

if (!trailers().isEmpty()) {
helper.add("trailers", trailers());
}

return helper.toString();
}
}
Expand Up @@ -120,11 +120,11 @@ public boolean equals(Object obj) {
return true;
}

if (!(obj instanceof DefaultRpcRequest)) {
if (!(obj instanceof RpcRequest)) {
return false;
}

final DefaultRpcRequest that = (DefaultRpcRequest) obj;
final RpcRequest that = (RpcRequest) obj;
return method().equals(that.method()) &&
params().equals(that.params());
}
Expand Down
Expand Up @@ -96,16 +96,16 @@ public boolean equals(Object obj) {
return true;
}

if (!(obj instanceof DefaultServerSentEvent)) {
if (!(obj instanceof ServerSentEvent)) {
return false;
}

final DefaultServerSentEvent that = (DefaultServerSentEvent) obj;
return Objects.equals(id, that.id) &&
Objects.equals(event, that.event) &&
Objects.equals(retry, that.retry) &&
Objects.equals(comment, that.comment) &&
Objects.equals(data, that.data);
final ServerSentEvent that = (ServerSentEvent) obj;
return Objects.equals(id, that.id()) &&
Objects.equals(event, that.event()) &&
Objects.equals(retry, that.retry()) &&
Objects.equals(comment, that.comment()) &&
Objects.equals(data, that.data());
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion site/src/sphinx/advanced-zipkin.rst
Expand Up @@ -3,4 +3,4 @@
Zipkin integration
==================

TBW - See :api:`HttpTracingService` and :api:`HttpTracingClient`
TBW - See :api:`TracingService` and :api:`TracingClient`
Expand Up @@ -16,7 +16,7 @@

package com.linecorp.armeria.client.tracing;

import static com.linecorp.armeria.common.tracing.RequestContextCurrentTraceContext.ensureScopeUsesRequestContext;
import static com.linecorp.armeria.client.tracing.TracingClient.checkTracing;

import java.net.InetAddress;
import java.net.InetSocketAddress;
Expand Down Expand Up @@ -54,32 +54,34 @@
*
* <p>This decorator puts trace data into HTTP headers. The specifications of header names and its values
* correspond to <a href="http://zipkin.io/">Zipkin</a>.
*
* @deprecated Use {@link TracingClient}.
*/
public class HttpTracingClient extends SimpleDecoratingClient<HttpRequest, HttpResponse> {
@Deprecated
public final class HttpTracingClient extends SimpleDecoratingClient<HttpRequest, HttpResponse> {

private static final Logger logger = LoggerFactory.getLogger(HttpTracingClient.class);

/**
* Creates a new tracing {@link Client} decorator using the specified {@link Tracing} instance.
*
* @deprecated Use {@link TracingClient#newDecorator(Tracing)}.
*/
@Deprecated
public static Function<Client<HttpRequest, HttpResponse>, HttpTracingClient> newDecorator(Tracing tracing) {
return newDecorator(tracing, null);
}

/**
* Creates a new tracing {@link Client} decorator using the specified {@link Tracing} instance
* and remote service name.
* and the remote service name.
*
* @deprecated Use {@link TracingClient#newDecorator(Tracing, String)}.
*/
@Deprecated
public static Function<Client<HttpRequest, HttpResponse>, HttpTracingClient> newDecorator(
Tracing tracing,
@Nullable String remoteServiceName) {
try {
ensureScopeUsesRequestContext(tracing);
} catch (IllegalStateException e) {
logger.warn("{} - it is appropriate to ignore this warning if this client is not being used " +
"inside an Armeria server (e.g., this is a normal spring-mvc tomcat server).",
e.getMessage());
}
Tracing tracing, @Nullable String remoteServiceName) {
checkTracing(tracing);
return delegate -> new HttpTracingClient(delegate, tracing, remoteServiceName);
}

Expand All @@ -91,8 +93,8 @@ public static Function<Client<HttpRequest, HttpResponse>, HttpTracingClient> new
/**
* Creates a new instance.
*/
protected HttpTracingClient(Client<HttpRequest, HttpResponse> delegate, Tracing tracing,
@Nullable String remoteServiceName) {
HttpTracingClient(Client<HttpRequest, HttpResponse> delegate, Tracing tracing,
@Nullable String remoteServiceName) {
super(delegate);
tracer = tracing.tracer();
injector = tracing.propagationFactory().create(AsciiStringKeyFactory.INSTANCE)
Expand Down

0 comments on commit 7fd864a

Please sign in to comment.