-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds HttpSender to aid proper handling of HttpEndpointSupplier (#253)
Signed-off-by: Adrian Cole <[email protected]>
- Loading branch information
1 parent
be50820
commit ec4e01c
Showing
20 changed files
with
901 additions
and
967 deletions.
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
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
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,152 @@ | ||
/* | ||
* Copyright 2016-2024 The OpenZipkin 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 zipkin2.reporter; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
import java.util.logging.Logger; | ||
import zipkin2.reporter.HttpEndpointSupplier.Factory; | ||
|
||
import static zipkin2.reporter.Call.propagateIfFatal; | ||
|
||
/** | ||
* Reports spans to Zipkin, using its <a href="https://zipkin.io/zipkin-api/#/">POST</a> endpoint. | ||
* | ||
* <p>Calls to {@linkplain #postSpans(Object, Object)} happen on the same async reporting thread, | ||
* but {@linkplain #close()} might be called from any thread. | ||
* | ||
* @since 3.3 | ||
*/ | ||
public abstract class HttpSender<U, B> extends BytesMessageSender.Base { | ||
final Logger logger; | ||
final HttpEndpointSupplier endpointSupplier; | ||
final U endpoint; | ||
|
||
/** close is typically called from a different thread */ | ||
final AtomicBoolean closeCalled = new AtomicBoolean(); | ||
|
||
/** | ||
* Called each invocation of {@linkplain #postSpans(Object, Object)}, unless the | ||
* {@linkplain HttpEndpointSupplier} is a {@linkplain ConstantHttpEndpointSupplier}, | ||
* Implementations should perform any validation needed here. | ||
* | ||
* @since 3.3 | ||
*/ | ||
protected abstract U newEndpoint(String endpoint); | ||
|
||
/** | ||
* Creates a new POST body from the encoded spans. | ||
* | ||
* <p>Below is the simplest implementation, when {@linkplain HttpSender#<B>} is a byte array. | ||
* <pre>{@code | ||
* @Override protected byte[] newBody(List<byte[]> encodedSpans) { | ||
* return encoding.encode(encodedSpans); | ||
* } | ||
* }</pre> | ||
* | ||
* <p>If you need the "Content-Type" value, you can access it via {@link Encoding#mediaType()}. | ||
* | ||
* @since 3.3 | ||
*/ | ||
protected abstract B newBody(List<byte[]> encodedSpans); | ||
|
||
/** | ||
* Implement to POST spans to the given endpoint. | ||
* | ||
* <p>If you need the "Content-Type" value, you can access it via {@link Encoding#mediaType()}. | ||
* | ||
* @since 3.3 | ||
*/ | ||
protected abstract void postSpans(U endpoint, B body) throws IOException; | ||
|
||
/** | ||
* Override to close any resources. | ||
* | ||
* @since 3.3 | ||
*/ | ||
protected void doClose() { | ||
} | ||
|
||
protected HttpSender(Encoding encoding, Factory endpointSupplierFactory, String endpoint) { | ||
this(Logger.getLogger(HttpSender.class.getName()), encoding, endpointSupplierFactory, endpoint); | ||
} | ||
|
||
HttpSender(Logger logger, Encoding encoding, Factory endpointSupplierFactory, String endpoint) { | ||
super(encoding); | ||
this.logger = logger; | ||
if (endpointSupplierFactory == null) { | ||
throw new NullPointerException("endpointSupplierFactory == null"); | ||
} | ||
if (endpoint == null) throw new NullPointerException("endpoint == null"); | ||
|
||
HttpEndpointSupplier endpointSupplier = endpointSupplierFactory.create(endpoint); | ||
if (endpointSupplier == null) { | ||
throw new NullPointerException("endpointSupplierFactory.create() returned null"); | ||
} | ||
if (endpointSupplier instanceof ConstantHttpEndpointSupplier) { | ||
this.endpoint = nextEndpoint(endpointSupplier); | ||
closeQuietly(endpointSupplier); | ||
this.endpointSupplier = null; | ||
} else { | ||
this.endpoint = null; | ||
this.endpointSupplier = endpointSupplier; | ||
} | ||
} | ||
|
||
final U nextEndpoint(HttpEndpointSupplier endpointSupplier) { | ||
String endpoint = endpointSupplier.get(); // eagerly resolve the endpoint | ||
if (endpoint == null) throw new NullPointerException("endpointSupplier.get() returned null"); | ||
return newEndpoint(endpoint); | ||
} | ||
|
||
/** Defaults to the most common max message size: 512KB. */ | ||
@Override public int messageMaxBytes() { | ||
return 512 * 1024; | ||
} | ||
|
||
/** Sends spans as an HTTP POST request. */ | ||
@Override public final void send(List<byte[]> encodedSpans) throws IOException { | ||
if (closeCalled.get()) throw new ClosedSenderException(); | ||
U endpoint = this.endpoint; | ||
if (endpoint == null) endpoint = nextEndpoint(endpointSupplier); | ||
B body = newBody(encodedSpans); | ||
if (body == null) throw new NullPointerException("newBody(encodedSpans) returned null"); | ||
postSpans(endpoint, newBody(encodedSpans)); | ||
} | ||
|
||
@Override public final void close() { | ||
if (!closeCalled.compareAndSet(false, true)) return; // already closed | ||
closeQuietly(endpointSupplier); | ||
doClose(); | ||
} | ||
|
||
final void closeQuietly(HttpEndpointSupplier endpointSupplier) { | ||
if (endpointSupplier == null) return; | ||
try { | ||
endpointSupplier.close(); | ||
} catch (Throwable t) { | ||
propagateIfFatal(t); | ||
logger.fine("ignoring error closing endpoint supplier: " + t.getMessage()); | ||
} | ||
} | ||
|
||
@Override public String toString() { | ||
String name = getClass().getSimpleName(); | ||
if (endpoint != null) { | ||
return name + "{" + endpoint + "}"; | ||
} | ||
return name + "{" + endpointSupplier + "}"; | ||
} | ||
} |
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
104 changes: 104 additions & 0 deletions
104
core/src/main/java/zipkin2/reporter/internal/SenderAdapter.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,104 @@ | ||
/* | ||
* Copyright 2016-2024 The OpenZipkin 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 zipkin2.reporter.internal; | ||
|
||
import java.io.IOException; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import zipkin2.reporter.BytesMessageSender; | ||
import zipkin2.reporter.Call; | ||
import zipkin2.reporter.Callback; | ||
import zipkin2.reporter.CheckResult; | ||
import zipkin2.reporter.Encoding; | ||
import zipkin2.reporter.Sender; | ||
|
||
/** | ||
* Reduces burden on types that need to extend {@linkplain Sender}. | ||
*/ | ||
public abstract class SenderAdapter extends Sender { | ||
protected abstract BytesMessageSender delegate(); | ||
|
||
@Override public final int messageSizeInBytes(List<byte[]> encodedSpans) { | ||
return delegate().messageSizeInBytes(encodedSpans); | ||
} | ||
|
||
@Override public final int messageSizeInBytes(int encodedSizeInBytes) { | ||
return delegate().messageSizeInBytes(encodedSizeInBytes); | ||
} | ||
|
||
@Override public final Encoding encoding() { | ||
return delegate().encoding(); | ||
} | ||
|
||
@Override public final int messageMaxBytes() { | ||
return delegate().messageMaxBytes(); | ||
} | ||
|
||
@Override @Deprecated public final Call<Void> sendSpans(List<byte[]> encodedSpans) { | ||
return new SendSpans(encodedSpans); | ||
} | ||
|
||
@Override public final void send(List<byte[]> encodedSpans) throws IOException { | ||
delegate().send(encodedSpans); | ||
} | ||
|
||
@Override @Deprecated public final CheckResult check() { | ||
try { | ||
delegate().send(Collections.<byte[]>emptyList()); | ||
return CheckResult.OK; | ||
} catch (Throwable e) { | ||
Call.propagateIfFatal(e); | ||
return CheckResult.failed(e); | ||
} | ||
} | ||
|
||
@Override public final void close() { | ||
try { | ||
delegate().close(); | ||
} catch (IOException e) { | ||
throw Platform.get().uncheckedIOException(e); | ||
} | ||
} | ||
|
||
@Override public final String toString() { | ||
return delegate().toString(); | ||
} | ||
|
||
final class SendSpans extends Call.Base<Void> { | ||
private final List<byte[]> encodedSpans; | ||
|
||
SendSpans(List<byte[]> encodedSpans) { | ||
this.encodedSpans = encodedSpans; | ||
} | ||
|
||
@Override protected Void doExecute() throws IOException { | ||
send(encodedSpans); | ||
return null; | ||
} | ||
|
||
@Override protected void doEnqueue(Callback<Void> callback) { | ||
try { | ||
send(encodedSpans); | ||
callback.onSuccess(null); | ||
} catch (Throwable t) { | ||
Call.propagateIfFatal(t); | ||
callback.onError(t); | ||
} | ||
} | ||
|
||
@Override public Call<Void> clone() { | ||
return new SendSpans(encodedSpans); | ||
} | ||
} | ||
} |
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,67 @@ | ||
/* | ||
* Copyright 2016-2024 The OpenZipkin 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 zipkin2.reporter; | ||
|
||
import java.net.MalformedURLException; | ||
import java.net.URI; | ||
import java.util.List; | ||
import java.util.function.Consumer; | ||
import java.util.logging.Logger; | ||
import zipkin2.Span; | ||
import zipkin2.codec.SpanBytesDecoder; | ||
import zipkin2.reporter.HttpEndpointSupplier.Factory; | ||
|
||
class FakeHttpSender extends HttpSender<String, byte[]> { | ||
final String originalEndpoint; | ||
final Consumer<List<Span>> onSpans; | ||
|
||
FakeHttpSender(Logger logger, String endpoint, Consumer<List<Span>> onSpans) { | ||
this(logger, endpoint, ConstantHttpEndpointSupplier.FACTORY, onSpans); | ||
} | ||
|
||
FakeHttpSender(Logger logger, String endpoint, Factory endpointSupplierFactory, | ||
Consumer<List<Span>> onSpans) { | ||
super(logger, Encoding.JSON, endpointSupplierFactory, endpoint); | ||
this.originalEndpoint = endpoint; | ||
this.onSpans = onSpans; | ||
} | ||
|
||
FakeHttpSender withHttpEndpointSupplierFactory(Factory endpointSupplierFactory) { | ||
return new FakeHttpSender(logger, originalEndpoint, endpointSupplierFactory, onSpans); | ||
} | ||
|
||
/** close is typically called from a different thread */ | ||
volatile boolean closeCalled; | ||
|
||
@Override protected String newEndpoint(String endpoint) { | ||
try { | ||
return URI.create(endpoint).toURL().toString(); // validate | ||
} catch (MalformedURLException e) { | ||
throw new IllegalArgumentException(e.getMessage()); | ||
} | ||
} | ||
|
||
@Override protected byte[] newBody(List<byte[]> encodedSpans) { | ||
return encoding.encode(encodedSpans); | ||
} | ||
|
||
@Override protected void postSpans(String endpoint, byte[] body) { | ||
List<Span> decoded = SpanBytesDecoder.JSON_V2.decodeList(body); | ||
onSpans.accept(decoded); | ||
} | ||
|
||
@Override protected void doClose() { | ||
closeCalled = true; | ||
} | ||
} |
Oops, something went wrong.