Skip to content

Commit

Permalink
Initialize opencensus for GKE (#176)
Browse files Browse the repository at this point in the history
* Initialize opencensus for GKE

* update readme

* Generalize initialization of sd exporter

* fix sdExporterOptions

* do not initilize SD exporters and error reporting unless we are in the cloud or set proper environment variables

* Update doc

* address comments

* remove redundant if
  • Loading branch information
gaplyk authored and jprobinson committed Jan 29, 2019
1 parent b3a5760 commit 27bac81
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 20 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ The `server/kit` package embodies Gizmo's goals to combine with go-kit.
* In this package you'll find:
* A more opinionated server with fewer choices.
* go-kit used for serving HTTP/JSON & gRPC used for serving HTTP2/RPC
* Monitoring and metrics are automatically registered if running within App Engine.
* Monitoring, traces and metrics are automatically registered if running within App Engine, Kubernetes Engine, Compute Engine or AWS EC2 Instances.
* to change the name and version for Error reporting and Traces use `SERVICE_NAME` and `SERVICE_VERSION` environment variables.
* Logs go to stdout locally or directly to Stackdriver when in GCP.
* Using Go's 1.8 graceful HTTP shutdown.
* Services using this package are expected to deploy to GCP.


#### [`auth`](https://godoc.org/github.com/NYTimes/gizmo/auth)

The `auth` package provides primitives for verifying inbound authentication tokens:
Expand Down
2 changes: 1 addition & 1 deletion server/kit/gae_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (

// project, service, version
func getGAEInfo() (string, string, string) {
return os.Getenv("GOOGLE_CLOUD_PROJECT"),
return googleProjectID(),
os.Getenv("GAE_SERVICE"),
os.Getenv("GAE_VERSION")
}
Expand Down
31 changes: 20 additions & 11 deletions server/kit/kitserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net"
"net/http"
"net/http/pprof"
"os"
"strings"

"cloud.google.com/go/errorreporting"
Expand Down Expand Up @@ -87,12 +88,28 @@ func NewServer(svc Service) *Server {
errs *errorreporting.Client
propr propagation.HTTPFormat
)
// if in App Engine, initiate an error reporter and the stackdriver exporters

projectID := googleProjectID()
var svcName, svcVersion string
if isGAE() {
projectID, serviceID, svcVersion := getGAEInfo()
_, svcName, svcVersion = getGAEInfo()
} else if n, v := os.Getenv("SERVICE_NAME"), os.Getenv("SERVICE_VERSION"); n != "" {
svcName, svcVersion = n, v
}

if opt := sdExporterOptions(projectID, svcName, svcVersion, lg); opt != nil {
err = initSDExporter(*opt)
if err != nil {
lg.Log("error", err,
"message", "unable to initiate error tracing exporter")
}

propr = &sdpropagation.HTTPFormat{}

errs, err = errorreporting.NewClient(ctx, projectID, errorreporting.Config{
ServiceName: serviceID,
ServiceName: svcName,
ServiceVersion: svcVersion,

OnError: func(err error) {
lg.Log("error", err,
"message", "error reporting client encountered an error")
Expand All @@ -102,14 +119,6 @@ func NewServer(svc Service) *Server {
lg.Log("error", err,
"message", "unable to initiate error reporting client")
}

err = initSDExporter(projectID, serviceID, svcVersion, lg)
if err != nil {
lg.Log("error", err,
"message", "unable to initiate error tracing exporter")
}

propr = &sdpropagation.HTTPFormat{}
}

s := &Server{
Expand Down
37 changes: 30 additions & 7 deletions server/kit/stackdriver.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,34 @@
package kit

import (
"os"

"contrib.go.opencensus.io/exporter/stackdriver"
"contrib.go.opencensus.io/exporter/stackdriver/monitoredresource"
"github.com/go-kit/kit/log"
"go.opencensus.io/stats/view"
"go.opencensus.io/trace"
)

func initSDExporter(projectID, service, version string, lg log.Logger) error {
exporter, err := stackdriver.NewExporter(stackdriver.Options{
ProjectID: projectID,
MonitoredResource: gaeInterface{
func sdExporterOptions(projectID, service, version string, lg log.Logger) *stackdriver.Options {
var mr monitoredresource.Interface
if m := monitoredresource.Autodetect(); m != nil {
mr = m
} else if isGAE() {
mr = gaeInterface{
typ: "gae_app",
labels: map[string]string{
"project_id": projectID,
},
},
}
}
if mr == nil {
return nil
}

return &stackdriver.Options{
ProjectID: projectID,
MonitoredResource: mr,
OnError: func(err error) {
lg.Log("error", err,
"message", "tracing client encountered an error")
Expand All @@ -24,7 +38,15 @@ func initSDExporter(projectID, service, version string, lg log.Logger) error {
"service": service,
"version": version,
},
})
}
}

func googleProjectID() string {
return os.Getenv("GOOGLE_CLOUD_PROJECT")
}

func initSDExporter(opt stackdriver.Options) error {
exporter, err := stackdriver.NewExporter(opt)
if err != nil {
return err
}
Expand All @@ -35,9 +57,10 @@ func initSDExporter(projectID, service, version string, lg log.Logger) error {

// implements contrib.go.opencensus.io/exporter/stackdriver/monitoredresource.Interface
type gaeInterface struct {
typ string
labels map[string]string
}

func (g gaeInterface) MonitoredResource() (string, map[string]string) {
return "gae_app", g.labels
return g.typ, g.labels
}

0 comments on commit 27bac81

Please sign in to comment.