Copyright 2017, OpenCensus 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 http://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 trace

import (
	
	

	
)

const (
	maxBucketSize     = 100000
	defaultBucketSize = 10
)

var (
	ssmu       sync.RWMutex // protects spanStores
	spanStores = make(map[string]*spanStore)
)
This exists purely to avoid exposing internal methods used by z-Pages externally.
type internalOnly struct{}

TODO(#412): remove
ReportActiveSpans returns the active spans for the given name.
func ( internalOnly) ( string) []*SpanData {
	 := spanStoreForName()
	if  == nil {
		return nil
	}
	var  []*SpanData
	.mu.Lock()
	defer .mu.Unlock()
	for  := range .active {
		 = append(, .makeSpanData())
	}
	return 
}
ReportSpansByError returns a sample of error spans. If code is nonzero, only spans with that status code are returned.
func ( internalOnly) ( string,  int32) []*SpanData {
	 := spanStoreForName()
	if  == nil {
		return nil
	}
	var  []*SpanData
	.mu.Lock()
	defer .mu.Unlock()
	if  != 0 {
		if ,  := .errors[];  {
			for ,  := range .buffer {
				if  == nil {
					break
				}
				 = append(, )
			}
		}
	} else {
		for ,  := range .errors {
			for ,  := range .buffer {
				if  == nil {
					break
				}
				 = append(, )
			}
		}
	}
	return 
}
ConfigureBucketSizes sets the number of spans to keep per latency and error bucket for different span names.
func ( internalOnly) ( []internal.BucketConfiguration) {
	for ,  := range  {
		 := .MaxRequestsSucceeded
		if  < 0 {
			 = 0
		}
		if  > maxBucketSize {
			 = maxBucketSize
		}
		 := .MaxRequestsErrors
		if  < 0 {
			 = 0
		}
		if  > maxBucketSize {
			 = maxBucketSize
		}
		spanStoreSetSize(.Name, , )
	}
}
ReportSpansPerMethod returns a summary of what spans are being stored for each span name.
func ( internalOnly) () map[string]internal.PerMethodSummary {
	 := make(map[string]internal.PerMethodSummary)
	ssmu.RLock()
	defer ssmu.RUnlock()
	for ,  := range spanStores {
		.mu.Lock()
		 := internal.PerMethodSummary{
			Active: len(.active),
		}
		for ,  := range .errors {
			.ErrorBuckets = append(.ErrorBuckets, internal.ErrorBucketSummary{
				ErrorCode: ,
				Size:      .size(),
			})
		}
		for ,  := range .latency {
			,  := latencyBucketBounds()
			.LatencyBuckets = append(.LatencyBuckets, internal.LatencyBucketSummary{
				MinLatency: ,
				MaxLatency: ,
				Size:       .size(),
			})
		}
		.mu.Unlock()
		[] = 
	}
	return 
}
ReportSpansByLatency returns a sample of successful spans. minLatency is the minimum latency of spans to be returned. maxLatency, if nonzero, is the maximum latency of spans to be returned.
func ( internalOnly) ( string, ,  time.Duration) []*SpanData {
	 := spanStoreForName()
	if  == nil {
		return nil
	}
	var  []*SpanData
	.mu.Lock()
	defer .mu.Unlock()
	for ,  := range .latency {
		,  := latencyBucketBounds()
		if +1 != len(.latency) &&  <=  {
			continue
		}
		if  != 0 &&  <  {
			continue
		}
		for ,  := range .buffer {
			if  == nil {
				break
			}
			if  != 0 ||  != 0 {
				 := .EndTime.Sub(.StartTime)
				if  <  {
					continue
				}
				if  != 0 &&  >  {
					continue
				}
			}
			 = append(, )
		}
	}
	return 
}
spanStore keeps track of spans stored for a particular span name. It contains all active spans; a sample of spans for failed requests, categorized by error code; and a sample of spans for successful requests, bucketed by latency.
type spanStore struct {
	mu                     sync.Mutex // protects everything below.
	active                 map[*Span]struct{}
	errors                 map[int32]*bucket
	latency                []bucket
	maxSpansPerErrorBucket int
}
newSpanStore creates a span store.
func ( string,  int,  int) *spanStore {
	 := &spanStore{
		active:                 make(map[*Span]struct{}),
		latency:                make([]bucket, len(defaultLatencies)+1),
		maxSpansPerErrorBucket: ,
	}
	for  := range .latency {
		.latency[] = makeBucket()
	}
	return 
}
spanStoreForName returns the spanStore for the given name. It returns nil if it doesn't exist.
func ( string) *spanStore {
	var  *spanStore
	ssmu.RLock()
	, _ = spanStores[]
	ssmu.RUnlock()
	return 
}
spanStoreForNameCreateIfNew returns the spanStore for the given name. It creates it if it didn't exist.
func ( string) *spanStore {
	ssmu.RLock()
	,  := spanStores[]
	ssmu.RUnlock()
	if  {
		return 
	}
	ssmu.Lock()
	defer ssmu.Unlock()
	,  = spanStores[]
	if  {
		return 
	}
	 = newSpanStore(, defaultBucketSize, defaultBucketSize)
	spanStores[] = 
	return 
}
spanStoreSetSize resizes the spanStore for the given name. It creates it if it didn't exist.
func ( string,  int,  int) {
	ssmu.RLock()
	,  := spanStores[]
	ssmu.RUnlock()
	if  {
		.resize(, )
		return
	}
	ssmu.Lock()
	defer ssmu.Unlock()
	,  = spanStores[]
	if  {
		.resize(, )
		return
	}
	 = newSpanStore(, , )
	spanStores[] = 
}

func ( *spanStore) ( int,  int) {
	.mu.Lock()
	for  := range .latency {
		.latency[].resize()
	}
	for ,  := range .errors {
		.resize()
	}
	.maxSpansPerErrorBucket = 
	.mu.Unlock()
}
add adds a span to the active bucket of the spanStore.
func ( *spanStore) ( *Span) {
	.mu.Lock()
	.active[] = struct{}{}
	.mu.Unlock()
}
finished removes a span from the active set, and adds a corresponding SpanData to a latency or error bucket.
func ( *spanStore) ( *Span,  *SpanData) {
	 := .EndTime.Sub(.StartTime)
	if  < 0 {
		 = 0
	}
	 := .Status.Code

	.mu.Lock()
	delete(.active, )
	if  == 0 {
		.latency[latencyBucket()].add()
	} else {
		if .errors == nil {
			.errors = make(map[int32]*bucket)
		}
		if  := .errors[];  != nil {
			.add()
		} else {
			 := makeBucket(.maxSpansPerErrorBucket)
			.errors[] = &
			.add()
		}
	}
	.mu.Unlock()