|
| 1 | +/* |
| 2 | + * Copyright 2012-2022 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.actuate.autoconfigure.tracing.zipkin; |
| 18 | + |
| 19 | +import java.io.ByteArrayOutputStream; |
| 20 | +import java.io.IOException; |
| 21 | +import java.util.List; |
| 22 | +import java.util.zip.GZIPOutputStream; |
| 23 | + |
| 24 | +import zipkin2.Call; |
| 25 | +import zipkin2.Callback; |
| 26 | +import zipkin2.CheckResult; |
| 27 | +import zipkin2.codec.Encoding; |
| 28 | +import zipkin2.reporter.BytesMessageEncoder; |
| 29 | +import zipkin2.reporter.ClosedSenderException; |
| 30 | +import zipkin2.reporter.Sender; |
| 31 | + |
| 32 | +import org.springframework.http.HttpEntity; |
| 33 | +import org.springframework.http.HttpHeaders; |
| 34 | +import org.springframework.http.HttpMethod; |
| 35 | +import org.springframework.web.client.RestTemplate; |
| 36 | + |
| 37 | +/** |
| 38 | + * A Zipkin {@link Sender} which uses {@link RestTemplate} for HTTP communication. |
| 39 | + * Supports automatic compression with gzip. |
| 40 | + * |
| 41 | + * @author Moritz Halbritter |
| 42 | + */ |
| 43 | +class ZipkinRestTemplateSender extends Sender { |
| 44 | + |
| 45 | + private static final int MESSAGE_MAX_BYTES = 500_000; |
| 46 | + |
| 47 | + private final String endpoint; |
| 48 | + |
| 49 | + private final RestTemplate restTemplate; |
| 50 | + |
| 51 | + private volatile boolean closed; |
| 52 | + |
| 53 | + ZipkinRestTemplateSender(String endpoint, RestTemplate restTemplate) { |
| 54 | + this.endpoint = endpoint; |
| 55 | + this.restTemplate = restTemplate; |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public Encoding encoding() { |
| 60 | + return Encoding.JSON; |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public int messageMaxBytes() { |
| 65 | + return MESSAGE_MAX_BYTES; |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public int messageSizeInBytes(List<byte[]> encodedSpans) { |
| 70 | + return encoding().listSizeInBytes(encodedSpans); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public int messageSizeInBytes(int encodedSizeInBytes) { |
| 75 | + return encoding().listSizeInBytes(encodedSizeInBytes); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public Call<Void> sendSpans(List<byte[]> encodedSpans) { |
| 80 | + if (this.closed) { |
| 81 | + throw new ClosedSenderException(); |
| 82 | + } |
| 83 | + return new HttpCall(this.endpoint, BytesMessageEncoder.JSON.encode(encodedSpans), this.restTemplate); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public CheckResult check() { |
| 88 | + try { |
| 89 | + sendSpans(List.of()).execute(); |
| 90 | + return CheckResult.OK; |
| 91 | + } |
| 92 | + catch (IOException | RuntimeException e) { |
| 93 | + return CheckResult.failed(e); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public void close() throws IOException { |
| 99 | + this.closed = true; |
| 100 | + } |
| 101 | + |
| 102 | + private static class HttpCall extends Call.Base<Void> { |
| 103 | + |
| 104 | + /** |
| 105 | + * Only use gzip compression on data which is bigger than this in bytes. |
| 106 | + */ |
| 107 | + private static final int COMPRESSION_THRESHOLD = 1024; |
| 108 | + |
| 109 | + private final String endpoint; |
| 110 | + |
| 111 | + private final byte[] body; |
| 112 | + |
| 113 | + private final RestTemplate restTemplate; |
| 114 | + |
| 115 | + public HttpCall(String endpoint, byte[] body, RestTemplate restTemplate) { |
| 116 | + this.endpoint = endpoint; |
| 117 | + this.body = body; |
| 118 | + this.restTemplate = restTemplate; |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + protected Void doExecute() throws IOException { |
| 123 | + HttpHeaders headers = new HttpHeaders(); |
| 124 | + headers.set("b3", "0"); |
| 125 | + headers.set("Content-Type", "application/json"); |
| 126 | + byte[] body; |
| 127 | + if (needsCompression(this.body)) { |
| 128 | + headers.set("Content-Encoding", "gzip"); |
| 129 | + body = compress(this.body); |
| 130 | + } |
| 131 | + else { |
| 132 | + body = this.body; |
| 133 | + } |
| 134 | + HttpEntity<byte[]> request = new HttpEntity<>(body, headers); |
| 135 | + this.restTemplate.exchange(this.endpoint, HttpMethod.POST, request, Void.class); |
| 136 | + return null; |
| 137 | + } |
| 138 | + |
| 139 | + private boolean needsCompression(byte[] body) { |
| 140 | + return body.length > COMPRESSION_THRESHOLD; |
| 141 | + } |
| 142 | + |
| 143 | + @Override |
| 144 | + protected void doEnqueue(Callback<Void> callback) { |
| 145 | + try { |
| 146 | + doExecute(); |
| 147 | + callback.onSuccess(null); |
| 148 | + } |
| 149 | + catch (IOException | RuntimeException e) { |
| 150 | + callback.onError(e); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + @Override |
| 155 | + public Call<Void> clone() { |
| 156 | + return new HttpCall(this.endpoint, this.body, this.restTemplate); |
| 157 | + } |
| 158 | + |
| 159 | + private byte[] compress(byte[] input) throws IOException { |
| 160 | + ByteArrayOutputStream result = new ByteArrayOutputStream(); |
| 161 | + try (GZIPOutputStream gzip = new GZIPOutputStream(result)) { |
| 162 | + gzip.write(input); |
| 163 | + } |
| 164 | + return result.toByteArray(); |
| 165 | + } |
| 166 | + |
| 167 | + } |
| 168 | + |
| 169 | +} |
0 commit comments