This repository has been archived by the owner on Jul 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 232
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make GenericZipkinSender to share logic between Zipkin 1 and 2 senders
Signed-off-by: Ben Keith <bkeith@signalfx.com>
- Loading branch information
Ben Keith
committed
May 2, 2018
1 parent
641547c
commit 60b8584
Showing
3 changed files
with
161 additions
and
129 deletions.
There are no files selected for viewing
118 changes: 118 additions & 0 deletions
118
jaeger-zipkin/src/main/java/io/jaegertracing/senders/zipkin/GenericZipkinSender.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,118 @@ | ||
/* | ||
* Copyright (c) 2016, Uber Technologies, Inc | ||
* | ||
* 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 io.jaegertracing.senders.zipkin; | ||
|
||
import io.jaegertracing.exceptions.SenderException; | ||
import io.jaegertracing.senders.Sender; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import lombok.ToString; | ||
|
||
@ToString(exclude = "spanBuffer") | ||
public abstract class GenericZipkinSender implements Sender { | ||
|
||
final List<byte[]> spanBuffer; | ||
|
||
protected GenericZipkinSender() { | ||
this.spanBuffer = new ArrayList<byte[]>(); | ||
} | ||
|
||
public abstract byte[] convertAndEncodeSpan(io.jaegertracing.Span span); | ||
|
||
public abstract int messageSizeInBytes(List<byte[]> msg); | ||
|
||
public abstract int messageMaxBytes(); | ||
|
||
public abstract void sendSpans(List<byte[]> spanBuffer) throws IOException; | ||
|
||
public abstract void closeSender() throws IOException; | ||
|
||
public abstract String delegateToString(); | ||
|
||
/* | ||
* Adds spans to an internal queue that flushes them as udp packets later. | ||
* This function does not need to be synchronized because the reporter creates | ||
* a single thread that calls this append function | ||
*/ | ||
@Override | ||
public int append(io.jaegertracing.Span span) throws SenderException { | ||
byte[] next = this.convertAndEncodeSpan(span); | ||
int messageSizeOfNextSpan = this.messageSizeInBytes(Collections.singletonList(next)); | ||
// don't enqueue something larger than we can drain | ||
if (messageSizeOfNextSpan > this.messageMaxBytes()) { | ||
throw new SenderException( | ||
delegateToString() + " received a span that was too large", null, 1); | ||
} | ||
|
||
spanBuffer.add(next); // speculatively add to the buffer so we can size it | ||
int nextSizeInBytes = this.messageSizeInBytes(spanBuffer); | ||
// If we can fit queued spans and the next into one message... | ||
if (nextSizeInBytes <= this.messageMaxBytes()) { | ||
|
||
// If there's still room, don't flush yet. | ||
if (nextSizeInBytes < this.messageMaxBytes()) { | ||
return 0; | ||
} | ||
// If we have exactly met the max message size, flush | ||
return flush(); | ||
} | ||
|
||
// Otherwise, remove speculatively added span and flush until we have room for it. | ||
spanBuffer.remove(spanBuffer.size() - 1); | ||
int n; | ||
try { | ||
n = flush(); | ||
} catch (SenderException e) { | ||
// +1 for the span not submitted in the buffer above | ||
throw new SenderException(e.getMessage(), e.getCause(), e.getDroppedSpanCount() + 1); | ||
} | ||
|
||
// Now that there's room, add the span as the only element in the buffer | ||
spanBuffer.add(next); | ||
return n; | ||
} | ||
|
||
@Override | ||
public int flush() throws SenderException { | ||
if (spanBuffer.isEmpty()) { | ||
return 0; | ||
} | ||
|
||
int n = spanBuffer.size(); | ||
try { | ||
this.sendSpans(spanBuffer); | ||
} catch (RuntimeException e) { | ||
throw new SenderException("Failed to flush spans.", e, n); | ||
} catch (IOException e) { | ||
throw new SenderException("Failed to flush spans.", e, n); | ||
} finally { | ||
spanBuffer.clear(); | ||
} | ||
return n; | ||
} | ||
|
||
@Override | ||
public int close() throws SenderException { | ||
int n = flush(); | ||
try { | ||
this.closeSender(); | ||
} catch (IOException e) { | ||
throw new SenderException("Failed to close " + delegateToString(), e, n); | ||
} | ||
return n; | ||
} | ||
} |
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