1 /* 2 * Copyright 2018, 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.implcore.trace.export; 18 19 import io.opencensus.implcore.trace.RecordEventsSpanImpl; 20 import io.opencensus.trace.export.SampledSpanStore; 21 import io.opencensus.trace.export.SpanData; 22 import java.util.Collection; 23 import java.util.Collections; 24 import java.util.Set; 25 26 /** Abstract implementation of the {@link SampledSpanStore}. */ 27 public abstract class SampledSpanStoreImpl extends SampledSpanStore { 28 private static final SampledSpanStoreImpl NOOP_SAMPLED_SPAN_STORE_IMPL = 29 new NoopSampledSpanStoreImpl(); 30 31 /** Returns the new no-op implmentation of {@link SampledSpanStoreImpl}. */ getNoopSampledSpanStoreImpl()32 public static SampledSpanStoreImpl getNoopSampledSpanStoreImpl() { 33 return NOOP_SAMPLED_SPAN_STORE_IMPL; 34 } 35 36 /** 37 * Considers to save the given spans to the stored samples. This must be called at the end of each 38 * Span with the option RECORD_EVENTS. 39 * 40 * @param span the span to be consider for storing into the store buckets. 41 */ considerForSampling(RecordEventsSpanImpl span)42 public abstract void considerForSampling(RecordEventsSpanImpl span); 43 44 /** 45 * Returns {@code true} if the SampledSpanStore is enabled. 46 * 47 * @return {@code true} if the SampledSpanStore is enabled. 48 */ getEnabled()49 public abstract boolean getEnabled(); 50 shutdown()51 protected void shutdown() {} 52 53 private static final class NoopSampledSpanStoreImpl extends SampledSpanStoreImpl { 54 private static final Summary EMPTY_SUMMARY = 55 Summary.create(Collections.<String, PerSpanNameSummary>emptyMap()); 56 private static final Set<String> EMPTY_REGISTERED_SPAN_NAMES = Collections.<String>emptySet(); 57 private static final Collection<SpanData> EMPTY_SPANDATA = Collections.<SpanData>emptySet(); 58 59 @Override getSummary()60 public Summary getSummary() { 61 return EMPTY_SUMMARY; 62 } 63 64 @Override considerForSampling(RecordEventsSpanImpl span)65 public void considerForSampling(RecordEventsSpanImpl span) {} 66 67 @Override getEnabled()68 public boolean getEnabled() { 69 return false; 70 } 71 72 @Override 73 @SuppressWarnings("deprecation") registerSpanNamesForCollection(Collection<String> spanNames)74 public void registerSpanNamesForCollection(Collection<String> spanNames) {} 75 76 @Override 77 @SuppressWarnings("deprecation") unregisterSpanNamesForCollection(Collection<String> spanNames)78 public void unregisterSpanNamesForCollection(Collection<String> spanNames) {} 79 80 @Override getRegisteredSpanNamesForCollection()81 public Set<String> getRegisteredSpanNamesForCollection() { 82 return EMPTY_REGISTERED_SPAN_NAMES; 83 } 84 85 @Override getErrorSampledSpans(ErrorFilter filter)86 public Collection<SpanData> getErrorSampledSpans(ErrorFilter filter) { 87 return EMPTY_SPANDATA; 88 } 89 90 @Override getLatencySampledSpans(LatencyFilter filter)91 public Collection<SpanData> getLatencySampledSpans(LatencyFilter filter) { 92 return EMPTY_SPANDATA; 93 } 94 } 95 } 96