7
7
8
8
import io .opentelemetry .exporter .internal .compression .Compressor ;
9
9
import io .opentelemetry .exporter .internal .http .HttpSender ;
10
+ import io .opentelemetry .exporter .internal .marshal .Marshaler ;
10
11
import io .opentelemetry .sdk .common .CompletableResultCode ;
11
12
import io .opentelemetry .sdk .common .export .RetryPolicy ;
12
13
import java .io .ByteArrayOutputStream ;
13
14
import java .io .IOException ;
14
15
import java .io .OutputStream ;
16
+ import java .io .UncheckedIOException ;
15
17
import java .net .URI ;
16
18
import java .net .URISyntaxException ;
17
19
import java .net .http .HttpClient ;
@@ -53,6 +55,7 @@ public final class JdkHttpSender implements HttpSender {
53
55
private final HttpClient client ;
54
56
private final URI uri ;
55
57
@ Nullable private final Compressor compressor ;
58
+ private final boolean exportAsJson ;
56
59
private final String contentType ;
57
60
private final long timeoutNanos ;
58
61
private final Supplier <Map <String , List <String >>> headerSupplier ;
@@ -63,6 +66,7 @@ public final class JdkHttpSender implements HttpSender {
63
66
HttpClient client ,
64
67
String endpoint ,
65
68
@ Nullable Compressor compressor ,
69
+ boolean exportAsJson ,
66
70
String contentType ,
67
71
long timeoutNanos ,
68
72
Supplier <Map <String , List <String >>> headerSupplier ,
@@ -74,6 +78,7 @@ public final class JdkHttpSender implements HttpSender {
74
78
throw new IllegalArgumentException (e );
75
79
}
76
80
this .compressor = compressor ;
81
+ this .exportAsJson = exportAsJson ;
77
82
this .contentType = contentType ;
78
83
this .timeoutNanos = timeoutNanos ;
79
84
this .headerSupplier = headerSupplier ;
@@ -83,6 +88,7 @@ public final class JdkHttpSender implements HttpSender {
83
88
JdkHttpSender (
84
89
String endpoint ,
85
90
@ Nullable Compressor compressor ,
91
+ boolean exportAsJson ,
86
92
String contentType ,
87
93
long timeoutNanos ,
88
94
long connectTimeoutNanos ,
@@ -93,6 +99,7 @@ public final class JdkHttpSender implements HttpSender {
93
99
configureClient (sslContext , connectTimeoutNanos ),
94
100
endpoint ,
95
101
compressor ,
102
+ exportAsJson ,
96
103
contentType ,
97
104
timeoutNanos ,
98
105
headerSupplier ,
@@ -111,7 +118,7 @@ private static HttpClient configureClient(
111
118
112
119
@ Override
113
120
public void send (
114
- Consumer < OutputStream > marshaler ,
121
+ Marshaler marshaler ,
115
122
int contentLength ,
116
123
Consumer <Response > onResponse ,
117
124
Consumer <Throwable > onError ) {
@@ -121,7 +128,7 @@ public void send(
121
128
try {
122
129
return sendInternal (marshaler );
123
130
} catch (IOException e ) {
124
- throw new IllegalStateException (e );
131
+ throw new UncheckedIOException (e );
125
132
}
126
133
},
127
134
executorService )
@@ -136,7 +143,7 @@ public void send(
136
143
}
137
144
138
145
// Visible for testing
139
- HttpResponse <byte []> sendInternal (Consumer < OutputStream > marshaler ) throws IOException {
146
+ HttpResponse <byte []> sendInternal (Marshaler marshaler ) throws IOException {
140
147
long startTimeNanos = System .nanoTime ();
141
148
HttpRequest .Builder requestBuilder =
142
149
HttpRequest .newBuilder ().uri (uri ).timeout (Duration .ofNanos (timeoutNanos ));
@@ -151,12 +158,12 @@ HttpResponse<byte[]> sendInternal(Consumer<OutputStream> marshaler) throws IOExc
151
158
if (compressor != null ) {
152
159
requestBuilder .header ("Content-Encoding" , compressor .getEncoding ());
153
160
try (OutputStream compressed = compressor .compress (os )) {
154
- marshaler . accept ( compressed );
161
+ write ( marshaler , compressed );
155
162
} catch (IOException e ) {
156
163
throw new IllegalStateException (e );
157
164
}
158
165
} else {
159
- marshaler . accept ( os );
166
+ write ( marshaler , os );
160
167
}
161
168
162
169
ByteBufferPool byteBufferPool = threadLocalByteBufPool .get ();
@@ -211,6 +218,14 @@ HttpResponse<byte[]> sendInternal(Consumer<OutputStream> marshaler) throws IOExc
211
218
throw exception ;
212
219
}
213
220
221
+ private void write (Marshaler marshaler , OutputStream os ) throws IOException {
222
+ if (exportAsJson ) {
223
+ marshaler .writeJsonTo (os );
224
+ } else {
225
+ marshaler .writeBinaryTo (os );
226
+ }
227
+ }
228
+
214
229
private HttpResponse <byte []> sendRequest (
215
230
HttpRequest .Builder requestBuilder , ByteBufferPool byteBufferPool ) throws IOException {
216
231
try {
0 commit comments