|
| 1 | +/* |
| 2 | + * Copyright 2019, OpenCensus 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 | + * http://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 io.opencensus.exporter.trace.util; |
| 18 | + |
| 19 | +import com.google.common.util.concurrent.SimpleTimeLimiter; |
| 20 | +import com.google.common.util.concurrent.TimeLimiter; |
| 21 | +import com.google.errorprone.annotations.MustBeClosed; |
| 22 | +import io.opencensus.common.Duration; |
| 23 | +import io.opencensus.common.Scope; |
| 24 | +import io.opencensus.trace.Sampler; |
| 25 | +import io.opencensus.trace.Span; |
| 26 | +import io.opencensus.trace.Status; |
| 27 | +import io.opencensus.trace.TraceOptions; |
| 28 | +import io.opencensus.trace.Tracer; |
| 29 | +import io.opencensus.trace.Tracing; |
| 30 | +import io.opencensus.trace.export.SpanData; |
| 31 | +import io.opencensus.trace.export.SpanExporter; |
| 32 | +import io.opencensus.trace.export.SpanExporter.Handler; |
| 33 | +import io.opencensus.trace.samplers.Samplers; |
| 34 | +import java.util.Collection; |
| 35 | +import java.util.concurrent.Callable; |
| 36 | +import java.util.concurrent.Executors; |
| 37 | +import java.util.concurrent.TimeUnit; |
| 38 | +import java.util.concurrent.TimeoutException; |
| 39 | +import java.util.logging.Level; |
| 40 | +import java.util.logging.Logger; |
| 41 | + |
| 42 | +/** |
| 43 | + * An abstract class that allows different tracing services to export recorded data for sampled |
| 44 | + * spans in their own format within a given time frame. If export does not complete within the time |
| 45 | + * frame, spans will be dropped and no retries will be performed. |
| 46 | + * |
| 47 | + * <p>Only extend this class if the client APIs don't support timeout natively. If there is a |
| 48 | + * timeout option in the client APIs (for example Stackdriver Trace V2 API allows you to set |
| 49 | + * timeout), use that instead. |
| 50 | + * |
| 51 | + * <p>To export data this MUST be register to to the ExportComponent using {@link |
| 52 | + * SpanExporter#registerHandler(String, Handler)}. |
| 53 | + * |
| 54 | + * @since 0.22 |
| 55 | + */ |
| 56 | +public abstract class TimeLimitedHandler extends SpanExporter.Handler { |
| 57 | + |
| 58 | + private static final Logger logger = Logger.getLogger(TimeLimitedHandler.class.getName()); |
| 59 | + private static final Tracer tracer = Tracing.getTracer(); |
| 60 | + private static final Sampler lowProbabilitySampler = Samplers.probabilitySampler(0.0001); |
| 61 | + |
| 62 | + private final Duration deadline; |
| 63 | + private final String exportSpanName; |
| 64 | + |
| 65 | + protected TimeLimitedHandler(Duration deadline, String exportSpanName) { |
| 66 | + this.deadline = deadline; |
| 67 | + this.exportSpanName = exportSpanName; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Exports a list of sampled (see {@link TraceOptions#isSampled()}) {@link Span}s using the |
| 72 | + * immutable representation {@link SpanData}, within the given {@code deadline} of this {@link |
| 73 | + * TimeLimitedHandler}. |
| 74 | + * |
| 75 | + * @param spanDataList a list of {@code SpanData} objects to be exported. |
| 76 | + * @since 0.22 |
| 77 | + */ |
| 78 | + public abstract void timeLimitedExport(Collection<SpanData> spanDataList); |
| 79 | + |
| 80 | + @Override |
| 81 | + public void export(final Collection<SpanData> spanDataList) { |
| 82 | + final Scope exportScope = newExportScope(); |
| 83 | + try { |
| 84 | + TimeLimiter timeLimiter = SimpleTimeLimiter.create(Executors.newSingleThreadExecutor()); |
| 85 | + timeLimiter.callWithTimeout( |
| 86 | + new Callable<Void>() { |
| 87 | + @Override |
| 88 | + public Void call() throws Exception { |
| 89 | + timeLimitedExport(spanDataList); |
| 90 | + return null; |
| 91 | + } |
| 92 | + }, |
| 93 | + deadline.toMillis(), |
| 94 | + TimeUnit.MILLISECONDS); |
| 95 | + } catch (TimeoutException e) { |
| 96 | + handleException(e, "Timeout when exporting traces: " + e); |
| 97 | + } catch (InterruptedException e) { |
| 98 | + handleException(e, "Interrupted when exporting traces: " + e); |
| 99 | + } catch (Exception e) { |
| 100 | + handleException(e, "Failed to export traces: " + e); |
| 101 | + } finally { |
| 102 | + exportScope.close(); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + @MustBeClosed |
| 107 | + private Scope newExportScope() { |
| 108 | + return tracer.spanBuilder(exportSpanName).setSampler(lowProbabilitySampler).startScopedSpan(); |
| 109 | + } |
| 110 | + |
| 111 | + private static void handleException(Exception e, String logMessage) { |
| 112 | + Status status = e instanceof TimeoutException ? Status.DEADLINE_EXCEEDED : Status.UNKNOWN; |
| 113 | + tracer |
| 114 | + .getCurrentSpan() |
| 115 | + .setStatus( |
| 116 | + status.withDescription( |
| 117 | + e.getMessage() == null ? e.getClass().getSimpleName() : e.getMessage())); |
| 118 | + logger.log(Level.WARNING, logMessage); |
| 119 | + } |
| 120 | +} |
0 commit comments