Skip to content

Commit

Permalink
Clean up Stackdriver point count metric (#777)
Browse files Browse the repository at this point in the history
  • Loading branch information
james-bebbington committed Aug 20, 2020
1 parent 05ab6c5 commit 7603148
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 29 deletions.
9 changes: 9 additions & 0 deletions exporter/stackdriverexporter/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ package stackdriverexporter

import (
"context"
"sync"

"go.opencensus.io/stats/view"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/configmodels"
"go.opentelemetry.io/collector/exporter/exporterhelper"
Expand All @@ -27,8 +29,15 @@ const (
typeStr = "stackdriver"
)

var once sync.Once

// NewFactory creates a factory for the stackdriver exporter
func NewFactory() component.ExporterFactory {
// register view for self-observability
once.Do(func() {
view.Register(viewPointCount)
})

return exporterhelper.NewFactory(
typeStr,
createDefaultConfig,
Expand Down
26 changes: 17 additions & 9 deletions exporter/stackdriverexporter/observability.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,8 @@ import (
"google.golang.org/grpc/status"
)

// TODO: re-think if processor should register it's own telemetry views or if some other
// mechanism should be used by the collector to discover views from all components

func init() {
view.Register(viewPointCount)
}

var (
pointCount = stats.Int64("otelcol/stackdriver/point_count", "Count of metric points written to Cloud Monitoring.", "1")
pointCount = stats.Int64("otelcol/googlecloudmonitoring/point_count", "Count of metric points written to Cloud Monitoring.", "1")
statusKey = tag.MustNewKey("status")
)

Expand All @@ -45,7 +38,22 @@ var viewPointCount = &view.View{
TagKeys: []tag.Key{statusKey},
}

func recordPointCount(ctx context.Context, points int, status string) {
func recordPointCount(ctx context.Context, success, dropped int, grpcErr error) {
if success > 0 {
recordPointCountDataPoint(ctx, success, "OK")
}

if dropped > 0 {
var st string
s, ok := status.FromError(grpcErr)
if ok {
st = statusCodeToString(s)
}
recordPointCountDataPoint(ctx, dropped, st)
}
}

func recordPointCountDataPoint(ctx context.Context, points int, status string) {
ctx, err := tag.New(ctx, tag.Insert(statusKey, status))
if err != nil {
return
Expand Down
33 changes: 13 additions & 20 deletions exporter/stackdriverexporter/stackdriver.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
traceexport "go.opentelemetry.io/otel/sdk/export/trace"
"google.golang.org/api/option"
"google.golang.org/grpc"
"google.golang.org/grpc/status"
)

const name = "stackdriver"
Expand Down Expand Up @@ -151,31 +150,25 @@ func newStackdriverMetricsExporter(cfg *Config) (component.MetricsExporter, erro

// pushMetrics calls StackdriverExporter.PushMetricsProto on each element of the given metrics
func (me *metricsExporter) pushMetrics(ctx context.Context, m pdata.Metrics) (int, error) {
var errors []error
var totalDropped int

mds := pdatautil.MetricsToMetricsData(m)
dropped := 0
for _, md := range mds {
d, err := me.mexporter.PushMetricsProto(ctx, md.Node, md.Resource, md.Metrics)
dropped += d

var points int
var st string
_, points := pdatautil.TimeseriesAndPointCount(md)
dropped, err := me.mexporter.PushMetricsProto(ctx, md.Node, md.Resource, md.Metrics)
recordPointCount(ctx, points-dropped, dropped, err)
totalDropped += dropped
if err != nil {
points = d
s, ok := status.FromError(err)
if ok {
st = statusCodeToString(s)
}
} else {
_, points = pdatautil.TimeseriesAndPointCount(md)
st = "OK"
errors = append(errors, err)
}
recordPointCount(ctx, points, st)
}

if err != nil {
return dropped, err
}
if len(errors) > 0 {
return totalDropped, componenterror.CombineErrors(errors)
}
return dropped, nil

return totalDropped, nil
}

// pushTraces calls texporter.ExportSpan for each span in the given traces
Expand Down

0 comments on commit 7603148

Please sign in to comment.