diff --git a/metrics/readme.go b/metrics/readme.go index bb64f91..bd49412 100644 --- a/metrics/readme.go +++ b/metrics/readme.go @@ -2,31 +2,23 @@ package metrics import ( "github.com/foomo/keel/markdown" - "github.com/foomo/keel/telemetry/nonrecording" "github.com/prometheus/client_golang/prometheus" ) func Readme() string { md := markdown.Markdown{} - values := nonrecording.Metrics() + var rows [][]string - gatherer, _ := prometheus.DefaultRegisterer.(*prometheus.Registry).Gather() - for _, value := range gatherer { - values = append(values, nonrecording.Metric{ - Name: value.GetName(), - Type: value.GetType().String(), - Help: value.GetHelp(), - }) + if gatherer, err := prometheus.DefaultGatherer.Gather(); err == nil { + for _, value := range gatherer { + rows = append(rows, []string{ + value.GetName(), + value.GetType().String(), + value.GetHelp(), + }) + } } - rows := make([][]string, 0, len(values)) - for _, value := range values { - rows = append(rows, []string{ - markdown.Code(value.Name), - value.Type, - value.Help, - }) - } if len(rows) > 0 { md.Println("### Metrics") md.Println("") diff --git a/service/httpreadme_test.go b/service/httpreadme_test.go index 7326c63..3da0c69 100644 --- a/service/httpreadme_test.go +++ b/service/httpreadme_test.go @@ -11,6 +11,9 @@ import ( "github.com/foomo/keel/config" "github.com/foomo/keel/env" "github.com/foomo/keel/service" + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" + "go.opentelemetry.io/otel/metric/instrument" "go.uber.org/zap" ) @@ -21,6 +24,7 @@ func ExampleNewHTTPReadme() { svr := keel.NewServer( keel.WithLogger(zap.NewNop()), + keel.WithPrometheusMeter(true), keel.WithHTTPReadmeService(true), ) @@ -40,6 +44,18 @@ func ExampleNewHTTPReadme() { _ = config.MustGetBool(c, "example.required.bool") _ = config.MustGetString(c, "example.required.string") + m := svr.Meter() + + // add metrics + fooBarCounter := promauto.NewCounter(prometheus.CounterOpts{ + Name: "foo_bar_total", + Help: "Foo bar metrics", + }) + fooBazCounter, _ := m.SyncInt64().Counter("foo_baz_total", instrument.WithDescription("Foo baz metrics")) + + fooBarCounter.Add(1) + fooBazCounter.Add(svr.Context(), 1) + // add http service svr.AddService(service.NewHTTP(l, "demp-http", "localhost:8080", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) @@ -136,6 +152,8 @@ func ExampleNewHTTPReadme() { // // | Name | Type | Description | // | ---------------------------------- | ------- | ------------------------------------------------------------------ | + // | foo_bar_total | COUNTER | Foo bar metrics | + // | foo_baz_total | COUNTER | Foo baz metrics | // | `go_gc_duration_seconds` | SUMMARY | A summary of the pause duration of garbage collection cycles. | // | `go_goroutines` | GAUGE | Number of goroutines that currently exist. | // | `go_info` | GAUGE | Information about the Go environment. | diff --git a/telemetry/nonrecording/instruments.go b/telemetry/nonrecording/instruments.go deleted file mode 100644 index d11c927..0000000 --- a/telemetry/nonrecording/instruments.go +++ /dev/null @@ -1,162 +0,0 @@ -// Copyright The OpenTelemetry Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package nonrecording - -import ( - "context" - "strings" - - "go.opentelemetry.io/otel/attribute" - "go.opentelemetry.io/otel/metric/instrument" - "go.opentelemetry.io/otel/metric/instrument/asyncfloat64" - "go.opentelemetry.io/otel/metric/instrument/asyncint64" - "go.opentelemetry.io/otel/metric/instrument/syncfloat64" - "go.opentelemetry.io/otel/metric/instrument/syncint64" -) - -type Metric struct { - Name string - Type string - Help string -} - -var metrics []Metric - -func Metrics() []Metric { - return metrics -} - -type nonrecordingAsyncFloat64Instrument struct { - instrument.Asynchronous -} - -var ( - _ asyncfloat64.InstrumentProvider = nonrecordingAsyncFloat64Instrument{} - _ asyncfloat64.Counter = nonrecordingAsyncFloat64Instrument{} - _ asyncfloat64.UpDownCounter = nonrecordingAsyncFloat64Instrument{} - _ asyncfloat64.Gauge = nonrecordingAsyncFloat64Instrument{} -) - -func (n nonrecordingAsyncFloat64Instrument) Counter(name string, opts ...instrument.Option) (asyncfloat64.Counter, error) { - metrics = append(metrics, Metric{Name: strings.ReplaceAll(name, ".", "_"), Type: "COUNTER", Help: instrument.NewConfig(opts...).Description()}) - return n, nil -} - -func (n nonrecordingAsyncFloat64Instrument) UpDownCounter(name string, opts ...instrument.Option) (asyncfloat64.UpDownCounter, error) { - metrics = append(metrics, Metric{Name: strings.ReplaceAll(name, ".", "_"), Type: "UPDOWNCOUNTER", Help: instrument.NewConfig(opts...).Description()}) - return n, nil -} - -func (n nonrecordingAsyncFloat64Instrument) Gauge(name string, opts ...instrument.Option) (asyncfloat64.Gauge, error) { - metrics = append(metrics, Metric{Name: strings.ReplaceAll(name, ".", "_"), Type: "GAUGE", Help: instrument.NewConfig(opts...).Description()}) - return n, nil -} - -func (nonrecordingAsyncFloat64Instrument) Observe(context.Context, float64, ...attribute.KeyValue) { -} - -type nonrecordingAsyncInt64Instrument struct { - instrument.Asynchronous -} - -var ( - _ asyncint64.InstrumentProvider = nonrecordingAsyncInt64Instrument{} - _ asyncint64.Counter = nonrecordingAsyncInt64Instrument{} - _ asyncint64.UpDownCounter = nonrecordingAsyncInt64Instrument{} - _ asyncint64.Gauge = nonrecordingAsyncInt64Instrument{} -) - -func (n nonrecordingAsyncInt64Instrument) Counter(name string, opts ...instrument.Option) (asyncint64.Counter, error) { - metrics = append(metrics, Metric{Name: strings.ReplaceAll(name, ".", "_"), Type: "COUNTER", Help: instrument.NewConfig(opts...).Description()}) - return n, nil -} - -func (n nonrecordingAsyncInt64Instrument) UpDownCounter(name string, opts ...instrument.Option) (asyncint64.UpDownCounter, error) { - metrics = append(metrics, Metric{Name: strings.ReplaceAll(name, ".", "_"), Type: "UPDOWNCOUNTER", Help: instrument.NewConfig(opts...).Description()}) - return n, nil -} - -func (n nonrecordingAsyncInt64Instrument) Gauge(name string, opts ...instrument.Option) (asyncint64.Gauge, error) { - metrics = append(metrics, Metric{Name: strings.ReplaceAll(name, ".", "_"), Type: "GAUGE", Help: instrument.NewConfig(opts...).Description()}) - return n, nil -} - -func (nonrecordingAsyncInt64Instrument) Observe(context.Context, int64, ...attribute.KeyValue) { -} - -type nonrecordingSyncFloat64Instrument struct { - instrument.Synchronous -} - -var ( - _ syncfloat64.InstrumentProvider = nonrecordingSyncFloat64Instrument{} - _ syncfloat64.Counter = nonrecordingSyncFloat64Instrument{} - _ syncfloat64.UpDownCounter = nonrecordingSyncFloat64Instrument{} - _ syncfloat64.Histogram = nonrecordingSyncFloat64Instrument{} -) - -func (n nonrecordingSyncFloat64Instrument) Counter(name string, opts ...instrument.Option) (syncfloat64.Counter, error) { - metrics = append(metrics, Metric{Name: strings.ReplaceAll(name, ".", "_"), Type: "COUNTER", Help: instrument.NewConfig(opts...).Description()}) - return n, nil -} - -func (n nonrecordingSyncFloat64Instrument) UpDownCounter(name string, opts ...instrument.Option) (syncfloat64.UpDownCounter, error) { - metrics = append(metrics, Metric{Name: strings.ReplaceAll(name, ".", "_"), Type: "UPDOWNCOUNTER", Help: instrument.NewConfig(opts...).Description()}) - return n, nil -} - -func (n nonrecordingSyncFloat64Instrument) Histogram(name string, opts ...instrument.Option) (syncfloat64.Histogram, error) { - metrics = append(metrics, Metric{Name: strings.ReplaceAll(name, ".", "_"), Type: "HISTOGRAM", Help: instrument.NewConfig(opts...).Description()}) - return n, nil -} - -func (nonrecordingSyncFloat64Instrument) Add(context.Context, float64, ...attribute.KeyValue) { - -} - -func (nonrecordingSyncFloat64Instrument) Record(context.Context, float64, ...attribute.KeyValue) { - -} - -type nonrecordingSyncInt64Instrument struct { - instrument.Synchronous -} - -var ( - _ syncint64.InstrumentProvider = nonrecordingSyncInt64Instrument{} - _ syncint64.Counter = nonrecordingSyncInt64Instrument{} - _ syncint64.UpDownCounter = nonrecordingSyncInt64Instrument{} - _ syncint64.Histogram = nonrecordingSyncInt64Instrument{} -) - -func (n nonrecordingSyncInt64Instrument) Counter(name string, opts ...instrument.Option) (syncint64.Counter, error) { - metrics = append(metrics, Metric{Name: strings.ReplaceAll(name, ".", "_"), Type: "COUNTER", Help: instrument.NewConfig(opts...).Description()}) - return n, nil -} - -func (n nonrecordingSyncInt64Instrument) UpDownCounter(name string, opts ...instrument.Option) (syncint64.UpDownCounter, error) { - metrics = append(metrics, Metric{Name: strings.ReplaceAll(name, ".", "_"), Type: "UPDOWNCOUNTER", Help: instrument.NewConfig(opts...).Description()}) - return n, nil -} - -func (n nonrecordingSyncInt64Instrument) Histogram(name string, opts ...instrument.Option) (syncint64.Histogram, error) { - metrics = append(metrics, Metric{Name: strings.ReplaceAll(name, ".", "_"), Type: "HISTOGRAM", Help: instrument.NewConfig(opts...).Description()}) - return n, nil -} - -func (nonrecordingSyncInt64Instrument) Add(context.Context, int64, ...attribute.KeyValue) { -} -func (nonrecordingSyncInt64Instrument) Record(context.Context, int64, ...attribute.KeyValue) { -} diff --git a/telemetry/nonrecording/meter.go b/telemetry/nonrecording/meter.go deleted file mode 100644 index 15c9582..0000000 --- a/telemetry/nonrecording/meter.go +++ /dev/null @@ -1,50 +0,0 @@ -package nonrecording - -import ( - "context" - - "go.opentelemetry.io/otel/metric" - "go.opentelemetry.io/otel/metric/instrument" - "go.opentelemetry.io/otel/metric/instrument/asyncfloat64" - "go.opentelemetry.io/otel/metric/instrument/asyncint64" - "go.opentelemetry.io/otel/metric/instrument/syncfloat64" - "go.opentelemetry.io/otel/metric/instrument/syncint64" -) - -// NewNoopMeterProvider creates a MeterProvider that does not record any metrics. -func NewNoopMeterProvider() metric.MeterProvider { - return noopMeterProvider{} -} - -type noopMeterProvider struct{} - -var _ metric.MeterProvider = noopMeterProvider{} - -func (noopMeterProvider) Meter(instrumentationName string, opts ...metric.MeterOption) metric.Meter { - return noopMeter{} -} - -// NewNoopMeter creates a Meter that does not record any metrics. -func NewNoopMeter() metric.Meter { - return noopMeter{} -} - -type noopMeter struct{} - -var _ metric.Meter = noopMeter{} - -func (noopMeter) AsyncInt64() asyncint64.InstrumentProvider { - return nonrecordingAsyncInt64Instrument{} -} -func (noopMeter) AsyncFloat64() asyncfloat64.InstrumentProvider { - return nonrecordingAsyncFloat64Instrument{} -} -func (noopMeter) SyncInt64() syncint64.InstrumentProvider { - return nonrecordingSyncInt64Instrument{} -} -func (noopMeter) SyncFloat64() syncfloat64.InstrumentProvider { - return nonrecordingSyncFloat64Instrument{} -} -func (noopMeter) RegisterCallback([]instrument.Asynchronous, func(context.Context)) error { - return nil -}