2020-03-24 07:41:10 +02:00
|
|
|
// Copyright The OpenTelemetry Authors
|
2019-11-26 21:47:15 +02:00
|
|
|
//
|
|
|
|
// 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"
|
2020-07-07 21:52:35 +02:00
|
|
|
"fmt"
|
2019-11-26 21:47:15 +02:00
|
|
|
"log"
|
|
|
|
"net/http"
|
2020-03-11 01:00:37 +02:00
|
|
|
"sync"
|
2019-11-26 21:47:15 +02:00
|
|
|
"time"
|
|
|
|
|
2021-02-18 19:59:37 +02:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
2021-06-12 20:39:22 +02:00
|
|
|
"go.opentelemetry.io/otel/exporters/prometheus"
|
2020-11-12 17:28:32 +02:00
|
|
|
"go.opentelemetry.io/otel/metric"
|
2022-03-02 17:50:29 +02:00
|
|
|
"go.opentelemetry.io/otel/metric/instrument"
|
2021-06-10 18:22:47 +02:00
|
|
|
"go.opentelemetry.io/otel/sdk/metric/aggregator/histogram"
|
|
|
|
controller "go.opentelemetry.io/otel/sdk/metric/controller/basic"
|
2021-12-13 22:13:03 +02:00
|
|
|
"go.opentelemetry.io/otel/sdk/metric/export/aggregation"
|
2021-06-10 18:22:47 +02:00
|
|
|
processor "go.opentelemetry.io/otel/sdk/metric/processor/basic"
|
|
|
|
selector "go.opentelemetry.io/otel/sdk/metric/selector/simple"
|
2019-11-26 21:47:15 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2021-02-18 19:59:37 +02:00
|
|
|
lemonsKey = attribute.Key("ex.com/lemons")
|
2022-03-02 17:50:29 +02:00
|
|
|
|
|
|
|
// TODO Bring back Global package
|
|
|
|
meterProvider metric.MeterProvider
|
2019-11-26 21:47:15 +02:00
|
|
|
)
|
|
|
|
|
2020-05-20 19:27:26 +02:00
|
|
|
func initMeter() {
|
2022-01-04 20:33:18 +02:00
|
|
|
config := prometheus.Config{
|
|
|
|
DefaultHistogramBoundaries: []float64{1, 2, 5, 10, 20, 50},
|
|
|
|
}
|
2021-06-10 18:22:47 +02:00
|
|
|
c := controller.New(
|
2021-09-27 17:51:47 +02:00
|
|
|
processor.NewFactory(
|
2021-06-10 18:22:47 +02:00
|
|
|
selector.NewWithHistogramDistribution(
|
|
|
|
histogram.WithExplicitBoundaries(config.DefaultHistogramBoundaries),
|
|
|
|
),
|
2021-10-15 20:18:36 +02:00
|
|
|
aggregation.CumulativeTemporalitySelector(),
|
2021-06-10 18:22:47 +02:00
|
|
|
processor.WithMemory(true),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
exporter, err := prometheus.New(config, c)
|
2019-11-26 21:47:15 +02:00
|
|
|
if err != nil {
|
2020-01-02 20:41:21 +02:00
|
|
|
log.Panicf("failed to initialize prometheus exporter %v", err)
|
2019-11-26 21:47:15 +02:00
|
|
|
}
|
2022-03-02 17:50:29 +02:00
|
|
|
// TODO Bring back Global package
|
|
|
|
// global.SetMeterProvider(exporter.MeterProvider())
|
|
|
|
meterProvider = exporter.MeterProvider()
|
2021-06-10 18:22:47 +02:00
|
|
|
|
2020-05-19 03:37:41 +02:00
|
|
|
http.HandleFunc("/", exporter.ServeHTTP)
|
2019-11-26 21:47:15 +02:00
|
|
|
go func() {
|
2020-01-02 20:41:21 +02:00
|
|
|
_ = http.ListenAndServe(":2222", nil)
|
2019-11-26 21:47:15 +02:00
|
|
|
}()
|
2020-07-07 21:52:35 +02:00
|
|
|
|
|
|
|
fmt.Println("Prometheus server running on :2222")
|
2019-11-26 21:47:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2020-05-20 19:27:26 +02:00
|
|
|
initMeter()
|
2019-11-26 21:47:15 +02:00
|
|
|
|
2022-03-02 17:50:29 +02:00
|
|
|
// TODO Bring back Global package
|
|
|
|
// meter := global.Meter("ex.com/basic")
|
|
|
|
meter := meterProvider.Meter("ex.com/basic")
|
2020-03-11 01:00:37 +02:00
|
|
|
observerLock := new(sync.RWMutex)
|
|
|
|
observerValueToReport := new(float64)
|
2021-02-18 19:59:37 +02:00
|
|
|
observerLabelsToReport := new([]attribute.KeyValue)
|
2022-03-02 17:50:29 +02:00
|
|
|
|
|
|
|
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) {
|
2020-03-11 01:00:37 +02:00
|
|
|
(*observerLock).RLock()
|
|
|
|
value := *observerValueToReport
|
2020-03-27 23:06:48 +02:00
|
|
|
labels := *observerLabelsToReport
|
2020-03-11 01:00:37 +02:00
|
|
|
(*observerLock).RUnlock()
|
2022-03-02 17:50:29 +02:00
|
|
|
gaugeObserver.Observe(ctx, value, labels...)
|
|
|
|
})
|
2019-11-26 21:47:15 +02:00
|
|
|
|
2022-03-02 17:50:29 +02:00
|
|
|
histogram, 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)
|
|
|
|
}
|
2019-11-26 21:47:15 +02:00
|
|
|
|
2021-02-18 19:59:37 +02:00
|
|
|
commonLabels := []attribute.KeyValue{lemonsKey.Int(10), attribute.String("A", "1"), attribute.String("B", "2"), attribute.String("C", "3")}
|
|
|
|
notSoCommonLabels := []attribute.KeyValue{lemonsKey.Int(13)}
|
2019-11-26 21:47:15 +02:00
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2020-03-11 01:00:37 +02:00
|
|
|
(*observerLock).Lock()
|
|
|
|
*observerValueToReport = 1.0
|
2020-03-27 23:06:48 +02:00
|
|
|
*observerLabelsToReport = commonLabels
|
2020-03-11 01:00:37 +02:00
|
|
|
(*observerLock).Unlock()
|
2022-03-02 17:50:29 +02:00
|
|
|
|
|
|
|
histogram.Record(ctx, 2.0, commonLabels...)
|
|
|
|
counter.Add(ctx, 12.0, commonLabels...)
|
2019-11-26 21:47:15 +02:00
|
|
|
|
2020-03-11 01:00:37 +02:00
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
|
|
|
|
(*observerLock).Lock()
|
|
|
|
*observerValueToReport = 1.0
|
2020-03-27 23:06:48 +02:00
|
|
|
*observerLabelsToReport = notSoCommonLabels
|
2020-03-11 01:00:37 +02:00
|
|
|
(*observerLock).Unlock()
|
2022-03-02 17:50:29 +02:00
|
|
|
histogram.Record(ctx, 2.0, notSoCommonLabels...)
|
|
|
|
counter.Add(ctx, 22.0, notSoCommonLabels...)
|
2019-11-26 21:47:15 +02:00
|
|
|
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
|
2020-03-11 01:00:37 +02:00
|
|
|
(*observerLock).Lock()
|
|
|
|
*observerValueToReport = 13.0
|
2020-03-27 23:06:48 +02:00
|
|
|
*observerLabelsToReport = commonLabels
|
2020-03-11 01:00:37 +02:00
|
|
|
(*observerLock).Unlock()
|
2022-03-02 17:50:29 +02:00
|
|
|
histogram.Record(ctx, 12.0, commonLabels...)
|
|
|
|
counter.Add(ctx, 13.0, commonLabels...)
|
2019-11-26 21:47:15 +02:00
|
|
|
|
2020-07-07 21:52:35 +02:00
|
|
|
fmt.Println("Example finished updating, please visit :2222")
|
|
|
|
|
|
|
|
select {}
|
2019-11-26 21:47:15 +02:00
|
|
|
}
|