-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add open telemetry framework, and using it add 1 new metric (#296)
- Loading branch information
1 parent
6f1cd09
commit 74cd140
Showing
8 changed files
with
215 additions
and
56 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package clusters | ||
|
||
import "github.com/istio-ecosystem/admiral/admiral/pkg/monitoring" | ||
|
||
// List all metrics part of the clusters package here | ||
var ( | ||
configWriterMeter = monitoring.NewMeter("config_writer") | ||
totalConfigWriterEvents = monitoring.NewCounter( | ||
"total_config_write_invocations", | ||
"total number of times config writer was invoked", | ||
monitoring.WithMeter(configWriterMeter)) | ||
) |
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,61 @@ | ||
package monitoring | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"reflect" | ||
|
||
api "go.opentelemetry.io/otel/metric" | ||
) | ||
|
||
// Metric interface for abstracting the different operations | ||
// for the various metric types provided by open telemetry | ||
type Metric interface { | ||
Increment(attributes api.MeasurementOption) | ||
Name() string | ||
} | ||
|
||
// NewCounter returns a new counter | ||
func NewCounter(name, description string, opts ...Options) Metric { | ||
o := createOptions(opts...) | ||
return newFloat64Counter(name, description, o) | ||
} | ||
|
||
type counter struct { | ||
name string | ||
description string | ||
ctx context.Context | ||
int64Counter api.Int64Counter | ||
} | ||
|
||
// Increment increases the value of the counter by 1, and adds the provided attributes | ||
func (c *counter) Increment(attributes api.MeasurementOption) { | ||
c.int64Counter.Add(c.ctx, 1, attributes) | ||
} | ||
|
||
// Name returns the name of the metric | ||
func (c *counter) Name() string { | ||
return c.name | ||
} | ||
|
||
func newFloat64Counter(name, description string, opts *options) *counter { | ||
ctx := context.TODO() | ||
meter := defaultMeter | ||
if reflect.ValueOf(opts.meter).IsValid() { | ||
meter = opts.meter | ||
} | ||
int64Counter, err := meter.Int64Counter( | ||
name, | ||
api.WithUnit("1"), | ||
api.WithDescription(description), | ||
) | ||
if err != nil { | ||
log.Fatalf("error creating int64 counter: %v", err) | ||
} | ||
return &counter{ | ||
name: name, | ||
description: description, | ||
ctx: ctx, | ||
int64Counter: int64Counter, | ||
} | ||
} |
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,47 @@ | ||
package monitoring | ||
|
||
import ( | ||
"go.opentelemetry.io/otel/exporters/prometheus" | ||
api "go.opentelemetry.io/otel/metric" | ||
"go.opentelemetry.io/otel/sdk/metric" | ||
) | ||
|
||
var ( | ||
meterName = "admiral_monitoring" | ||
exporter, _ = prometheus.New() | ||
provider = metric.NewMeterProvider(metric.WithReader(exporter)) | ||
defaultMeter = provider.Meter(meterName) | ||
) | ||
|
||
func InitializeMonitoring() error { | ||
return nil | ||
} | ||
|
||
// Options accepts a pointer to options. It is used | ||
// to update the options by calling an array of functions | ||
type Options func(*options) | ||
|
||
// NewMeter creates a new meter which defines the metric scope | ||
func NewMeter(meterName string) api.Meter { | ||
return provider.Meter(meterName) | ||
} | ||
|
||
// WithMeter configures the given Meter | ||
func WithMeter(meter api.Meter) Options { | ||
return func(opts *options) { | ||
opts.meter = meter | ||
} | ||
} | ||
|
||
type options struct { | ||
meter api.Meter | ||
unit string | ||
} | ||
|
||
func createOptions(opts ...Options) *options { | ||
o := &options{} | ||
for _, opt := range opts { | ||
opt(o) | ||
} | ||
return o | ||
} |
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.