mirror of
https://github.com/open-telemetry/opentelemetry-go.git
synced 2024-12-14 10:13:10 +02:00
1f5b159161
* Refactor golangci-lint conf Order settings alphabetically. * Add revive settings to golangci conf * Check blank imports * Check bool-literal-in-expr * Check constant-logical-expr * Check context-as-argument * Check context-key-type * Check deep-exit * Check defer * Check dot-imports * Check duplicated-imports * Check early-return * Check empty-block * Check empty-lines * Check error-naming * Check error-return * Check error-strings * Check errorf * Stop ignoring context first arg in tests * Check exported comments * Check flag-parameter * Check identical branches * Check if-return * Check increment-decrement * Check indent-error-flow * Check deny list of go imports * Check import shadowing * Check package comments * Check range * Check range val in closure * Check range val address * Check redefines builtin id * Check string-format * Check struct tag * Check superfluous else * Check time equal * Check var naming * Check var declaration * Check unconditional recursion * Check unexported return * Check unhandled errors * Check unnecessary stmt * Check unnecessary break * Check waitgroup by value * Exclude deep-exit check in example*_test.go files
139 lines
3.8 KiB
Go
139 lines
3.8 KiB
Go
// 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 main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"sync"
|
|
"time"
|
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
"go.opentelemetry.io/otel/exporters/prometheus"
|
|
"go.opentelemetry.io/otel/metric/global"
|
|
"go.opentelemetry.io/otel/metric/instrument"
|
|
"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"
|
|
)
|
|
|
|
var (
|
|
lemonsKey = attribute.Key("ex.com/lemons")
|
|
)
|
|
|
|
func initMeter() error {
|
|
config := prometheus.Config{
|
|
DefaultHistogramBoundaries: []float64{1, 2, 5, 10, 20, 50},
|
|
}
|
|
c := controller.New(
|
|
processor.NewFactory(
|
|
selector.NewWithHistogramDistribution(
|
|
histogram.WithExplicitBoundaries(config.DefaultHistogramBoundaries),
|
|
),
|
|
aggregation.CumulativeTemporalitySelector(),
|
|
processor.WithMemory(true),
|
|
),
|
|
)
|
|
exporter, err := prometheus.New(config, c)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to initialize prometheus exporter: %w", err)
|
|
}
|
|
|
|
global.SetMeterProvider(exporter.MeterProvider())
|
|
|
|
http.HandleFunc("/", exporter.ServeHTTP)
|
|
go func() {
|
|
_ = http.ListenAndServe(":2222", nil)
|
|
}()
|
|
|
|
fmt.Println("Prometheus server running on :2222")
|
|
return nil
|
|
}
|
|
|
|
func main() {
|
|
if err := initMeter(); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
meter := global.Meter("ex.com/basic")
|
|
|
|
observerLock := new(sync.RWMutex)
|
|
observerValueToReport := new(float64)
|
|
observerAttrsToReport := new([]attribute.KeyValue)
|
|
|
|
gaugeObserver, err := meter.AsyncFloat64().Gauge("ex.com.one")
|
|
if err != nil {
|
|
log.Panicf("failed to initialize instrument: %v", err)
|
|
}
|
|
_ = meter.RegisterCallback([]instrument.Asynchronous{gaugeObserver}, func(ctx context.Context) {
|
|
(*observerLock).RLock()
|
|
value := *observerValueToReport
|
|
attrs := *observerAttrsToReport
|
|
(*observerLock).RUnlock()
|
|
gaugeObserver.Observe(ctx, value, attrs...)
|
|
})
|
|
|
|
hist, err := meter.SyncFloat64().Histogram("ex.com.two")
|
|
if err != nil {
|
|
log.Panicf("failed to initialize instrument: %v", err)
|
|
}
|
|
counter, err := meter.SyncFloat64().Counter("ex.com.three")
|
|
if err != nil {
|
|
log.Panicf("failed to initialize instrument: %v", err)
|
|
}
|
|
|
|
commonAttrs := []attribute.KeyValue{lemonsKey.Int(10), attribute.String("A", "1"), attribute.String("B", "2"), attribute.String("C", "3")}
|
|
notSoCommonAttrs := []attribute.KeyValue{lemonsKey.Int(13)}
|
|
|
|
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
|
|
defer stop()
|
|
|
|
(*observerLock).Lock()
|
|
*observerValueToReport = 1.0
|
|
*observerAttrsToReport = commonAttrs
|
|
(*observerLock).Unlock()
|
|
|
|
hist.Record(ctx, 2.0, commonAttrs...)
|
|
counter.Add(ctx, 12.0, commonAttrs...)
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
(*observerLock).Lock()
|
|
*observerValueToReport = 1.0
|
|
*observerAttrsToReport = notSoCommonAttrs
|
|
(*observerLock).Unlock()
|
|
hist.Record(ctx, 2.0, notSoCommonAttrs...)
|
|
counter.Add(ctx, 22.0, notSoCommonAttrs...)
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
(*observerLock).Lock()
|
|
*observerValueToReport = 13.0
|
|
*observerAttrsToReport = commonAttrs
|
|
(*observerLock).Unlock()
|
|
hist.Record(ctx, 12.0, commonAttrs...)
|
|
counter.Add(ctx, 13.0, commonAttrs...)
|
|
|
|
fmt.Println("Example finished updating, please visit :2222")
|
|
|
|
<-ctx.Done()
|
|
}
|