-
Notifications
You must be signed in to change notification settings - Fork 292
/
StackdriverTraceAutoConfiguration.java
259 lines (225 loc) · 9.79 KB
/
StackdriverTraceAutoConfiguration.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
* Copyright 2017-2020 the original author or 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
*
* https://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 com.google.cloud.spring.autoconfigure.trace;
import brave.TracingCustomizer;
import brave.baggage.BaggagePropagation;
import brave.handler.SpanHandler;
import brave.propagation.B3Propagation;
import brave.propagation.Propagation;
import brave.propagation.stackdriver.StackdriverTracePropagation;
import com.google.api.gax.core.CredentialsProvider;
import com.google.api.gax.core.ExecutorProvider;
import com.google.api.gax.core.FixedExecutorProvider;
import com.google.cloud.spring.core.DefaultCredentialsProvider;
import com.google.cloud.spring.core.GcpProjectIdProvider;
import com.google.cloud.spring.core.UserAgentHeaderProvider;
import io.grpc.CallOptions;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.auth.MoreCallCredentials;
import java.io.IOException;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import javax.annotation.PreDestroy;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.actuate.autoconfigure.tracing.BraveAutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import zipkin2.CheckResult;
import zipkin2.Span;
import zipkin2.reporter.AsyncReporter;
import zipkin2.reporter.Reporter;
import zipkin2.reporter.ReporterMetrics;
import zipkin2.reporter.Sender;
import zipkin2.reporter.brave.ZipkinSpanHandler;
import zipkin2.reporter.stackdriver.StackdriverEncoder;
import zipkin2.reporter.stackdriver.StackdriverSender;
import zipkin2.reporter.stackdriver.StackdriverSender.Builder;
/** Config for Stackdriver Trace. */
@AutoConfiguration
@EnableConfigurationProperties({GcpTraceProperties.class})
@ConditionalOnProperty(
value = {"spring.cloud.gcp.trace.enabled"},
matchIfMissing = true)
@ConditionalOnClass(StackdriverSender.class)
@AutoConfigureBefore(BraveAutoConfiguration.class)
public class StackdriverTraceAutoConfiguration {
private static final Log LOGGER = LogFactory.getLog(StackdriverTraceAutoConfiguration.class);
/**
* Stackdriver reporter bean name. Name of the bean matters for supporting multiple tracing
* systems.
*/
public static final String REPORTER_BEAN_NAME = "stackdriverReporter";
/**
* Stackdriver sender bean name. Name of the bean matters for supporting multiple tracing systems.
*/
public static final String SENDER_BEAN_NAME = "stackdriverSender";
/**
* Stackdriver span handler bean name. Name of the bean matters for supporting multiple tracing
* systems.
*/
public static final String SPAN_HANDLER_BEAN_NAME = "stackdriverSpanHandler";
/**
* Stackdriver customizer bean name. Name of the bean matters for supporting multiple tracing
* systems.
*/
public static final String CUSTOMIZER_BEAN_NAME = "stackdriverTracingCustomizer";
private final GcpProjectIdProvider finalProjectIdProvider;
private final CredentialsProvider finalCredentialsProvider;
private final UserAgentHeaderProvider headerProvider = new UserAgentHeaderProvider(this.getClass());
private ThreadPoolTaskScheduler defaultTraceSenderThreadPool;
public StackdriverTraceAutoConfiguration(
GcpProjectIdProvider gcpProjectIdProvider,
CredentialsProvider credentialsProvider,
GcpTraceProperties gcpTraceProperties)
throws IOException {
this.finalProjectIdProvider =
(gcpTraceProperties.getProjectId() != null)
? gcpTraceProperties::getProjectId
: gcpProjectIdProvider;
this.finalCredentialsProvider =
gcpTraceProperties.getCredentials().hasKey()
? new DefaultCredentialsProvider(gcpTraceProperties)
: credentialsProvider;
}
@Bean(CUSTOMIZER_BEAN_NAME)
@ConditionalOnMissingBean(name = CUSTOMIZER_BEAN_NAME)
public TracingCustomizer stackdriverTracingCustomizer(
@Qualifier(SPAN_HANDLER_BEAN_NAME) SpanHandler spanHandler) {
return builder -> builder.supportsJoin(false).traceId128Bit(true).addSpanHandler(spanHandler);
}
@Bean(SPAN_HANDLER_BEAN_NAME)
@ConditionalOnMissingBean(name = SPAN_HANDLER_BEAN_NAME)
public SpanHandler stackdriverSpanHandler(
@Qualifier(REPORTER_BEAN_NAME) Reporter<Span> stackdriverReporter) {
return ZipkinSpanHandler.create(stackdriverReporter);
}
@Bean
@ConditionalOnMissingBean
ReporterMetrics reporterMetrics() {
return ReporterMetrics.NOOP_METRICS;
}
@Bean
@ConditionalOnMissingBean(name = "traceExecutorProvider")
public ExecutorProvider traceExecutorProvider(
GcpTraceProperties traceProperties,
@Qualifier("traceSenderThreadPool") Optional<ThreadPoolTaskScheduler> userProvidedScheduler) {
ThreadPoolTaskScheduler scheduler;
if (userProvidedScheduler.isPresent()) {
scheduler = userProvidedScheduler.get();
} else {
this.defaultTraceSenderThreadPool = new ThreadPoolTaskScheduler();
scheduler = this.defaultTraceSenderThreadPool;
scheduler.setPoolSize(traceProperties.getNumExecutorThreads());
scheduler.setThreadNamePrefix("gcp-trace-sender");
scheduler.setDaemon(true);
scheduler.initialize();
}
return FixedExecutorProvider.create(scheduler.getScheduledExecutor());
}
@Bean(destroyMethod = "shutdownNow")
@ConditionalOnMissingBean(name = "stackdriverSenderChannel")
public ManagedChannel stackdriverSenderChannel() {
return ManagedChannelBuilder.forTarget("dns:///cloudtrace.googleapis.com")
.userAgent(this.headerProvider.getUserAgent())
.build();
}
@Bean(REPORTER_BEAN_NAME)
@ConditionalOnMissingBean(name = REPORTER_BEAN_NAME)
public Reporter<Span> stackdriverReporter(
ReporterMetrics reporterMetrics,
GcpTraceProperties trace,
@Qualifier(SENDER_BEAN_NAME) Sender sender) {
AsyncReporter<Span> asyncReporter =
AsyncReporter.builder(sender)
// historical constraint. Note: AsyncReporter supports memory bounds
.queuedMaxSpans(1000)
.messageTimeout(trace.getMessageTimeout(), TimeUnit.SECONDS)
.metrics(reporterMetrics)
.build(StackdriverEncoder.V2);
CheckResult checkResult = asyncReporter.check();
if (!checkResult.ok()) {
LOGGER.warn(
"Error when performing Stackdriver AsyncReporter health check.", checkResult.error());
}
return asyncReporter;
}
@Bean(SENDER_BEAN_NAME)
@ConditionalOnMissingBean(name = SENDER_BEAN_NAME)
public Sender stackdriverSender(
GcpTraceProperties traceProperties,
@Qualifier("traceExecutorProvider") ExecutorProvider executorProvider,
@Qualifier("stackdriverSenderChannel") ManagedChannel channel)
throws IOException {
CallOptions callOptions =
CallOptions.DEFAULT
.withCallCredentials(
MoreCallCredentials.from(this.finalCredentialsProvider.getCredentials()))
.withExecutor(executorProvider.getExecutor());
if (traceProperties.getAuthority() != null) {
callOptions = callOptions.withAuthority(traceProperties.getAuthority());
}
if (traceProperties.getCompression() != null) {
callOptions = callOptions.withCompression(traceProperties.getCompression());
}
if (traceProperties.getDeadlineMs() != null) {
callOptions =
callOptions.withDeadlineAfter(traceProperties.getDeadlineMs(), TimeUnit.MILLISECONDS);
}
if (traceProperties.getMaxInboundSize() != null) {
callOptions = callOptions.withMaxInboundMessageSize(traceProperties.getMaxInboundSize());
}
if (traceProperties.getMaxOutboundSize() != null) {
callOptions = callOptions.withMaxOutboundMessageSize(traceProperties.getMaxOutboundSize());
}
if (traceProperties.isWaitForReady() != null) {
if (Boolean.TRUE.equals(traceProperties.isWaitForReady())) {
callOptions = callOptions.withWaitForReady();
} else {
callOptions = callOptions.withoutWaitForReady();
}
}
final Builder builder =
StackdriverSender.newBuilder(channel)
.projectId(this.finalProjectIdProvider.getProjectId())
.callOptions(callOptions);
if (traceProperties.getServerResponseTimeoutMs() != null) {
builder.serverResponseTimeoutMs(traceProperties.getServerResponseTimeoutMs());
}
return builder.build();
}
@Bean
@ConditionalOnMissingBean
public BaggagePropagation.FactoryBuilder baggagePropagationFactoryBuilder() {
Propagation.Factory primary =
B3Propagation.newFactoryBuilder().injectFormat(B3Propagation.Format.MULTI).build();
return BaggagePropagation.newFactoryBuilder(StackdriverTracePropagation.newFactory(primary));
}
@PreDestroy
public void closeScheduler() {
if (this.defaultTraceSenderThreadPool != null) {
this.defaultTraceSenderThreadPool.shutdown();
}
}
}