1 // Copyright 2017, OpenCensus Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 16 // Package zpages implements a collection of HTML pages that display RPC stats 17 // and trace data, and also functions to write that same data in plain text to 18 // an io.Writer. 19 // 20 // Users can also embed the HTML for stats and traces in custom status pages. 21 // 22 // zpages are currrently work-in-process and cannot display minutely and 23 // hourly stats correctly. 24 // 25 // # Performance 26 // 27 // Installing the zpages has a performance overhead because additional traces 28 // and stats will be collected in-process. In most cases, we expect this 29 // overhead will not be significant but it depends on many factors, including 30 // how many spans your process creates and how richly annotated they are. 31 package zpages // import "go.opencensus.io/zpages" 32 33 import ( 34 "net/http" 35 "path" 36 "sync" 37 38 "go.opencensus.io/internal" 39 ) 40 41 // TODO(ramonza): Remove Handler to make initialization lazy. 42 43 // Handler is deprecated: Use Handle. 44 var Handler http.Handler 45 46 func init() { 47 mux := http.NewServeMux() 48 Handle(mux, "/") 49 Handler = mux 50 } 51 52 // Handle adds the z-pages to the given ServeMux rooted at pathPrefix. 53 func Handle(mux *http.ServeMux, pathPrefix string) { 54 enable() 55 if mux == nil { 56 mux = http.DefaultServeMux 57 } 58 mux.HandleFunc(path.Join(pathPrefix, "rpcz"), rpczHandler) 59 mux.HandleFunc(path.Join(pathPrefix, "tracez"), tracezHandler) 60 mux.Handle(path.Join(pathPrefix, "public/"), http.FileServer(fs)) 61 } 62 63 var enableOnce sync.Once 64 65 func enable() { 66 enableOnce.Do(func() { 67 internal.LocalSpanStoreEnabled = true 68 registerRPCViews() 69 }) 70 } 71