mirror of
https://github.com/open-telemetry/opentelemetry-go.git
synced 2024-12-24 20:14:40 +02:00
3c8e1853f0
* factor instrumentation library out of the instrument descriptor * SDK tests pass * checkpoint work * otlp and opencensus tests passing * prometheus * tests pass, working on lint * lint applied: MetricReader->Reader * comments * Changelog * Apply suggestions from code review Co-authored-by: alrex <alrex.boten@gmail.com> * remove an interdependency * fix build * re-indent one * Apply suggestions from code review Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Lint&feedback * update after rename * comment fix * style fix for meter options * remove libraryReader, let Controller implement the reader API directly * rename 'impl' field to 'provider' * remove a type assertion * move metric/registry into internal; move registry.MeterProvider into metric controller * add test for controller registry function * CheckpointSet->Reader everywhere * lint * remove two unnecessary accessor methods; Controller implements MeterProvider and InstrumentationLibraryReader directly, no need to get these * use a sync.Map * ensure the initOnce is always called; handle multiple errors * Apply suggestions from code review Co-authored-by: Anthony Mirabella <a9@aneurysm9.com> * cleanup locking in metrictest * Revert "ensure the initOnce is always called; handle multiple errors" This reverts commit3356eb5ed0
. * Revert "use a sync.Map" This reverts commitea7bc599bd
. * restore the TODO about sync.Map Co-authored-by: alrex <alrex.boten@gmail.com> Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> Co-authored-by: Anthony Mirabella <a9@aneurysm9.com>
204 lines
5.3 KiB
Go
204 lines
5.3 KiB
Go
// 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.
|
|
|
|
package otlpmetricgrpc_test
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
"time"
|
|
|
|
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric"
|
|
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc"
|
|
"go.opentelemetry.io/otel/sdk/metric/selector/simple"
|
|
|
|
"google.golang.org/grpc/credentials"
|
|
|
|
"go.opentelemetry.io/otel"
|
|
"go.opentelemetry.io/otel/metric"
|
|
"go.opentelemetry.io/otel/metric/global"
|
|
controller "go.opentelemetry.io/otel/sdk/metric/controller/basic"
|
|
processor "go.opentelemetry.io/otel/sdk/metric/processor/basic"
|
|
)
|
|
|
|
func Example_insecure() {
|
|
ctx := context.Background()
|
|
client := otlpmetricgrpc.NewClient(otlpmetricgrpc.WithInsecure())
|
|
exp, err := otlpmetric.New(ctx, client)
|
|
if err != nil {
|
|
log.Fatalf("Failed to create the collector exporter: %v", err)
|
|
}
|
|
defer func() {
|
|
ctx, cancel := context.WithTimeout(ctx, time.Second)
|
|
defer cancel()
|
|
if err := exp.Shutdown(ctx); err != nil {
|
|
otel.Handle(err)
|
|
}
|
|
}()
|
|
|
|
pusher := controller.New(
|
|
processor.NewFactory(
|
|
simple.NewWithExactDistribution(),
|
|
exp,
|
|
),
|
|
controller.WithExporter(exp),
|
|
controller.WithCollectPeriod(2*time.Second),
|
|
)
|
|
global.SetMeterProvider(pusher)
|
|
|
|
if err := pusher.Start(ctx); err != nil {
|
|
log.Fatalf("could not start metric controoler: %v", err)
|
|
}
|
|
defer func() {
|
|
ctx, cancel := context.WithTimeout(ctx, time.Second)
|
|
defer cancel()
|
|
// pushes any last exports to the receiver
|
|
if err := pusher.Stop(ctx); err != nil {
|
|
otel.Handle(err)
|
|
}
|
|
}()
|
|
|
|
meter := global.Meter("test-meter")
|
|
|
|
// Recorder metric example
|
|
counter := metric.Must(meter).
|
|
NewFloat64Counter(
|
|
"an_important_metric",
|
|
metric.WithDescription("Measures the cumulative epicness of the app"),
|
|
)
|
|
|
|
for i := 0; i < 10; i++ {
|
|
log.Printf("Doing really hard work (%d / 10)\n", i+1)
|
|
counter.Add(ctx, 1.0)
|
|
}
|
|
}
|
|
|
|
func Example_withTLS() {
|
|
// Please take at look at https://pkg.go.dev/google.golang.org/grpc/credentials#TransportCredentials
|
|
// for ways on how to initialize gRPC TransportCredentials.
|
|
creds, err := credentials.NewClientTLSFromFile("my-cert.pem", "")
|
|
if err != nil {
|
|
log.Fatalf("failed to create gRPC client TLS credentials: %v", err)
|
|
}
|
|
|
|
ctx := context.Background()
|
|
client := otlpmetricgrpc.NewClient(otlpmetricgrpc.WithTLSCredentials(creds))
|
|
exp, err := otlpmetric.New(ctx, client)
|
|
if err != nil {
|
|
log.Fatalf("failed to create the collector exporter: %v", err)
|
|
}
|
|
defer func() {
|
|
ctx, cancel := context.WithTimeout(ctx, time.Second)
|
|
defer cancel()
|
|
if err := exp.Shutdown(ctx); err != nil {
|
|
otel.Handle(err)
|
|
}
|
|
}()
|
|
|
|
pusher := controller.New(
|
|
processor.NewFactory(
|
|
simple.NewWithExactDistribution(),
|
|
exp,
|
|
),
|
|
controller.WithExporter(exp),
|
|
controller.WithCollectPeriod(2*time.Second),
|
|
)
|
|
global.SetMeterProvider(pusher)
|
|
|
|
if err := pusher.Start(ctx); err != nil {
|
|
log.Fatalf("could not start metric controoler: %v", err)
|
|
}
|
|
|
|
defer func() {
|
|
ctx, cancel := context.WithTimeout(ctx, time.Second)
|
|
defer cancel()
|
|
// pushes any last exports to the receiver
|
|
if err := pusher.Stop(ctx); err != nil {
|
|
otel.Handle(err)
|
|
}
|
|
}()
|
|
|
|
meter := global.Meter("test-meter")
|
|
|
|
// Recorder metric example
|
|
counter := metric.Must(meter).
|
|
NewFloat64Counter(
|
|
"an_important_metric",
|
|
metric.WithDescription("Measures the cumulative epicness of the app"),
|
|
)
|
|
|
|
for i := 0; i < 10; i++ {
|
|
log.Printf("Doing really hard work (%d / 10)\n", i+1)
|
|
counter.Add(ctx, 1.0)
|
|
}
|
|
}
|
|
|
|
func Example_withDifferentSignalCollectors() {
|
|
client := otlpmetricgrpc.NewClient(
|
|
otlpmetricgrpc.WithInsecure(),
|
|
otlpmetricgrpc.WithEndpoint("localhost:30080"),
|
|
)
|
|
ctx := context.Background()
|
|
exp, err := otlpmetric.New(ctx, client)
|
|
if err != nil {
|
|
log.Fatalf("failed to create the collector exporter: %v", err)
|
|
}
|
|
|
|
defer func() {
|
|
ctx, cancel := context.WithTimeout(ctx, time.Second)
|
|
defer cancel()
|
|
if err := exp.Shutdown(ctx); err != nil {
|
|
otel.Handle(err)
|
|
}
|
|
}()
|
|
|
|
pusher := controller.New(
|
|
processor.NewFactory(
|
|
simple.NewWithExactDistribution(),
|
|
exp,
|
|
),
|
|
controller.WithExporter(exp),
|
|
controller.WithCollectPeriod(2*time.Second),
|
|
)
|
|
global.SetMeterProvider(pusher)
|
|
|
|
if err := pusher.Start(ctx); err != nil {
|
|
log.Fatalf("could not start metric controoler: %v", err)
|
|
}
|
|
defer func() {
|
|
ctx, cancel := context.WithTimeout(ctx, time.Second)
|
|
defer cancel()
|
|
// pushes any last exports to the receiver
|
|
if err := pusher.Stop(ctx); err != nil {
|
|
otel.Handle(err)
|
|
}
|
|
}()
|
|
|
|
meter := global.Meter("test-meter")
|
|
|
|
// Recorder metric example
|
|
counter := metric.Must(meter).
|
|
NewFloat64Counter(
|
|
"an_important_metric",
|
|
metric.WithDescription("Measures the cumulative epicness of the app"),
|
|
)
|
|
|
|
for i := 0; i < 10; i++ {
|
|
log.Printf("Doing really hard work (%d / 10)\n", i+1)
|
|
counter.Add(ctx, 1.0)
|
|
}
|
|
|
|
log.Printf("Done!")
|
|
}
|