github.com/zorawar87/trillian@v1.2.1/monitoring/opencensus/trace.go (about) 1 // Copyright 2018 Google LLC. All Rights Reserved. 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 package opencensus 16 17 import ( 18 "errors" 19 "net/http" 20 21 "contrib.go.opencensus.io/exporter/stackdriver" 22 "go.opencensus.io/plugin/ocgrpc" 23 "go.opencensus.io/plugin/ochttp" 24 "go.opencensus.io/stats/view" 25 "go.opencensus.io/trace" 26 "google.golang.org/grpc" 27 ) 28 29 // EnableRPCServerTracing turns on Stackdriver tracing. The returned 30 // options must be passed to the GRPC server. The supplied 31 // projectID can be nil for GCP but might need to be set for other 32 // cloud platforms. Refer to the appropriate documentation. The percentage 33 // of traced requests can be set between 0 and 100. Note that 0 does not 34 // disable tracing entirely but causes the default configuration to be used. 35 func EnableRPCServerTracing(projectID string, percent int) ([]grpc.ServerOption, error) { 36 if err := exporter(projectID); err != nil { 37 return nil, err 38 } 39 if err := applyConfig(percent); err != nil { 40 return nil, err 41 } 42 // Register the views to collect server request count. 43 if err := view.Register(ocgrpc.DefaultServerViews...); err != nil { 44 return nil, err 45 } 46 47 return []grpc.ServerOption{grpc.StatsHandler(&ocgrpc.ServerHandler{})}, nil 48 } 49 50 // EnableHTTPServerTracing turns on Stackdriver tracing for HTTP requests 51 // on the default ServeMux. The returned handler must be passed to the HTTP 52 // server. The supplied projectID can be nil for GCP but might need to be set 53 // for other cloud platforms. Refer to the appropriate documentation. 54 // The percentage of traced requests can be set between 0 and 100. Note that 0 55 // does not disable tracing entirely but causes the default configuration to be 56 // used. 57 func EnableHTTPServerTracing(projectID string, percent int) (http.Handler, error) { 58 if err := exporter(projectID); err != nil { 59 return nil, err 60 } 61 if err := applyConfig(percent); err != nil { 62 return nil, err 63 } 64 if err := view.Register(ochttp.DefaultServerViews...); err != nil { 65 return nil, err 66 } 67 return &ochttp.Handler{}, nil 68 } 69 70 func exporter(projectID string) error { 71 sde, err := stackdriver.NewExporter(stackdriver.Options{ProjectID: projectID}) 72 if err != nil { 73 return err 74 } 75 view.RegisterExporter(sde) 76 trace.RegisterExporter(sde) 77 78 return nil 79 } 80 81 func applyConfig(percent int) error { 82 switch { 83 case percent == 0: 84 // Use the default config, which traces relatively few requests. 85 case percent == 100: 86 trace.ApplyConfig(trace.Config{DefaultSampler: trace.AlwaysSample()}) 87 case percent > 100: 88 return errors.New("cannot trace more than 100 percent of requests") 89 default: 90 trace.ApplyConfig(trace.Config{DefaultSampler: trace.ProbabilitySampler(float64(percent) / 100.0)}) 91 } 92 return nil 93 }