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 zpages

import (
	
	
	
	
	
	
	
	
	

	
	
)

const bytesPerKb = 1024

var (
	programStartTime = time.Now()
	mu               sync.Mutex // protects snaps
	snaps            = make(map[methodKey]*statSnapshot)
viewType lists the views we are interested in for RPC stats. A view's map value indicates whether that view contains data for received RPCs.
WriteHTMLRpczPage writes an HTML document to w containing per-method RPC stats.
func ( io.Writer) {
	if  := headerTemplate.Execute(, headerData{Title: "RPC Stats"});  != nil {
		log.Printf("zpages: executing template: %v", )
	}
	WriteHTMLRpczSummary()
	if  := footerTemplate.Execute(, nil);  != nil {
		log.Printf("zpages: executing template: %v", )
	}
}
WriteHTMLRpczSummary writes HTML to w containing per-method RPC stats. It includes neither a header nor footer, so you can embed this data in other pages.
func ( io.Writer) {
	mu.Lock()
	if  := statsTemplate.Execute(, getStatsPage());  != nil {
		log.Printf("zpages: executing template: %v", )
	}
	mu.Unlock()
}
WriteTextRpczPage writes formatted text to w containing per-method RPC stats.
func ( io.Writer) {
	mu.Lock()
	defer mu.Unlock()
	 := getStatsPage()

	for ,  := range .StatGroups {
		switch  {
		case 0:
			fmt.Fprint(, "Sent:\n")
		case 1:
			fmt.Fprint(, "\nReceived:\n")
		}
		 := tabwriter.NewWriter(, 6, 8, 1, ' ', 0)
		fmt.Fprint(, "Method\tCount\t\t\tAvgLat\t\t\tMaxLat\t\t\tRate\t\t\tIn (MiB/s)\t\t\tOut (MiB/s)\t\t\tErrors\t\t\n")
		fmt.Fprint(, "\tMin\tHr\tTot\tMin\tHr\tTot\tMin\tHr\tTot\tMin\tHr\tTot\tMin\tHr\tTot\tMin\tHr\tTot\tMin\tHr\tTot\n")
		for ,  := range .Snapshots {
			fmt.Fprintf(, "%s\t%d\t%d\t%d\t%v\t%v\t%v\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%d\t%d\t%d\n",
				.Method,
				.CountMinute,
				.CountHour,
				.CountTotal,
				.AvgLatencyMinute,
				.AvgLatencyHour,
				.AvgLatencyTotal,
				.RPCRateMinute,
				.RPCRateHour,
				.RPCRateTotal,
				.InputRateMinute/bytesPerKb,
				.InputRateHour/bytesPerKb,
				.InputRateTotal/bytesPerKb,
				.OutputRateMinute/bytesPerKb,
				.OutputRateHour/bytesPerKb,
				.OutputRateTotal/bytesPerKb,
				.ErrorsMinute,
				.ErrorsHour,
				.ErrorsTotal)
		}
		.Flush()
	}
}
headerData contains data for the header template.
type headerData struct {
	Title string
}
statsPage aggregates stats on the page for 'sent' and 'received' categories
type statsPage struct {
	StatGroups []*statGroup
}
statGroup aggregates snapshots for a directional category
type statGroup struct {
	Direction string
	Snapshots []*statSnapshot
}

func ( *statGroup) () int {
	return len(.Snapshots)
}

func ( *statGroup) (,  int) {
	.Snapshots[], .Snapshots[] = .Snapshots[], .Snapshots[]
}

func ( *statGroup) (,  int) bool {
	return .Snapshots[].Method < .Snapshots[].Method
}
statSnapshot holds the data items that are presented in a single row of RPC stat information.
TODO: compute hour/minute values from cumulative
	Method           string
	Received         bool
	CountMinute      uint64
	CountHour        uint64
	CountTotal       uint64
	AvgLatencyMinute time.Duration
	AvgLatencyHour   time.Duration
	AvgLatencyTotal  time.Duration
	RPCRateMinute    float64
	RPCRateHour      float64
	RPCRateTotal     float64
	InputRateMinute  float64
	InputRateHour    float64
	InputRateTotal   float64
	OutputRateMinute float64
	OutputRateHour   float64
	OutputRateTotal  float64
	ErrorsMinute     uint64
	ErrorsHour       uint64
	ErrorsTotal      uint64
}

type methodKey struct {
	method   string
	received bool
}

type snapExporter struct{}

func ( snapExporter) ( *view.Data) {
	,  := viewType[.View]
	if ! {
		return
	}
	if len(.Rows) == 0 {
		return
	}
	 := float64(time.Since(programStartTime)) / float64(time.Second)

	 := func(,  float64) float64 {
		 := 
		if  > 0 &&  >  {
			 = 
		}
		return  / 
	}

	 := func( float64) time.Duration {
		if math.IsInf(, 0) || math.IsNaN() {
			return 0
		}
		return time.Duration(float64(time.Millisecond) * )
	}

	 := make(map[string]struct{})

	mu.Lock()
	defer mu.Unlock()
	for ,  := range .Rows {
		var  string
		for ,  := range .Tags {
			if .Key == ocgrpc.KeyClientMethod || .Key == ocgrpc.KeyServerMethod {
				 = .Value
				break
			}
		}

		 := methodKey{method: , received: }
		 := snaps[]
		if  == nil {
			 = &statSnapshot{Method: , Received: }
			snaps[] = 
		}

		var (
			   float64
			 float64
		)
		switch v := .Data.(type) {
		case *view.CountData:
			 = float64(.Value)
			 = float64(.Value)
		case *view.DistributionData:
			 = .Sum()
			 = float64(.Count)
		case *view.SumData:
			 = .Value
			 = .Value
		}
Update field of s corresponding to the view.
		switch .View {
		case ocgrpc.ClientCompletedRPCsView:
			if ,  := []; ! {
				[] = struct{}{}
				.ErrorsTotal = 0
			}
			for ,  := range .Tags {
				if .Key == ocgrpc.KeyClientStatus && .Value != "OK" {
					.ErrorsTotal += uint64()
				}
			}

		case ocgrpc.ClientRoundtripLatencyView:
			.AvgLatencyTotal = ( / )

		case ocgrpc.ClientSentBytesPerRPCView:
			.OutputRateTotal = (0, )

		case ocgrpc.ClientReceivedBytesPerRPCView:
			.InputRateTotal = (0, )

		case ocgrpc.ClientSentMessagesPerRPCView:
			.CountTotal = uint64()
			.RPCRateTotal = (0, )

currently unused

		case ocgrpc.ServerCompletedRPCsView:
			if ,  := []; ! {
				[] = struct{}{}
				.ErrorsTotal = 0
			}
			for ,  := range .Tags {
				if .Key == ocgrpc.KeyServerStatus && .Value != "OK" {
					.ErrorsTotal += uint64()
				}
			}

		case ocgrpc.ServerLatencyView:
			.AvgLatencyTotal = ( / )

		case ocgrpc.ServerSentBytesPerRPCView:
			.OutputRateTotal = (0, )

		case ocgrpc.ServerReceivedMessagesPerRPCView:
			.CountTotal = uint64()
			.RPCRateTotal = (0, )

currently unused
		}
	}
}

func () *statsPage {
	 := statGroup{Direction: "Sent"}
	 := statGroup{Direction: "Received"}
	for ,  := range snaps {
		if .received {
			.Snapshots = append(.Snapshots, )
		} else {
			.Snapshots = append(.Snapshots, )
		}
	}
	sort.Sort(&)
	sort.Sort(&)

	return &statsPage{
		StatGroups: []*statGroup{&, &},
	}