mirror of
https://github.com/hanzoai/tasks.git
synced 2026-10-04 02:04:03 +00:00
<!-- Describe what has changed in this PR --> **What changed?** WISOTT <!-- Tell your future self why have you made these changes --> **Why?** Fixes https://github.com/temporalio/temporal/issues/4996 <!-- How have you verified this change? Tested locally? Added a unit test? Checked in staging env? --> **How did you test it?** Automated tests (which actually include metric tests!) <!-- Assuming the worst case, what can be broken when deploying this change to production? --> **Potential risks** Metrics are not reported / incorrectly reported? <!-- Is this PR a hotfix candidate or require that a notification be sent to the broader community? (Yes/No) --> **Is hotfix candidate?** No.
133 lines
4.1 KiB
Go
133 lines
4.1 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"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
|
exporters "go.opentelemetry.io/otel/exporters/prometheus"
|
|
"go.opentelemetry.io/otel/metric"
|
|
sdkmetrics "go.opentelemetry.io/otel/sdk/metric"
|
|
|
|
"go.temporal.io/server/common/log"
|
|
"go.temporal.io/server/common/log/tag"
|
|
)
|
|
|
|
var _ OpenTelemetryProvider = (*openTelemetryProviderImpl)(nil)
|
|
|
|
type (
|
|
OpenTelemetryProvider interface {
|
|
Stop(logger log.Logger)
|
|
GetMeter() metric.Meter
|
|
}
|
|
|
|
openTelemetryProviderImpl struct {
|
|
meter metric.Meter
|
|
config *PrometheusConfig
|
|
server *http.Server
|
|
}
|
|
)
|
|
|
|
func NewOpenTelemetryProvider(
|
|
logger log.Logger,
|
|
prometheusConfig *PrometheusConfig,
|
|
clientConfig *ClientConfig,
|
|
) (*openTelemetryProviderImpl, error) {
|
|
reg := prometheus.NewRegistry()
|
|
exporter, err := exporters.New(exporters.WithRegisterer(reg))
|
|
if err != nil {
|
|
logger.Error("Failed to initialize prometheus exporter.", tag.Error(err))
|
|
return nil, err
|
|
}
|
|
|
|
var views []sdkmetrics.View
|
|
for _, u := range []string{Dimensionless, Bytes, Milliseconds} {
|
|
views = append(views, sdkmetrics.NewView(
|
|
sdkmetrics.Instrument{
|
|
Kind: sdkmetrics.InstrumentKindHistogram,
|
|
Unit: u,
|
|
},
|
|
sdkmetrics.Stream{
|
|
Aggregation: sdkmetrics.AggregationExplicitBucketHistogram{
|
|
Boundaries: clientConfig.PerUnitHistogramBoundaries[u],
|
|
},
|
|
},
|
|
))
|
|
}
|
|
provider := sdkmetrics.NewMeterProvider(
|
|
sdkmetrics.WithReader(exporter),
|
|
sdkmetrics.WithView(views...),
|
|
)
|
|
metricServer := initPrometheusListener(prometheusConfig, reg, logger)
|
|
meter := provider.Meter("temporal")
|
|
reporter := &openTelemetryProviderImpl{
|
|
meter: meter,
|
|
config: prometheusConfig,
|
|
server: metricServer,
|
|
}
|
|
|
|
return reporter, nil
|
|
}
|
|
|
|
func initPrometheusListener(config *PrometheusConfig, reg *prometheus.Registry, logger log.Logger) *http.Server {
|
|
handlerPath := config.HandlerPath
|
|
if handlerPath == "" {
|
|
handlerPath = "/metrics"
|
|
}
|
|
|
|
handler := http.NewServeMux()
|
|
handler.HandleFunc(handlerPath, promhttp.HandlerFor(reg, promhttp.HandlerOpts{Registry: reg}).ServeHTTP)
|
|
|
|
if config.ListenAddress == "" {
|
|
logger.Fatal("Listen address must be specified.", tag.Address(config.ListenAddress))
|
|
}
|
|
server := &http.Server{Addr: config.ListenAddress, Handler: handler}
|
|
|
|
go func() {
|
|
err := server.ListenAndServe()
|
|
if err != http.ErrServerClosed {
|
|
logger.Fatal("Failed to initialize prometheus listener.", tag.Address(config.ListenAddress))
|
|
}
|
|
}()
|
|
|
|
return server
|
|
}
|
|
|
|
func (r *openTelemetryProviderImpl) GetMeter() metric.Meter {
|
|
return r.meter
|
|
}
|
|
|
|
func (r *openTelemetryProviderImpl) Stop(logger log.Logger) {
|
|
ctx, closeCtx := context.WithTimeout(context.Background(), time.Second)
|
|
defer closeCtx()
|
|
if err := r.server.Shutdown(ctx); !(err == nil || err == http.ErrServerClosed) {
|
|
logger.Error("Prometheus metrics server shutdown failure.", tag.Address(r.config.ListenAddress), tag.Error(err))
|
|
}
|
|
}
|