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"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
2020-03-11 01:00:37 +02:00
|
|
|
"sync"
|
2019-11-26 21:47:15 +02:00
|
|
|
"time"
|
|
|
|
|
2020-03-27 23:06:48 +02:00
|
|
|
"go.opentelemetry.io/otel/api/core"
|
2019-11-26 21:47:15 +02:00
|
|
|
"go.opentelemetry.io/otel/api/global"
|
|
|
|
"go.opentelemetry.io/otel/api/key"
|
|
|
|
"go.opentelemetry.io/otel/api/metric"
|
2020-03-02 23:54:57 +02:00
|
|
|
"go.opentelemetry.io/otel/exporters/metric/prometheus"
|
2019-11-26 21:47:15 +02:00
|
|
|
"go.opentelemetry.io/otel/sdk/metric/controller/push"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
lemonsKey = key.New("ex.com/lemons")
|
|
|
|
)
|
|
|
|
|
|
|
|
func initMeter() *push.Controller {
|
2020-01-03 19:48:45 +02:00
|
|
|
pusher, hf, err := prometheus.InstallNewPipeline(prometheus.Config{})
|
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
|
|
|
}
|
2020-01-02 20:41:21 +02:00
|
|
|
http.HandleFunc("/", hf)
|
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
|
|
|
}()
|
|
|
|
|
|
|
|
return pusher
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
defer initMeter().Stop()
|
|
|
|
|
2020-03-11 17:23:32 +02:00
|
|
|
meter := global.Meter("ex.com/basic")
|
2020-03-11 01:00:37 +02:00
|
|
|
observerLock := new(sync.RWMutex)
|
|
|
|
observerValueToReport := new(float64)
|
2020-03-27 23:06:48 +02:00
|
|
|
observerLabelsToReport := new([]core.KeyValue)
|
2020-03-11 01:00:37 +02:00
|
|
|
cb := func(result metric.Float64ObserverResult) {
|
|
|
|
(*observerLock).RLock()
|
|
|
|
value := *observerValueToReport
|
2020-03-27 23:06:48 +02:00
|
|
|
labels := *observerLabelsToReport
|
2020-03-11 01:00:37 +02:00
|
|
|
(*observerLock).RUnlock()
|
2020-03-27 23:06:48 +02:00
|
|
|
result.Observe(value, labels...)
|
2020-03-11 01:00:37 +02:00
|
|
|
}
|
2020-03-19 21:02:46 +02:00
|
|
|
_ = metric.Must(meter).RegisterFloat64Observer("ex.com.one", cb,
|
2020-03-11 01:00:37 +02:00
|
|
|
metric.WithDescription("A measure set to 1.0"),
|
2019-11-26 21:47:15 +02:00
|
|
|
)
|
|
|
|
|
2020-04-15 01:07:11 +02:00
|
|
|
measureTwo := metric.Must(meter).NewFloat64Measure("ex.com.two")
|
2020-03-11 20:57:57 +02:00
|
|
|
measureThree := metric.Must(meter).NewFloat64Counter("ex.com.three")
|
2019-11-26 21:47:15 +02:00
|
|
|
|
2020-03-27 23:06:48 +02:00
|
|
|
commonLabels := []core.KeyValue{lemonsKey.Int(10), key.String("A", "1"), key.String("B", "2"), key.String("C", "3")}
|
|
|
|
notSoCommonLabels := []core.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()
|
2019-11-26 21:47:15 +02:00
|
|
|
meter.RecordBatch(
|
|
|
|
ctx,
|
|
|
|
commonLabels,
|
|
|
|
measureTwo.Measurement(2.0),
|
|
|
|
measureThree.Measurement(12.0),
|
|
|
|
)
|
|
|
|
|
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()
|
2019-11-26 21:47:15 +02:00
|
|
|
meter.RecordBatch(
|
|
|
|
ctx,
|
|
|
|
notSoCommonLabels,
|
|
|
|
measureTwo.Measurement(2.0),
|
|
|
|
measureThree.Measurement(22.0),
|
|
|
|
)
|
|
|
|
|
|
|
|
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()
|
2019-11-26 21:47:15 +02:00
|
|
|
meter.RecordBatch(
|
|
|
|
ctx,
|
|
|
|
commonLabels,
|
|
|
|
measureTwo.Measurement(12.0),
|
|
|
|
measureThree.Measurement(13.0),
|
|
|
|
)
|
|
|
|
|
|
|
|
time.Sleep(100 * time.Second)
|
|
|
|
}
|