Skip to content

Commit

Permalink
Merge fe2b58b into dc1269d
Browse files Browse the repository at this point in the history
  • Loading branch information
marwan-at-work committed Feb 12, 2019
2 parents dc1269d + fe2b58b commit 4e25153
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 91 deletions.
109 changes: 109 additions & 0 deletions observe/observe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// Package observe provides functions
// that help with setting tracing/metrics
// in cloud providers, mainly GCP.
package observe

import (
"os"

"contrib.go.opencensus.io/exporter/stackdriver"
"contrib.go.opencensus.io/exporter/stackdriver/monitoredresource"
"go.opencensus.io/exporter/prometheus"
)

// NewStackDriverExporter will return the tracing and metrics through
// the stack driver exporter, if exists in the underlying platform.
// If exporter is registered, it returns the exporter so you can register
// it and ensure to call Flush on termination.
func NewStackDriverExporter(projectID string, onErr func(error)) (*stackdriver.Exporter, error) {
_, svcName, svcVersion := GetServiceInfo()
opts := getSDOpts(projectID, svcName, svcVersion, onErr)
if opts == nil {
return nil, nil
}
return stackdriver.NewExporter(*opts)
}

// NewPrometheusExporter return a prometheus Exporter for OpenCensus.
func NewPrometheusExporter(opts prometheus.Options) (*prometheus.Exporter, error) {
return prometheus.NewExporter(opts)
}

// GoogleProjectID returns the GCP Project ID
// that can be used to instantiate various
// GCP clients such as Stack Driver.
func GoogleProjectID() string {
return os.Getenv("GOOGLE_CLOUD_PROJECT")
}

// IsGAE tells you whether your program is running
// within the App Engine platform.
func IsGAE() bool {
return os.Getenv("GAE_DEPLOYMENT_ID") != ""
}

// GetGAEInfo returns the GCP Project ID,
// the service, and the version of the application.
func GetGAEInfo() (projectID, service, version string) {
return GoogleProjectID(),
os.Getenv("GAE_SERVICE"),
os.Getenv("GAE_VERSION")
}

// GetServiceInfo returns the GCP Project ID,
// the service name and version (gae or through
// GAE_SERVICE/GAE_VERSION env vars)
func GetServiceInfo() (projectID, service, version string) {
projectID = GoogleProjectID()
if IsGAE() {
_, service, version = GetGAEInfo()
} else if n, v := os.Getenv("SERVICE_NAME"), os.Getenv("SERVICE_VERSION"); n != "" {
service, version = n, v
}
return projectID, service, version
}

// getSDOpts returns Stack Driver Options that you can pass directly
// to the OpenCensus exporter or other libraries.
func getSDOpts(projectID, service, version string, onErr func(err error)) *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: onErr,
DefaultTraceAttributes: map[string]interface{}{
"service": service,
"version": version,
},
}
}

// IsGCPEnabled returns whether the running application
// is inside GCP or has access to its products.
func IsGCPEnabled() bool {
return monitoredresource.Autodetect() != nil || IsGAE()
}

// 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 g.typ, g.labels
}
30 changes: 19 additions & 11 deletions server/kit/kitserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,19 @@ import (
"net"
"net/http"
"net/http/pprof"
"os"
"strings"

"cloud.google.com/go/errorreporting"
sdpropagation "contrib.go.opencensus.io/exporter/stackdriver/propagation"
"github.com/NYTimes/gizmo/observe"
"github.com/go-kit/kit/log"
httptransport "github.com/go-kit/kit/transport/http"
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
"github.com/pkg/errors"
"go.opencensus.io/plugin/ocgrpc"
"go.opencensus.io/plugin/ochttp"
"go.opencensus.io/stats/view"
"go.opencensus.io/trace"
"go.opencensus.io/trace/propagation"
ocontext "golang.org/x/net/context"
"google.golang.org/grpc"
Expand All @@ -28,6 +30,7 @@ import (
type Server struct {
logger log.Logger
logClose func() error
ocFlush func()

errs *errorreporting.Client

Expand Down Expand Up @@ -89,27 +92,26 @@ func NewServer(svc Service) *Server {
propr propagation.HTTPFormat
)

projectID := googleProjectID()
var svcName, svcVersion string
if isGAE() {
_, svcName, svcVersion = getGAEInfo()
} else if n, v := os.Getenv("SERVICE_NAME"), os.Getenv("SERVICE_VERSION"); n != "" {
svcName, svcVersion = n, v
projectID, svcName, svcVersion := observe.GetServiceInfo()
onErr := func(err error) {
lg.Log("error", err, "message", "tracing client encountered an error")
}

if opt := sdExporterOptions(projectID, svcName, svcVersion, lg); opt != nil {
err = initSDExporter(*opt)
ocFlush := func() {}
if observe.IsGCPEnabled() {
exp, err := observe.NewStackDriverExporter(projectID, onErr)
if err != nil {
lg.Log("error", err,
"message", "unable to initiate error tracing exporter")
}
ocFlush = exp.Flush
trace.RegisterExporter(exp)
view.RegisterExporter(exp)

propr = &sdpropagation.HTTPFormat{}

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

OnError: func(err error) {
lg.Log("error", err,
"message", "error reporting client encountered an error")
Expand All @@ -127,6 +129,7 @@ func NewServer(svc Service) *Server {
exit: make(chan chan error),
logger: lg,
logClose: logClose,
ocFlush: ocFlush,
errs: errs,
}
s.svr = &http.Server{
Expand Down Expand Up @@ -337,6 +340,11 @@ func (s *Server) start() error {
s.logClose()
}

// flush the stack driver exporter
if s.ocFlush != nil {
s.ocFlush()
}

if s.errs != nil {
s.errs.Close()
}
Expand Down
3 changes: 2 additions & 1 deletion server/kit/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"os"

"github.com/NYTimes/gizmo/observe"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/transport/http"
"google.golang.org/grpc/metadata"
Expand All @@ -23,7 +24,7 @@ import (
// If an empty string is provided, "gae_log" will be used in App Engine and "stdout" elsewhere.
// For more information about to use of logID see the documentation here: https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#FIELDS.log_name
func NewLogger(ctx context.Context, logID string) (log.Logger, func() error, error) {
projectID, serviceID, svcVersion := getGAEInfo()
projectID, serviceID, svcVersion := observe.GetGAEInfo()
lg, cl, err := newStackdriverLogger(ctx, logID, projectID, serviceID, svcVersion)
// if Stackdriver logger was not able to find information about monitored resource it returns nil.
if err != nil {
Expand Down
15 changes: 2 additions & 13 deletions server/kit/sd_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,18 @@ import (
"encoding"
"encoding/json"
"fmt"
"os"
"reflect"
"strings"

"cloud.google.com/go/logging"
"contrib.go.opencensus.io/exporter/stackdriver/monitoredresource"
"github.com/NYTimes/gizmo/observe"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/pkg/errors"
"google.golang.org/genproto/googleapis/api/monitoredres"
)

// project, service, version
func getGAEInfo() (string, string, string) {
return googleProjectID(),
os.Getenv("GAE_SERVICE"),
os.Getenv("GAE_VERSION")
}

func isGAE() bool {
return os.Getenv("GAE_DEPLOYMENT_ID") != ""
}

type sdLogger struct {
project string
monRes *monitoredres.MonitoredResource
Expand All @@ -43,7 +32,7 @@ func newStackdriverLogger(ctx context.Context, logID, projectID, service, versio
"version_id": version,
},
}
if isGAE() {
if observe.IsGAE() {
resource.Type = "gae_app"
if logID == "" {
logID = "app_logs"
Expand Down
66 changes: 0 additions & 66 deletions server/kit/stackdriver.go

This file was deleted.

0 comments on commit 4e25153

Please sign in to comment.