2022-03-02 17:50:29 +02:00
|
|
|
// 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.
|
|
|
|
|
2023-03-22 23:41:28 +02:00
|
|
|
package metric_test
|
2022-03-02 17:50:29 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"runtime"
|
|
|
|
"time"
|
|
|
|
|
2023-05-02 20:15:39 +02:00
|
|
|
"go.opentelemetry.io/otel"
|
2023-01-11 07:21:58 +02:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
2022-04-28 21:52:10 +02:00
|
|
|
"go.opentelemetry.io/otel/metric"
|
2022-03-02 17:50:29 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func ExampleMeter_synchronous() {
|
2023-03-24 17:00:18 +02:00
|
|
|
// Create a histogram using the global MeterProvider.
|
2023-05-02 20:15:39 +02:00
|
|
|
workDuration, err := otel.Meter("go.opentelemetry.io/otel/metric#SyncExample").Int64Histogram(
|
2022-03-02 17:50:29 +02:00
|
|
|
"workDuration",
|
2023-04-27 20:25:48 +02:00
|
|
|
metric.WithUnit("ms"))
|
2022-03-02 17:50:29 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Failed to register instrument")
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
startTime := time.Now()
|
|
|
|
ctx := context.Background()
|
|
|
|
// Do work
|
|
|
|
// ...
|
|
|
|
workDuration.Record(ctx, time.Since(startTime).Milliseconds())
|
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleMeter_asynchronous_single() {
|
2023-05-02 20:15:39 +02:00
|
|
|
meter := otel.Meter("go.opentelemetry.io/otel/metric#AsyncExample")
|
2022-03-02 17:50:29 +02:00
|
|
|
|
2023-01-11 07:21:58 +02:00
|
|
|
_, err := meter.Int64ObservableGauge(
|
|
|
|
"DiskUsage",
|
2023-04-27 20:25:48 +02:00
|
|
|
metric.WithUnit("By"),
|
|
|
|
metric.WithInt64Callback(func(_ context.Context, obsrv metric.Int64Observer) error {
|
2023-01-11 07:21:58 +02:00
|
|
|
// Do the real work here to get the real disk usage. For example,
|
|
|
|
//
|
|
|
|
// usage, err := GetDiskUsage(diskID)
|
|
|
|
// if err != nil {
|
|
|
|
// if retryable(err) {
|
|
|
|
// // Retry the usage measurement.
|
|
|
|
// } else {
|
|
|
|
// return err
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// For demonstration purpose, a static value is used here.
|
|
|
|
usage := 75000
|
2023-04-27 20:25:48 +02:00
|
|
|
obsrv.Observe(int64(usage), metric.WithAttributes(attribute.Int("disk.id", 3)))
|
2023-01-08 17:53:07 +02:00
|
|
|
return nil
|
2023-01-11 07:21:58 +02:00
|
|
|
}),
|
|
|
|
)
|
2022-03-02 17:50:29 +02:00
|
|
|
if err != nil {
|
2023-01-11 07:21:58 +02:00
|
|
|
fmt.Println("failed to register instrument")
|
2022-03-02 17:50:29 +02:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleMeter_asynchronous_multiple() {
|
2023-05-02 20:15:39 +02:00
|
|
|
meter := otel.Meter("go.opentelemetry.io/otel/metric#MultiAsyncExample")
|
2022-03-02 17:50:29 +02:00
|
|
|
|
|
|
|
// This is just a sample of memory stats to record from the Memstats
|
2023-01-03 20:34:10 +02:00
|
|
|
heapAlloc, _ := meter.Int64ObservableUpDownCounter("heapAllocs")
|
|
|
|
gcCount, _ := meter.Int64ObservableCounter("gcCount")
|
2022-03-02 17:50:29 +02:00
|
|
|
|
2023-01-13 18:31:14 +02:00
|
|
|
_, err := meter.RegisterCallback(
|
2023-04-21 01:14:39 +02:00
|
|
|
func(_ context.Context, o metric.Observer) error {
|
2022-03-02 17:50:29 +02:00
|
|
|
memStats := &runtime.MemStats{}
|
|
|
|
// This call does work
|
|
|
|
runtime.ReadMemStats(memStats)
|
|
|
|
|
2023-01-19 16:18:26 +02:00
|
|
|
o.ObserveInt64(heapAlloc, int64(memStats.HeapAlloc))
|
|
|
|
o.ObserveInt64(gcCount, int64(memStats.NumGC))
|
2022-03-02 17:50:29 +02:00
|
|
|
|
2023-01-08 17:53:07 +02:00
|
|
|
return nil
|
2022-03-02 17:50:29 +02:00
|
|
|
},
|
2023-01-13 18:31:14 +02:00
|
|
|
heapAlloc,
|
|
|
|
gcCount,
|
2022-03-02 17:50:29 +02:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Failed to register callback")
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|