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.implcore.trace.internal.ConcurrentIntrusiveList; 21 import io.opencensus.trace.export.RunningSpanStore; 22 import io.opencensus.trace.export.SpanData; 23 import java.util.ArrayList; 24 import java.util.Collection; 25 import java.util.HashMap; 26 import java.util.List; 27 import java.util.Map; 28 import javax.annotation.concurrent.ThreadSafe; 29 30 /** In-process implementation of the {@link RunningSpanStore}. */ 31 @ThreadSafe 32 public final class InProcessRunningSpanStoreImpl extends RunningSpanStoreImpl { 33 private final ConcurrentIntrusiveList<RecordEventsSpanImpl> runningSpans; 34 35 public InProcessRunningSpanStoreImpl() { 36 runningSpans = new ConcurrentIntrusiveList<RecordEventsSpanImpl>(); 37 } 38 39 @Override 40 public void onStart(RecordEventsSpanImpl span) { 41 runningSpans.addElement(span); 42 } 43 44 @Override 45 public void onEnd(RecordEventsSpanImpl span) { 46 runningSpans.removeElement(span); 47 } 48 49 @Override 50 public Summary getSummary() { 51 Collection<RecordEventsSpanImpl> allRunningSpans = runningSpans.getAll(); 52 Map<String, Integer> numSpansPerName = new HashMap<String, Integer>(); 53 for (RecordEventsSpanImpl span : allRunningSpans) { 54 Integer prevValue = numSpansPerName.get(span.getName()); 55 numSpansPerName.put(span.getName(), prevValue != null ? prevValue + 1 : 1); 56 } 57 Map<String, PerSpanNameSummary> perSpanNameSummary = new HashMap<String, PerSpanNameSummary>(); 58 for (Map.Entry<String, Integer> it : numSpansPerName.entrySet()) { 59 perSpanNameSummary.put(it.getKey(), PerSpanNameSummary.create(it.getValue())); 60 } 61 Summary summary = Summary.create(perSpanNameSummary); 62 return summary; 63 } 64 65 @Override 66 public Collection<SpanData> getRunningSpans(Filter filter) { 67 Collection<RecordEventsSpanImpl> allRunningSpans = runningSpans.getAll(); 68 int maxSpansToReturn = 69 filter.getMaxSpansToReturn() == 0 ? allRunningSpans.size() : filter.getMaxSpansToReturn(); 70 List<SpanData> ret = new ArrayList<SpanData>(maxSpansToReturn); 71 for (RecordEventsSpanImpl span : allRunningSpans) { 72 if (ret.size() == maxSpansToReturn) { 73 break; 74 } 75 if (span.getName().equals(filter.getSpanName())) { 76 ret.add(span.toSpanData()); 77 } 78 } 79 return ret; 80 } 81 } 82