New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add otel metrics for spiderpool #81
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e6558e2
add otel metrics for spiderpool
Icarus9913 3a7b9ad
spiderpool metrics pkg optimization
Icarus9913 895eab6
Merge remote-tracking branch 'upstream/main' into feat/wk/otel-metrics
Icarus9913 0a8b6d0
spiderpool otel metrics fix http server close
Icarus9913 File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
The table of contents is too big for display.
Diff view
Diff view
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
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,97 @@ | ||
// Copyright 2022 Authors of spidernet-io | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package metrics | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"time" | ||
|
||
"go.opentelemetry.io/otel/exporters/prometheus" | ||
"go.opentelemetry.io/otel/metric" | ||
"go.opentelemetry.io/otel/metric/global" | ||
"go.opentelemetry.io/otel/metric/instrument" | ||
"go.opentelemetry.io/otel/metric/instrument/syncfloat64" | ||
"go.opentelemetry.io/otel/metric/instrument/syncint64" | ||
"go.opentelemetry.io/otel/sdk/metric/aggregator/histogram" | ||
controller "go.opentelemetry.io/otel/sdk/metric/controller/basic" | ||
"go.opentelemetry.io/otel/sdk/metric/export/aggregation" | ||
processor "go.opentelemetry.io/otel/sdk/metric/processor/basic" | ||
selector "go.opentelemetry.io/otel/sdk/metric/selector/simple" | ||
) | ||
|
||
// meter is a global creator of metric instruments. | ||
var meter metric.Meter | ||
|
||
// InitMetricController will set up meter with the input param(required) and create a prometheus exporter. | ||
// returns http handler and error | ||
func InitMetricController(meterName string) (http.Handler, error) { | ||
if len(meterName) == 0 { | ||
return nil, fmt.Errorf("Failed to init metric controller, meter name is asked to be set!") | ||
} | ||
|
||
config := prometheus.Config{ | ||
DefaultHistogramBoundaries: []float64{1, 5, 7, 10, 15, 20, 30}, | ||
} | ||
c := controller.New( | ||
processor.NewFactory( | ||
selector.NewWithHistogramDistribution( | ||
histogram.WithExplicitBoundaries(config.DefaultHistogramBoundaries)), | ||
aggregation.CumulativeTemporalitySelector(), | ||
processor.WithMemory(true), | ||
), | ||
) | ||
exporter, err := prometheus.New(config, c) | ||
if nil != err { | ||
return nil, err | ||
} | ||
global.SetMeterProvider(exporter.MeterProvider()) | ||
|
||
m := global.Meter(meterName) | ||
meter = m | ||
|
||
return exporter, nil | ||
} | ||
|
||
// NewMetricInt64Counter will create otel Int64Counter metric. | ||
// The first param metricName is required and the second param is optional. | ||
func NewMetricInt64Counter(metricName string, description string) (syncint64.Counter, error) { | ||
if len(metricName) == 0 { | ||
return nil, fmt.Errorf("Failed to create metric Int64Counter, metric name is asked to be set.") | ||
} | ||
return meter.SyncInt64().Counter(metricName, instrument.WithDescription(description)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 已规避并发问题。syncInt64采用CAS自旋方式来解决并发问题。 |
||
} | ||
|
||
// NewMetricFloat64Histogram will create otel Float64Histogram metric. | ||
// The first param metricName is required and the second param is optional. | ||
func NewMetricFloat64Histogram(metricName string, description string) (syncfloat64.Histogram, error) { | ||
if len(metricName) == 0 { | ||
return nil, fmt.Errorf("Failed to create metric Float64Histogram, metric name is asked to be set.") | ||
} | ||
return meter.SyncFloat64().Histogram(metricName, instrument.WithDescription(description)) | ||
} | ||
|
||
var _ TimeRecorder = &timeRecorder{} | ||
|
||
// timeRecorder owns a field to record start time. | ||
type timeRecorder struct { | ||
startTime time.Time | ||
} | ||
|
||
// TimeRecorder will help you to compute time duration. | ||
type TimeRecorder interface { | ||
SinceInSeconds() float64 | ||
} | ||
|
||
// NewTimeRecorder will create TimeRecorder and record the current time. | ||
func NewTimeRecorder() TimeRecorder { | ||
t := timeRecorder{} | ||
t.startTime = time.Now() | ||
return &t | ||
} | ||
|
||
// SinceInSeconds returns the duration of time since the start time as a float64. | ||
func (t *timeRecorder) SinceInSeconds() float64 { | ||
return float64(time.Since(t.startTime)) / 1e9 | ||
} |
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,68 @@ | ||
// Copyright 2022 Authors of spidernet-io | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package metrics_test | ||
|
||
import ( | ||
"context" | ||
"net" | ||
"net/http" | ||
"testing" | ||
"time" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
|
||
"github.com/spidernet-io/spiderpool/pkg/metrics" | ||
) | ||
|
||
var ( | ||
listener net.Listener | ||
server *http.Server | ||
httpHandle http.Handler | ||
err error | ||
ctx context.Context | ||
) | ||
|
||
func TestMetrics(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Metrics Suite") | ||
} | ||
|
||
// start a http server to set up an otel exporter | ||
var _ = BeforeSuite(func() { | ||
ctx = context.TODO() | ||
|
||
server = new(http.Server) | ||
httpHandle, err = metrics.InitMetricController(SpiderPoolMeter) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
go func() { | ||
defer GinkgoRecover() | ||
|
||
/* Obtain a reference to a local port for the given sock, | ||
* if snum is zero it means select any available local port. | ||
*/ | ||
listener, err = net.Listen("tcp", net.JoinHostPort("0.0.0.0", "0")) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
MetricsTestServerAddr = "http://" + listener.Addr().String() | ||
|
||
server.Handler = httpHandle | ||
server.IdleTimeout = time.Second * 10 | ||
server.ReadTimeout = time.Second * 30 | ||
server.WriteTimeout = time.Second * 60 | ||
|
||
err = server.Serve(listener) | ||
if nil != err && err != http.ErrServerClosed { | ||
By("Error: Otel metrics http server failed. " + err.Error()) | ||
} | ||
|
||
}() | ||
}) | ||
|
||
// close http listener | ||
var _ = AfterSuite(func() { | ||
err = server.Shutdown(ctx) | ||
Expect(err).NotTo(HaveOccurred()) | ||
}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it could be a default logger to use
var loger = log.Stdloger.(" metric")