2020-03-23 22:41:10 -07:00
|
|
|
// Copyright The OpenTelemetry Authors
|
2019-11-26 15:47:15 -04: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-10 16:00:37 -07:00
|
|
|
"sync"
|
2019-11-26 15:47:15 -04:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"go.opentelemetry.io/otel/api/global"
|
2020-05-14 07:06:03 +08:00
|
|
|
"go.opentelemetry.io/otel/api/kv"
|
2019-11-26 15:47:15 -04:00
|
|
|
"go.opentelemetry.io/otel/api/metric"
|
2020-03-02 13:54:57 -08:00
|
|
|
"go.opentelemetry.io/otel/exporters/metric/prometheus"
|
2019-11-26 15:47:15 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-05-13 16:21:23 -07:00
|
|
|
lemonsKey = kv.Key("ex.com/lemons")
|
2019-11-26 15:47:15 -04:00
|
|
|
)
|
|
|
|
|
2020-05-20 10:27:26 -07:00
|
|
|
func initMeter() {
|
|
|
|
exporter, err := prometheus.InstallNewPipeline(prometheus.Config{})
|
2019-11-26 15:47:15 -04:00
|
|
|
if err != nil {
|
2020-01-02 19:41:21 +01:00
|
|
|
log.Panicf("failed to initialize prometheus exporter %v", err)
|
2019-11-26 15:47:15 -04:00
|
|
|
}
|
2020-05-18 18:37:41 -07:00
|
|
|
http.HandleFunc("/", exporter.ServeHTTP)
|
2019-11-26 15:47:15 -04:00
|
|
|
go func() {
|
2020-01-02 19:41:21 +01:00
|
|
|
_ = http.ListenAndServe(":2222", nil)
|
2019-11-26 15:47:15 -04:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2020-05-20 10:27:26 -07:00
|
|
|
initMeter()
|
2019-11-26 15:47:15 -04:00
|
|
|
|
2020-03-11 12:23:32 -03:00
|
|
|
meter := global.Meter("ex.com/basic")
|
2020-03-10 16:00:37 -07:00
|
|
|
observerLock := new(sync.RWMutex)
|
|
|
|
observerValueToReport := new(float64)
|
2020-05-14 07:06:03 +08:00
|
|
|
observerLabelsToReport := new([]kv.KeyValue)
|
2020-05-19 21:33:10 -07:00
|
|
|
cb := func(_ context.Context, result metric.Float64ObserverResult) {
|
2020-03-10 16:00:37 -07:00
|
|
|
(*observerLock).RLock()
|
|
|
|
value := *observerValueToReport
|
2020-03-27 14:06:48 -07:00
|
|
|
labels := *observerLabelsToReport
|
2020-03-10 16:00:37 -07:00
|
|
|
(*observerLock).RUnlock()
|
2020-03-27 14:06:48 -07:00
|
|
|
result.Observe(value, labels...)
|
2020-03-10 16:00:37 -07:00
|
|
|
}
|
2020-05-21 15:42:14 -07:00
|
|
|
_ = metric.Must(meter).NewFloat64ValueObserver("ex.com.one", cb,
|
2020-05-18 11:03:43 -07:00
|
|
|
metric.WithDescription("A ValueObserver set to 1.0"),
|
2019-11-26 15:47:15 -04:00
|
|
|
)
|
|
|
|
|
2020-05-15 22:11:12 -07:00
|
|
|
valuerecorder := metric.Must(meter).NewFloat64ValueRecorder("ex.com.two")
|
|
|
|
counter := metric.Must(meter).NewFloat64Counter("ex.com.three")
|
2019-11-26 15:47:15 -04:00
|
|
|
|
2020-05-14 07:06:03 +08:00
|
|
|
commonLabels := []kv.KeyValue{lemonsKey.Int(10), kv.String("A", "1"), kv.String("B", "2"), kv.String("C", "3")}
|
|
|
|
notSoCommonLabels := []kv.KeyValue{lemonsKey.Int(13)}
|
2019-11-26 15:47:15 -04:00
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2020-03-10 16:00:37 -07:00
|
|
|
(*observerLock).Lock()
|
|
|
|
*observerValueToReport = 1.0
|
2020-03-27 14:06:48 -07:00
|
|
|
*observerLabelsToReport = commonLabels
|
2020-03-10 16:00:37 -07:00
|
|
|
(*observerLock).Unlock()
|
2019-11-26 15:47:15 -04:00
|
|
|
meter.RecordBatch(
|
|
|
|
ctx,
|
|
|
|
commonLabels,
|
2020-05-15 22:11:12 -07:00
|
|
|
valuerecorder.Measurement(2.0),
|
|
|
|
counter.Measurement(12.0),
|
2019-11-26 15:47:15 -04:00
|
|
|
)
|
|
|
|
|
2020-03-10 16:00:37 -07:00
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
|
|
|
|
(*observerLock).Lock()
|
|
|
|
*observerValueToReport = 1.0
|
2020-03-27 14:06:48 -07:00
|
|
|
*observerLabelsToReport = notSoCommonLabels
|
2020-03-10 16:00:37 -07:00
|
|
|
(*observerLock).Unlock()
|
2019-11-26 15:47:15 -04:00
|
|
|
meter.RecordBatch(
|
|
|
|
ctx,
|
|
|
|
notSoCommonLabels,
|
2020-05-15 22:11:12 -07:00
|
|
|
valuerecorder.Measurement(2.0),
|
|
|
|
counter.Measurement(22.0),
|
2019-11-26 15:47:15 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
|
2020-03-10 16:00:37 -07:00
|
|
|
(*observerLock).Lock()
|
|
|
|
*observerValueToReport = 13.0
|
2020-03-27 14:06:48 -07:00
|
|
|
*observerLabelsToReport = commonLabels
|
2020-03-10 16:00:37 -07:00
|
|
|
(*observerLock).Unlock()
|
2019-11-26 15:47:15 -04:00
|
|
|
meter.RecordBatch(
|
|
|
|
ctx,
|
|
|
|
commonLabels,
|
2020-05-15 22:11:12 -07:00
|
|
|
valuerecorder.Measurement(12.0),
|
|
|
|
counter.Measurement(13.0),
|
2019-11-26 15:47:15 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
time.Sleep(100 * time.Second)
|
|
|
|
}
|