Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
Signed-off-by: bitliu <[email protected]>
  • Loading branch information
Xunzhuo committed Oct 26, 2023
1 parent 778dd57 commit f21ee8e
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 14 deletions.
4 changes: 4 additions & 0 deletions api/v1alpha1/shared_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ const (
DefaultEnvoyProxyImage = "envoyproxy/envoy-dev:latest"
// DefaultRateLimitImage is the default image used by ratelimit.
DefaultRateLimitImage = "envoyproxy/ratelimit:master"
// HTTPProtocol is the common-used http protocol.
HTTPProtocol = "http"
// GRPCProtocol is the common-used grpc protocol.
GRPCProtocol = "grpc"
)

// GroupVersionKind unambiguously identifies a Kind.
Expand Down
18 changes: 9 additions & 9 deletions internal/metrics/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,43 +57,43 @@ type Metadata struct {
}

// metrics stores stores metrics
type metricstore struct {
type store struct {
started bool
mu sync.Mutex
stores map[string]Metadata
}

// stores is a global that stores all registered metrics
var stores = metricstore{
var stores = store{
stores: map[string]Metadata{},
}

// register records a newly defined metric. Only valid before an exporter is set.
func (d *metricstore) register(metricstore Metadata) {
func (d *store) register(store Metadata) {
d.mu.Lock()
defer d.mu.Unlock()
if d.started {
metricsLogger.Error(errors.New("cannot initialize metric after metric has started"), "metric", metricstore.Name)
metricsLogger.Error(errors.New("cannot initialize metric after metric has started"), "metric", store.Name)
}
d.stores[metricstore.Name] = metricstore
d.stores[store.Name] = store
}

// preAddOptions runs pre-run steps before adding to meter provider.
func (d *metricstore) preAddOptions() []metric.Option {
func (d *store) preAddOptions() []metric.Option {
d.mu.Lock()
defer d.mu.Unlock()
d.started = true
opts := []metric.Option{}
for name, metricstore := range d.stores {
if metricstore.Bounds == nil {
for name, store := range d.stores {
if store.Bounds == nil {
continue
}
// for each histogram metric (i.e. those with bounds), set up a view explicitly defining those buckets.
v := metric.WithView(metric.NewView(
metric.Instrument{Name: name},
metric.Stream{
Aggregation: metric.AggregationExplicitBucketHistogram{
Boundaries: metricstore.Bounds,
Boundaries: store.Bounds,
}},
))
opts = append(opts, v)
Expand Down
2 changes: 1 addition & 1 deletion internal/metrics/otel_metric_counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (f *Counter) Increment() {
}

func (f *Counter) Decrement() {
f.Add(1)
f.Add(-1)
}

func (f *Counter) With(labelValues ...LabelValue) *Counter {
Expand Down
9 changes: 5 additions & 4 deletions internal/metrics/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package metrics
import (
"context"
"fmt"
"net"
"net/http"
"time"

Expand Down Expand Up @@ -73,7 +74,7 @@ func start(address string, handler http.Handler) error {

func newOptions(svr *config.Server) registerOptions {
newOpts := registerOptions{}
newOpts.address = fmt.Sprintf("%s:%d", v1alpha1.GatewayMetricsHost, v1alpha1.GatewayMetricsPort)
newOpts.address = net.JoinHostPort(v1alpha1.GatewayMetricsHost, fmt.Sprint(v1alpha1.GatewayMetricsPort))

if !svr.EnvoyGateway.IfDisablePrometheus() {
newOpts.pullOptions.disable = true
Expand Down Expand Up @@ -142,7 +143,7 @@ func registerOTELPromExporter(otelOpts *[]metric.Option, opts registerOptions) e
// registerOTELHTTPexporter registers OTEL HTTP metrics exporter (PUSH mode).
func registerOTELHTTPexporter(otelOpts *[]metric.Option, opts registerOptions) error {
for _, sink := range opts.pushOptions.sinks {
if sink.protocol == "http" {
if sink.protocol == v1alpha1.HTTPProtocol {
address := fmt.Sprintf("%s:%d", sink.host, sink.port)
httpexporter, err := otlpmetrichttp.New(
context.Background(),
Expand All @@ -165,8 +166,8 @@ func registerOTELHTTPexporter(otelOpts *[]metric.Option, opts registerOptions) e
// registerOTELgRPCexporter registers OTEL gRPC metrics exporter (PUSH mode).
func registerOTELgRPCexporter(otelOpts *[]metric.Option, opts registerOptions) error {
for _, sink := range opts.pushOptions.sinks {
if sink.protocol == "grpc" {
address := fmt.Sprintf("%s:%d", sink.host, sink.port)
if sink.protocol == v1alpha1.GRPCProtocol {
address := net.JoinHostPort(sink.host, fmt.Sprint(sink.port))
httpexporter, err := otlpmetricgrpc.New(
context.Background(),
otlpmetricgrpc.WithEndpoint(address),
Expand Down

0 comments on commit f21ee8e

Please sign in to comment.