-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Set up abstractions to expose metrics in index-provider. Add explicit metrics to measure time to sync and time to mirror ads as a first concrete usage. Relates to #241
- Loading branch information
Showing
8 changed files
with
202 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package metrics | ||
|
||
import "go.opentelemetry.io/otel/attribute" | ||
|
||
var Attributes struct { | ||
StatusFailure attribute.KeyValue | ||
StatusSuccess attribute.KeyValue | ||
} | ||
|
||
func init() { | ||
Attributes.StatusFailure = attribute.String("status", "failure") | ||
Attributes.StatusSuccess = attribute.String("status", "success") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package metrics | ||
|
||
import ( | ||
"go.opentelemetry.io/otel/metric/global" | ||
) | ||
|
||
var meter = global.MeterProvider().Meter("index-provider") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package metrics | ||
|
||
import ( | ||
"go.opentelemetry.io/otel/metric/instrument" | ||
"go.opentelemetry.io/otel/metric/instrument/syncint64" | ||
"go.opentelemetry.io/otel/metric/unit" | ||
) | ||
|
||
var Mirror struct { | ||
SyncDuration syncint64.Histogram | ||
ProcessDuration syncint64.Histogram | ||
} | ||
|
||
func init() { | ||
var err error | ||
if Mirror.ProcessDuration, err = meter.SyncInt64().Histogram( | ||
"index-provider/mirror/process_duration", | ||
instrument.WithUnit(unit.Milliseconds), | ||
instrument.WithDescription("The time taken to process ad mirroring in milliseconds"), | ||
); err != nil { | ||
panic(err) | ||
} | ||
if Mirror.SyncDuration, err = meter.SyncInt64().Histogram( | ||
"index-provider/mirror/sync_duration", | ||
instrument.WithUnit(unit.Milliseconds), | ||
instrument.WithDescription("The time taken to sync content in milliseconds"), | ||
); err != nil { | ||
panic(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package metrics | ||
|
||
import ( | ||
"context" | ||
"net" | ||
"net/http" | ||
|
||
logging "github.com/ipfs/go-log/v2" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/promhttp" | ||
otelprom "go.opentelemetry.io/otel/exporters/prometheus" | ||
"go.opentelemetry.io/otel/metric/global" | ||
"go.opentelemetry.io/otel/sdk/metric" | ||
) | ||
|
||
var log = logging.Logger("provider/metrics") | ||
|
||
type Server struct { | ||
exporter otelprom.Exporter | ||
httpserver http.Server | ||
listen net.Listener | ||
} | ||
|
||
// NewServer instantiates a new server that upon start exposes the collected metrics as Prometheus | ||
// metrics. | ||
func NewServer(listenAddr string) (*Server, error) { | ||
listen, err := net.Listen("tcp", listenAddr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
exporter := otelprom.New() | ||
provider := metric.NewMeterProvider(metric.WithReader(exporter)) | ||
global.SetMeterProvider(provider) | ||
if err := prometheus.Register(exporter.Collector); err != nil { | ||
return nil, err | ||
} | ||
|
||
mux := http.NewServeMux() | ||
mux.Handle("/metrics", promhttp.HandlerFor(prometheus.DefaultGatherer, promhttp.HandlerOpts{ | ||
EnableOpenMetrics: true, | ||
})) | ||
return &Server{ | ||
exporter: exporter, | ||
httpserver: http.Server{ | ||
Handler: mux, | ||
}, | ||
listen: listen, | ||
}, nil | ||
} | ||
|
||
func (s *Server) Start() error { | ||
log.Infow("Starting metrics server", "listenAddr", s.listen.Addr()) | ||
go func() { | ||
err := s.httpserver.Serve(s.listen) | ||
log.Infow("Stopped metric server", "err", err) | ||
}() | ||
return nil | ||
} | ||
|
||
func (s *Server) Shutdown(ctx context.Context) error { | ||
sErr := s.httpserver.Shutdown(ctx) | ||
eErr := s.exporter.Shutdown(ctx) | ||
if sErr != nil { | ||
return sErr | ||
} | ||
return eErr | ||
} |
Oops, something went wrong.