Skip to content

Commit

Permalink
Adding metrics (#249)
Browse files Browse the repository at this point in the history
This change adds metrics reporting to the tracer.

Co-Authored-By: Isobel Redelmeier <[email protected]>
  • Loading branch information
alrex and iredelmeier authored Mar 31, 2020
1 parent e23ae13 commit 17fbf59
Show file tree
Hide file tree
Showing 17 changed files with 904 additions and 35 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Changelog

## [Pending Release](https://github.com/lightstep/lightstep-tracer-go/compare/v0.19.0...HEAD)
## [Pending Release](https://github.com/lightstep/lightstep-tracer-go/compare/v0.20.0...HEAD)

## [v0.20.0](https://github.com/lightstep/lightstep-tracer-go/compare/v0.19.0...v0.20.0)
* Adding support for reporting metrics to Lightstep
* Updating opencensus dependency

## [v0.19.0](https://github.com/lightstep/lightstep-tracer-go/compare/v0.18.1...v0.19.0)
* Add flush duration to status report struct.
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ logAndMetricsHandler := func(event lightstep.Event){
switch event := event.(type) {
case EventStatusReport:
metrics.Count("tracer.dropped_spans", event.DroppedSpans())
case MetricEventStatusReport:
metrics.Count("tracer.sent_metrics", event.SentMetrics())
case ErrorEvent:
logger.Error("LS Tracer error: %s", event)
default:
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.19.0
0.20.0
10 changes: 10 additions & 0 deletions constants/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package constants

const (
// ComponentNameKey is the tag key identifying the service
ComponentNameKey = "lightstep.component_name"
// HostnameKey is the tag key identifying hostname
HostnameKey = "lightstep.hostname"
// ServiceVersionKey is the tag key identifying service version
ServiceVersionKey = "service.version"
)
101 changes: 93 additions & 8 deletions events.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,11 @@ func (e *eventFlushError) State() EventFlushErrorState {
}

func (e *eventFlushError) String() string {
return e.err.Error()
return e.Error()
}

func (e *eventFlushError) Error() string {
return e.err.Error()
return e.Err().Error()
}

func (e *eventFlushError) Err() error {
Expand All @@ -131,11 +131,11 @@ func (*eventConnectionError) Event() {}
func (*eventConnectionError) EventConnectionError() {}

func (e *eventConnectionError) String() string {
return e.err.Error()
return e.Error()
}

func (e *eventConnectionError) Error() string {
return e.err.Error()
return e.Err().Error()
}

func (e *eventConnectionError) Err() error {
Expand Down Expand Up @@ -173,6 +173,22 @@ type EventStatusReport interface {
FlushDuration() time.Duration
}

// MetricEventStatusReport occurs every time metrics are sent successfully.. It
// contains all metrics collected since the previous successful flush.
type MetricEventStatusReport interface {
Event
MetricEventStatusReport()

// StartTime is the earliest time a span was added to the report buffer.
StartTime() time.Time

// FinishTime is the latest time a span was added to the report buffer.
FinishTime() time.Time

// SentMetrics is the number of metrics sent in the report buffer.
SentMetrics() int
}

type eventStatusReport struct {
startTime time.Time
finishTime time.Time
Expand Down Expand Up @@ -270,11 +286,11 @@ func (e *eventUnsupportedTracer) Tracer() opentracing.Tracer {
}

func (e *eventUnsupportedTracer) String() string {
return e.err.Error()
return e.Error()
}

func (e *eventUnsupportedTracer) Error() string {
return e.err.Error()
return e.Err().Error()
}

func (e *eventUnsupportedTracer) Err() error {
Expand Down Expand Up @@ -322,11 +338,11 @@ func (e *eventUnsupportedValue) Value() interface{} {
}

func (e *eventUnsupportedValue) String() string {
return e.err.Error()
return e.Error()
}

func (e *eventUnsupportedValue) Error() string {
return e.err.Error()
return e.Err().Error()
}

func (e *eventUnsupportedValue) Err() error {
Expand All @@ -353,3 +369,72 @@ func (eventTracerDisabled) EventTracerDisabled() {}
func (eventTracerDisabled) String() string {
return tracerDisabled
}

type EventSystemMetricsMeasurementFailed interface {
ErrorEvent
}

type eventSystemMetricsMeasurementFailed struct {
err error
}

func newEventSystemMetricsMeasurementFailed(err error) *eventSystemMetricsMeasurementFailed {
return &eventSystemMetricsMeasurementFailed{
err: err,
}
}

func (e *eventSystemMetricsMeasurementFailed) Event() {}

func (e *eventSystemMetricsMeasurementFailed) String() string {
return e.Error()
}

func (e *eventSystemMetricsMeasurementFailed) Error() string {
return e.Err().Error()
}

func (e *eventSystemMetricsMeasurementFailed) Err() error {
return e.err
}

type eventSystemMetricsStatusReport struct {
startTime time.Time
finishTime time.Time
sentMetrics int
}

func newEventSystemMetricsStatusReport(
startTime, finishTime time.Time,
sentMetrics int,
) *eventSystemMetricsStatusReport {
return &eventSystemMetricsStatusReport{
startTime: startTime,
finishTime: finishTime,
sentMetrics: sentMetrics,
}
}

func (e *eventSystemMetricsStatusReport) Event() {}

func (e *eventSystemMetricsStatusReport) MetricEventStatusReport() {}

func (e *eventSystemMetricsStatusReport) String() string {
return fmt.Sprint(
"METRICS STATUS REPORT start: ", e.startTime,
", end: ", e.finishTime,
", sent metrics: ", e.sentMetrics,
)
}

func (e *eventSystemMetricsStatusReport) StartTime() time.Time {
return e.startTime
}

func (e *eventSystemMetricsStatusReport) FinishTime() time.Time {
return e.finishTime
}

func (e *eventSystemMetricsStatusReport) SentMetrics() int {
return e.sentMetrics
}
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ module github.com/lightstep/lightstep-tracer-go
go 1.12

require (
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-ole/go-ole v1.2.4 // indirect
github.com/gogo/protobuf v1.2.1
github.com/lightstep/lightstep-tracer-common/golang/gogo v0.0.0-20190605223551-bc2310a04743
github.com/lightstep/lightstep-tracer-common/golang/gogo v0.0.0-20200305213919-a88bf8de3718
github.com/onsi/ginkgo v1.7.0
github.com/onsi/gomega v1.4.3
github.com/opentracing/opentracing-go v1.0.2
github.com/shirou/gopsutil v2.20.1+incompatible
go.opencensus.io v0.22.3
google.golang.org/grpc v1.21.0
)
17 changes: 8 additions & 9 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d h1:G0m3OIz70MZUWq3EgK3CesDbo8upS2Vm9/P3FtgI+Jk=
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/go-ole/go-ole v1.2.4 h1:nNBDSCOigTSiarFpYE9J/KtEA1IOW4CNeqT9TQDqCxI=
github.com/go-ole/go-ole v1.2.4/go.mod h1:XCwSNxSkXRo4vlyPy93sltvi/qJq0jqQhjqQNIwKuxM=
github.com/gogo/protobuf v1.2.1 h1:/s5zKNz0uPFCZ5hddgPdo2TK2TVrUNMn0OOX8/aZMTE=
github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58=
Expand All @@ -22,8 +26,8 @@ github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/lightstep/lightstep-tracer-common/golang/gogo v0.0.0-20190605223551-bc2310a04743 h1:143Bb8f8DuGWck/xpNUOckBVYfFbBTnLevfRZ1aVVqo=
github.com/lightstep/lightstep-tracer-common/golang/gogo v0.0.0-20190605223551-bc2310a04743/go.mod h1:qklhhLq1aX+mtWk9cPHPzaBjWImj5ULL6C7HFJtXQMM=
github.com/lightstep/lightstep-tracer-common/golang/gogo v0.0.0-20200305213919-a88bf8de3718 h1:lrdADj7ifyBpqGJ+cT4vE5ztUoAF87uUf76+epwPViY=
github.com/lightstep/lightstep-tracer-common/golang/gogo v0.0.0-20200305213919-a88bf8de3718/go.mod h1:qklhhLq1aX+mtWk9cPHPzaBjWImj5ULL6C7HFJtXQMM=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.7.0 h1:WSHQ+IS43OoUrWtD1/bbclrwK8TTH5hzp+umCiuxHgs=
github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
Expand All @@ -33,6 +37,8 @@ github.com/opentracing/opentracing-go v1.0.2 h1:3jA2P6O1F9UOrWVpwrIo17pu01KWvNWg
github.com/opentracing/opentracing-go v1.0.2/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/shirou/gopsutil v2.20.1+incompatible h1:oIq9Cq4i84Hk8uQAUOG3eNdI/29hBawGrD5YRl6JRDY=
github.com/shirou/gopsutil v2.20.1+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
Expand All @@ -47,24 +53,19 @@ golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73r
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190311183353-d8887717615a h1:oWX7TPOiFAMXLq8o0ikBYfCJVlRHBcsciT5bXOrH628=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190603091049-60506f45cf65 h1:+rhAzEzT3f4JtomfC371qB+0Ola2caSKcY69NUBZrRQ=
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859 h1:R/3boaszxrf1GEUWTVDzSKVwLmSJpwZ1yqXm8j0v2QI=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6 h1:bjcUS9ztw9kFmmIxJInhon/0Is3p+EHBKNgquIzo1OI=
golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a h1:1BGLXjeY4akVXGgbC9HugT3Jv3hCI0z56oJR5vAMgBU=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190502145724-3ef323f4f1fd h1:r7DufRZuZbWB7j439YfAzP8RPDa9unLkpwQKUYbIMPI=
golang.org/x/sys v0.0.0-20190502145724-3ef323f4f1fd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
Expand All @@ -79,7 +80,6 @@ google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoA
google.golang.org/genproto v0.0.0-20190425155659-357c62f0e4bb/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE=
google.golang.org/genproto v0.0.0-20190530194941-fb225487d101 h1:wuGevabY6r+ivPNagjUXGGxF+GqgMd+dBhjsxW4q9u4=
google.golang.org/genproto v0.0.0-20190530194941-fb225487d101/go.mod h1:z3L6/3dTEVtUr6QSP8miRzeRqwQOioJ9I66odjN4I7s=
google.golang.org/grpc v1.19.0 h1:cfg4PD8YEdSFnm7qLV4++93WcmhH2nIUhMjhdCvl3j8=
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
google.golang.org/grpc v1.20.1/go.mod h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiqXj38=
google.golang.org/grpc v1.21.0 h1:G+97AoqBnmZIT91cLG/EkCoK9NSelj64P8bOHHNmGn0=
Expand All @@ -90,7 +90,6 @@ gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v2 v2.2.1 h1:mUhvW9EsL+naU5Q3cakzfE91YhliOondGd6ZrsDBHQE=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Expand Down
47 changes: 47 additions & 0 deletions internal/metrics/internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package metrics

import (
"fmt"
"time"

"github.com/onsi/ginkgo"
"github.com/onsi/gomega"
)

var _ = ginkgo.Describe("the reporter", func() {
ginkgo.Describe("configuration", func() {
ginkgo.It("uses default values", func() {
defaultReporter := NewReporter()
gomega.Expect(defaultReporter.tracerID).To(gomega.Equal(uint64(0)))
gomega.Expect(defaultReporter.attributes).To(gomega.Equal(map[string]string{}))
gomega.Expect(defaultReporter.address).To(gomega.Equal(fmt.Sprintf("%s%s", defaultReporterAddress, reporterPath)))
gomega.Expect(defaultReporter.timeout).To(gomega.Equal(defaultReporterTimeout))
gomega.Expect(defaultReporter.measurementDuration).To(gomega.Equal(defaultReporterMeasurementDuration))
gomega.Expect(defaultReporter.accessToken).To(gomega.Equal(""))
})
ginkgo.It("uses configured values", func() {
duration := time.Second * 1
accessToken := "token-1234"
tracerID := uint64(1234)
attributes := map[string]string{
"key1": "val1",
"key2": "val2",
}
address := "http://localhost:8080"
defaultReporter := NewReporter(
WithReporterTracerID(tracerID),
WithReporterAttributes(attributes),
WithReporterAddress(address),
WithReporterTimeout(duration),
WithReporterMeasurementDuration(duration),
WithReporterAccessToken(accessToken),
)
gomega.Expect(defaultReporter.tracerID).To(gomega.Equal(tracerID))
gomega.Expect(defaultReporter.attributes).To(gomega.Equal(attributes))
gomega.Expect(defaultReporter.address).To(gomega.Equal(fmt.Sprintf("%s%s", address, reporterPath)))
gomega.Expect(defaultReporter.timeout).To(gomega.Equal(duration))
gomega.Expect(defaultReporter.measurementDuration).To(gomega.Equal(duration))
gomega.Expect(defaultReporter.accessToken).To(gomega.Equal(accessToken))
})
})
})
Loading

0 comments on commit 17fbf59

Please sign in to comment.