-
-
Notifications
You must be signed in to change notification settings - Fork 539
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
250 additions
and
26 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
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,62 @@ | ||
package metrics | ||
|
||
import ( | ||
"go.opentelemetry.io/otel/metric" | ||
) | ||
|
||
// AppMetrics holds all the application metrics | ||
type AppMetrics struct { | ||
metric.Meter | ||
|
||
RegisteredPeers metric.Int64UpDownCounter | ||
RegisterTimes metric.Float64Histogram | ||
RegisterCalls metric.Int64Counter | ||
DeregisterCalls metric.Int64Counter | ||
} | ||
|
||
func NewAppMetrics(meter metric.Meter) (*AppMetrics, error) { | ||
registeredPeers, err := meter.Int64UpDownCounter("registered_peers_total") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
registerTimes, err := meter.Float64Histogram("register_times_milliseconds", | ||
metric.WithExplicitBucketBoundaries(getStandardBucketBoundaries()...)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
registerCalls, err := meter.Int64Counter("register_calls_total") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
deregisterCalls, err := meter.Int64Counter("deregister_calls_total") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &AppMetrics{ | ||
Meter: meter, | ||
RegisteredPeers: registeredPeers, | ||
RegisterTimes: registerTimes, | ||
RegisterCalls: registerCalls, | ||
DeregisterCalls: deregisterCalls, | ||
}, nil | ||
} | ||
|
||
func getStandardBucketBoundaries() []float64 { | ||
return []float64{ | ||
0.1, | ||
0.5, | ||
1, | ||
5, | ||
10, | ||
50, | ||
100, | ||
500, | ||
1000, | ||
5000, | ||
10000, | ||
} | ||
} |
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,74 @@ | ||
package metrics | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"reflect" | ||
|
||
prometheus2 "github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/promhttp" | ||
"go.opentelemetry.io/otel" | ||
"go.opentelemetry.io/otel/exporters/prometheus" | ||
api "go.opentelemetry.io/otel/metric" | ||
"go.opentelemetry.io/otel/sdk/metric" | ||
) | ||
|
||
const defaultEndpoint = "/metrics" | ||
|
||
// Metrics holds the metrics information and exposes it | ||
type Metrics struct { | ||
Meter api.Meter | ||
provider *metric.MeterProvider | ||
Endpoint string | ||
|
||
*http.Server | ||
} | ||
|
||
// NewServer initializes and returns a new Metrics instance | ||
func NewServer(port int, endpoint string) *Metrics { | ||
exporter, err := prometheus.New() | ||
if err != nil { | ||
return nil | ||
} | ||
|
||
provider := metric.NewMeterProvider(metric.WithReader(exporter)) | ||
otel.SetMeterProvider(provider) | ||
|
||
pkg := reflect.TypeOf(defaultEndpoint).PkgPath() | ||
meter := provider.Meter(pkg) | ||
|
||
if endpoint == "" { | ||
endpoint = defaultEndpoint | ||
} | ||
|
||
router := http.NewServeMux() | ||
router.Handle(endpoint, promhttp.HandlerFor( | ||
prometheus2.DefaultGatherer, | ||
promhttp.HandlerOpts{EnableOpenMetrics: true})) | ||
|
||
server := &http.Server{ | ||
Addr: fmt.Sprintf(":%d", port), | ||
Handler: router, | ||
} | ||
|
||
return &Metrics{ | ||
Meter: meter, | ||
provider: provider, | ||
Endpoint: endpoint, | ||
Server: server, | ||
} | ||
} | ||
|
||
// Shutdown stops the metrics server | ||
func (m *Metrics) Shutdown(ctx context.Context) error { | ||
if err := m.Server.Shutdown(ctx); err != nil { | ||
return fmt.Errorf("http server: %w", err) | ||
} | ||
|
||
if err := m.provider.Shutdown(ctx); err != nil { | ||
return fmt.Errorf("meter provider: %w", err) | ||
} | ||
|
||
return nil | ||
} |
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
Oops, something went wrong.