mirror of
https://github.com/hanzoai/tasks.git
synced 2026-10-04 02:04:03 +00:00
## What changed? <!-- Describe what has changed in this PR --> - Add backward compatibility configs for tally to otel migration ## Why? <!-- Tell your future self why have you made these changes --> - There are some breaking changes when switching from tally to otel metric framework. 1. Unit name will be appended to the metric name 2. `_total` will be appended to counter metrics 3. Timer metric will be emitted as milliseconds instead of seconds This PR adds configs for controlling both behaviors when using otel framework so that the behavior will be the same as tally. ## How did you test it? <!-- How have you verified this change? Tested locally? Added a unit test? Checked in staging env? --> - Unit tests - Tested locally with various combination of configurations. ## Potential risks <!-- Assuming the worst case, what can be broken when deploying this change to production? --> - No risk by default. If new options are enabled, timer metric value may be wrong or not bucketed correctly. ## Documentation <!-- Have you made sure this change doesn't falsify anything currently stated in `docs/`? If significant new behavior is added, have you described that in `docs/`? --> ## Is hotfix candidate? <!-- Is this PR a hotfix candidate or does it require a notification to be sent to the broader community? (Yes/No) --> - No.
274 lines
9.2 KiB
Go
274 lines
9.2 KiB
Go
// The MIT License
|
|
//
|
|
// Copyright (c) 2020 Temporal Technologies Inc. All rights reserved.
|
|
//
|
|
// Copyright (c) 2020 Uber Technologies, Inc.
|
|
//
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
// in the Software without restriction, including without limitation the rights
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
// furnished to do so, subject to the following conditions:
|
|
//
|
|
// The above copyright notice and this permission notice shall be included in
|
|
// all copies or substantial portions of the Software.
|
|
//
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
// THE SOFTWARE.
|
|
|
|
package metrics
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"sync"
|
|
"time"
|
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
"go.opentelemetry.io/otel/metric"
|
|
"go.temporal.io/server/common/log"
|
|
"go.temporal.io/server/common/log/tag"
|
|
)
|
|
|
|
// otelMetricsHandler is an adapter around an OpenTelemetry [metric.Meter] that implements the [Handler] interface.
|
|
type (
|
|
otelMetricsHandler struct {
|
|
l log.Logger
|
|
set attribute.Set
|
|
provider OpenTelemetryProvider
|
|
excludeTags map[string]map[string]struct{}
|
|
catalog catalog
|
|
gauges *sync.Map // string -> *gaugeAdapter. note: shared between multiple otelMetricsHandlers
|
|
recordTimerInSeconds bool
|
|
}
|
|
|
|
// This is to work around the lack of synchronous gauge:
|
|
// https://github.com/open-telemetry/opentelemetry-specification/issues/2318
|
|
// Basically, otel gauges only support getting a value with a callback, they can't store a
|
|
// value for us. So we have to store it ourselves and supply it to a callback.
|
|
gaugeAdapter struct {
|
|
lock sync.Mutex
|
|
values map[attribute.Distinct]gaugeValue
|
|
}
|
|
gaugeValue struct {
|
|
value float64
|
|
// In practice, we can use attribute.Set itself as the map key in gaugeAdapter and it
|
|
// works, but according to the API we should use attribute.Distinct. But we can't get a
|
|
// Set back from a Distinct, so we have to store the set here also.
|
|
set attribute.Set
|
|
}
|
|
gaugeAdapterGauge struct {
|
|
omp *otelMetricsHandler
|
|
adapter *gaugeAdapter
|
|
}
|
|
)
|
|
|
|
var _ Handler = (*otelMetricsHandler)(nil)
|
|
|
|
// NewOtelMetricsHandler returns a new Handler that uses the provided OpenTelemetry [metric.Meter] to record metrics.
|
|
// This OTel handler supports metric descriptions for metrics registered with the New*Def functions. However, those
|
|
// functions must be called before this constructor. Otherwise, the descriptions will be empty. This is because the
|
|
// OTel metric descriptions are generated from the globalRegistry. You may also record metrics that are not registered
|
|
// via the New*Def functions. In that case, the metric description will be the OTel default (the metric name itself).
|
|
func NewOtelMetricsHandler(
|
|
l log.Logger,
|
|
o OpenTelemetryProvider,
|
|
cfg ClientConfig,
|
|
) (*otelMetricsHandler, error) {
|
|
c, err := globalRegistry.buildCatalog()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to build metrics catalog: %w", err)
|
|
}
|
|
|
|
return &otelMetricsHandler{
|
|
l: l,
|
|
set: makeInitialSet(cfg.Tags),
|
|
provider: o,
|
|
excludeTags: configExcludeTags(cfg),
|
|
catalog: c,
|
|
gauges: new(sync.Map),
|
|
recordTimerInSeconds: cfg.RecordTimerInSeconds,
|
|
}, nil
|
|
}
|
|
|
|
// WithTags creates a new Handler with the provided Tag list.
|
|
// Tags are merged with the existing tags.
|
|
func (omp *otelMetricsHandler) WithTags(tags ...Tag) Handler {
|
|
newHandler := *omp
|
|
newHandler.set = newHandler.makeSet(tags)
|
|
return &newHandler
|
|
}
|
|
|
|
// Counter obtains a counter for the given name.
|
|
func (omp *otelMetricsHandler) Counter(counter string) CounterIface {
|
|
opts := addOptions(omp, counterOptions{}, counter)
|
|
c, err := omp.provider.GetMeter().Int64Counter(counter, opts...)
|
|
if err != nil {
|
|
omp.l.Error("error getting metric", tag.NewStringTag("MetricName", counter), tag.Error(err))
|
|
return CounterFunc(func(i int64, t ...Tag) {})
|
|
}
|
|
|
|
return CounterFunc(func(i int64, t ...Tag) {
|
|
option := metric.WithAttributeSet(omp.makeSet(t))
|
|
c.Add(context.Background(), i, option)
|
|
})
|
|
}
|
|
|
|
func (omp *otelMetricsHandler) getGaugeAdapter(gauge string) (*gaugeAdapter, error) {
|
|
if v, ok := omp.gauges.Load(gauge); ok {
|
|
return v.(*gaugeAdapter), nil
|
|
}
|
|
adapter := &gaugeAdapter{
|
|
values: make(map[attribute.Distinct]gaugeValue),
|
|
}
|
|
if v, wasLoaded := omp.gauges.LoadOrStore(gauge, adapter); wasLoaded {
|
|
return v.(*gaugeAdapter), nil
|
|
}
|
|
|
|
opts := addOptions(omp, gaugeOptions{
|
|
metric.WithFloat64Callback(adapter.callback),
|
|
}, gauge)
|
|
// Register the gauge with otel. It will call our callback when it wants to read the values.
|
|
_, err := omp.provider.GetMeter().Float64ObservableGauge(gauge, opts...)
|
|
if err != nil {
|
|
omp.gauges.Delete(gauge)
|
|
omp.l.Error("error getting metric", tag.NewStringTag("MetricName", gauge), tag.Error(err))
|
|
return nil, err
|
|
}
|
|
|
|
return adapter, nil
|
|
}
|
|
|
|
// Gauge obtains a gauge for the given name.
|
|
func (omp *otelMetricsHandler) Gauge(gauge string) GaugeIface {
|
|
adapter, err := omp.getGaugeAdapter(gauge)
|
|
if err != nil {
|
|
return GaugeFunc(func(i float64, t ...Tag) {})
|
|
}
|
|
return &gaugeAdapterGauge{
|
|
omp: omp,
|
|
adapter: adapter,
|
|
}
|
|
}
|
|
|
|
func (a *gaugeAdapter) callback(ctx context.Context, o metric.Float64Observer) error {
|
|
a.lock.Lock()
|
|
defer a.lock.Unlock()
|
|
for _, v := range a.values {
|
|
o.Observe(v.value, metric.WithAttributeSet(v.set))
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (g *gaugeAdapterGauge) Record(v float64, tags ...Tag) {
|
|
set := g.omp.makeSet(tags)
|
|
g.adapter.lock.Lock()
|
|
defer g.adapter.lock.Unlock()
|
|
g.adapter.values[set.Equivalent()] = gaugeValue{value: v, set: set}
|
|
}
|
|
|
|
// Timer obtains a timer for the given name.
|
|
func (omp *otelMetricsHandler) Timer(timer string) TimerIface {
|
|
if omp.recordTimerInSeconds {
|
|
return omp.timerInSeconds(timer)
|
|
}
|
|
return omp.timerInMilliseconds(timer)
|
|
}
|
|
|
|
func (omp *otelMetricsHandler) timerInMilliseconds(timer string) TimerIface {
|
|
opts := addOptions(omp, int64HistogramOptions{metric.WithUnit(Milliseconds)}, timer)
|
|
c, err := omp.provider.GetMeter().Int64Histogram(timer, opts...)
|
|
if err != nil {
|
|
omp.l.Error("error getting metric", tag.NewStringTag("MetricName", timer), tag.Error(err))
|
|
return TimerFunc(func(i time.Duration, t ...Tag) {})
|
|
}
|
|
|
|
return TimerFunc(func(i time.Duration, t ...Tag) {
|
|
option := metric.WithAttributeSet(omp.makeSet(t))
|
|
c.Record(context.Background(), i.Milliseconds(), option)
|
|
})
|
|
}
|
|
|
|
func (omp *otelMetricsHandler) timerInSeconds(timer string) TimerIface {
|
|
opts := addOptions(omp, float64HistogramOptions{metric.WithUnit(Seconds)}, timer)
|
|
c, err := omp.provider.GetMeter().Float64Histogram(timer, opts...)
|
|
if err != nil {
|
|
omp.l.Error("error getting metric", tag.NewStringTag("MetricName", timer), tag.Error(err))
|
|
return TimerFunc(func(i time.Duration, t ...Tag) {})
|
|
}
|
|
|
|
return TimerFunc(func(i time.Duration, t ...Tag) {
|
|
option := metric.WithAttributeSet(omp.makeSet(t))
|
|
c.Record(context.Background(), i.Seconds(), option)
|
|
})
|
|
}
|
|
|
|
// Histogram obtains a histogram for the given name.
|
|
func (omp *otelMetricsHandler) Histogram(histogram string, unit MetricUnit) HistogramIface {
|
|
opts := addOptions(omp, int64HistogramOptions{metric.WithUnit(string(unit))}, histogram)
|
|
c, err := omp.provider.GetMeter().Int64Histogram(histogram, opts...)
|
|
if err != nil {
|
|
omp.l.Error("error getting metric", tag.NewStringTag("MetricName", histogram), tag.Error(err))
|
|
return HistogramFunc(func(i int64, t ...Tag) {})
|
|
}
|
|
|
|
return HistogramFunc(func(i int64, t ...Tag) {
|
|
option := metric.WithAttributeSet(omp.makeSet(t))
|
|
c.Record(context.Background(), i, option)
|
|
})
|
|
}
|
|
|
|
func (omp *otelMetricsHandler) Stop(l log.Logger) {
|
|
omp.provider.Stop(l)
|
|
}
|
|
|
|
func (omp *otelMetricsHandler) Close() error {
|
|
return nil
|
|
}
|
|
|
|
func (omp *otelMetricsHandler) StartBatch(_ string) BatchHandler {
|
|
return omp
|
|
}
|
|
|
|
// makeSet returns an otel attribute.Set with the given tags merged with the
|
|
// otelMetricsHandler's tags.
|
|
func (omp *otelMetricsHandler) makeSet(tags []Tag) attribute.Set {
|
|
if len(tags) == 0 {
|
|
return omp.set
|
|
}
|
|
attrs := make([]attribute.KeyValue, 0, omp.set.Len()+len(tags))
|
|
for i := omp.set.Iter(); i.Next(); {
|
|
attrs = append(attrs, i.Attribute())
|
|
}
|
|
for _, t := range tags {
|
|
attrs = append(attrs, omp.convertTag(t))
|
|
}
|
|
return attribute.NewSet(attrs...)
|
|
}
|
|
|
|
func (omp *otelMetricsHandler) convertTag(tag Tag) attribute.KeyValue {
|
|
if vals, ok := omp.excludeTags[tag.Key()]; ok {
|
|
if _, ok := vals[tag.Value()]; !ok {
|
|
return attribute.String(tag.Key(), tagExcludedValue)
|
|
}
|
|
}
|
|
return attribute.String(tag.Key(), tag.Value())
|
|
}
|
|
|
|
func makeInitialSet(tags map[string]string) attribute.Set {
|
|
if len(tags) == 0 {
|
|
return *attribute.EmptySet()
|
|
}
|
|
var attrs []attribute.KeyValue
|
|
for k, v := range tags {
|
|
attrs = append(attrs, attribute.String(k, v))
|
|
}
|
|
return attribute.NewSet(attrs...)
|
|
}
|