mirror of
https://github.com/open-telemetry/opentelemetry-go.git
synced 2024-12-12 10:04:29 +02:00
6f04903876
* draft using stop aggregating on prometheus client (counters) * remove prometheus client aggregations Measures are being exported as summaries since histograms doesn't exist on OpenTelemetry yet. Better error handling must be done. * make pre commit * add simple error callback * remove options from collector * refactor exporter to smaller methods * wording * change to snapshot * lock collection and checkpointset read * remove histogram options and unexported fields from the Exporter * documenting why prometheus uses a stateful batcher * add todo for histograms * change summaries objects to summary quantiles * remove histogram buckets from tests * wording * rename 'lockedCheckpoint' to 'syncCheckpointSet' * default summary quantiles should be defaulted to no buckets. * add quantiles options * refactor test.CheckpointSet and add docs * flip aggregators merge Co-authored-by: Joshua MacDonald <jmacd@users.noreply.github.com>
112 lines
3.2 KiB
Go
112 lines
3.2 KiB
Go
// Copyright 2019, 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"
|
|
"log"
|
|
"net/http"
|
|
"time"
|
|
|
|
"go.opentelemetry.io/otel/api/global"
|
|
"go.opentelemetry.io/otel/api/key"
|
|
"go.opentelemetry.io/otel/api/metric"
|
|
"go.opentelemetry.io/otel/exporter/metric/prometheus"
|
|
sdkmetric "go.opentelemetry.io/otel/sdk/metric"
|
|
"go.opentelemetry.io/otel/sdk/metric/batcher/defaultkeys"
|
|
"go.opentelemetry.io/otel/sdk/metric/controller/push"
|
|
"go.opentelemetry.io/otel/sdk/metric/selector/simple"
|
|
)
|
|
|
|
var (
|
|
fooKey = key.New("ex.com/foo")
|
|
barKey = key.New("ex.com/bar")
|
|
lemonsKey = key.New("ex.com/lemons")
|
|
)
|
|
|
|
func initMeter() *push.Controller {
|
|
selector := simple.NewWithExactMeasure()
|
|
exporter, err := prometheus.NewExporter(prometheus.Options{})
|
|
|
|
if err != nil {
|
|
log.Panicf("failed to initialize metric stdout exporter %v", err)
|
|
}
|
|
// Prometheus needs to use a stateful batcher since counters (and histogram since they are a collection of Counters)
|
|
// are cumulative (i.e., monotonically increasing values) and should not be resetted after each export.
|
|
//
|
|
// Prometheus uses this approach to be resilient to scrape failures.
|
|
// If a Prometheus server tries to scrape metrics from a host and fails for some reason,
|
|
// it could try again on the next scrape and no data would be lost, only resolution.
|
|
//
|
|
// Gauges (or LastValues) and Summaries are an exception to this and have different behaviors.
|
|
batcher := defaultkeys.New(selector, sdkmetric.NewDefaultLabelEncoder(), true)
|
|
pusher := push.New(batcher, exporter, time.Second)
|
|
pusher.Start()
|
|
|
|
go func() {
|
|
_ = http.ListenAndServe(":2222", exporter)
|
|
}()
|
|
|
|
global.SetMeterProvider(pusher)
|
|
return pusher
|
|
}
|
|
|
|
func main() {
|
|
defer initMeter().Stop()
|
|
|
|
meter := global.MeterProvider().Meter("ex.com/basic")
|
|
|
|
oneMetric := meter.NewFloat64Gauge("ex.com.one",
|
|
metric.WithKeys(fooKey, barKey, lemonsKey),
|
|
metric.WithDescription("A gauge set to 1.0"),
|
|
)
|
|
|
|
measureTwo := meter.NewFloat64Measure("ex.com.two", metric.WithKeys(key.New("A")))
|
|
measureThree := meter.NewFloat64Counter("ex.com.three")
|
|
|
|
commonLabels := meter.Labels(lemonsKey.Int(10), key.String("A", "1"), key.String("B", "2"), key.String("C", "3"))
|
|
notSoCommonLabels := meter.Labels(lemonsKey.Int(13))
|
|
|
|
ctx := context.Background()
|
|
|
|
meter.RecordBatch(
|
|
ctx,
|
|
commonLabels,
|
|
oneMetric.Measurement(1.0),
|
|
measureTwo.Measurement(2.0),
|
|
measureThree.Measurement(12.0),
|
|
)
|
|
|
|
meter.RecordBatch(
|
|
ctx,
|
|
notSoCommonLabels,
|
|
oneMetric.Measurement(1.0),
|
|
measureTwo.Measurement(2.0),
|
|
measureThree.Measurement(22.0),
|
|
)
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
meter.RecordBatch(
|
|
ctx,
|
|
commonLabels,
|
|
oneMetric.Measurement(13.0),
|
|
measureTwo.Measurement(12.0),
|
|
measureThree.Measurement(13.0),
|
|
)
|
|
|
|
time.Sleep(100 * time.Second)
|
|
}
|