1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
| package main
import ( "context" "flag" "log" "math/rand" "net/http" "os" "os/signal" "time"
"github.com/prometheus/client_golang/prometheus/promhttp" "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc" "go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp" "go.opentelemetry.io/otel/exporters/prometheus" api "go.opentelemetry.io/otel/metric" "go.opentelemetry.io/otel/sdk/instrumentation" "go.opentelemetry.io/otel/sdk/metric" "go.opentelemetry.io/otel/sdk/resource" semconv "go.opentelemetry.io/otel/semconv/v1.17.0" "google.golang.org/grpc" "google.golang.org/grpc/credentials/insecure" )
var ( otlpGrpcEndpoint = flag.String("otlp-grpc", "", "otlp协议grpc导出地址,例如:192.168.239.154: 4317") otlpHttpEndpoint = flag.String("otlp-http", "", "otlp协议http导出地址,例如:192.168.239.154: 4318") )
func main() { flag.Parse() ctx := context.Background() shutdown, err := initProvider(*otlpGrpcEndpoint, *otlpHttpEndpoint) if err != nil { log.Fatal(err) } defer shutdown(ctx) getMetrics() if *otlpGrpcEndpoint == "" && *otlpHttpEndpoint == "" { go serveMetrics() } ctx, stop := signal.NotifyContext(ctx, os.Kill, os.Interrupt) defer stop() <-ctx.Done() }
func initProvider(otlpGrpcEndpoint, otlpHttpEndpoint string) (func(context.Context) error, error) { ctx := context.Background() res, err := resource.New(ctx, resource.WithOS(), resource.WithHost(), resource.WithAttributes( semconv.ServiceName("meter-basic"), semconv.ServiceVersion("1.0.0"), attribute.String("env", "dev"), attribute.String("author", "lin"), )) if err != nil { log.Println(err) return nil, err } var metricReader metric.Reader { if otlpGrpcEndpoint != "" { conn, err := grpc.DialContext(ctx, otlpGrpcEndpoint, grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithBlock()) if err != nil { log.Println(err) return nil, err } exporter, err := otlpmetricgrpc.New(ctx, otlpmetricgrpc.WithGRPCConn(conn), otlpmetricgrpc.WithInsecure()) if err != nil { log.Println(err) return nil, err } metricReader = metric.NewPeriodicReader(exporter, metric.WithInterval(time.Second*5)) } else if otlpHttpEndpoint != "" { exporter, err := otlpmetrichttp.New(context.Background(), otlpmetrichttp.WithEndpoint(otlpHttpEndpoint), otlpmetrichttp.WithInsecure()) if err != nil { log.Println(err) return nil, err } metricReader = metric.NewPeriodicReader(exporter, metric.WithInterval(time.Second*5)) } else { metricReader, err = prometheus.New() if err != nil { log.Println(err) return nil, err } } } view := metric.NewView(metric.Instrument{ Name: "custom_histogram", Scope: instrumentation.Scope{Name: "metrics-basic-meter"}, }, metric.Stream{ Name: "myhistogram", Aggregation: metric.AggregationExplicitBucketHistogram{ Boundaries: []float64{2, 4, 8, 16, 32, 64, 128, 256, 512}, }, }) provider := metric.NewMeterProvider(metric.WithResource(res), metric.WithReader(metricReader), metric.WithView(view)) otel.SetMeterProvider(provider) return provider.Shutdown, nil }
func getMetrics() { ctx := context.Background() meter := otel.Meter("metrics-basic-meter") attrs := []attribute.KeyValue{ attribute.Key("A").String("B"), attribute.Key("C").String("D"), } counter, err := meter.Float64Counter("counter", api.WithDescription("累计指标")) if err != nil { log.Fatal(err) } counter.Add(ctx, 5, api.WithAttributes(attrs...)) _, err = meter.Float64ObservableCounter("counter1", api.WithDescription("异步累计指标"), api.WithFloat64Callback(func(ctx context.Context, fo api.Float64Observer) error { fo.Observe(float64(time.Now().Unix())) return nil })) if err != nil { log.Fatal(err) } rng := rand.New(rand.NewSource(time.Now().UnixMicro())) gauge, err := meter.Float64ObservableGauge("gauge", api.WithDescription("实时指标")) if err != nil { log.Fatal(err) } meter.RegisterCallback(func(ctx context.Context, o api.Observer) error { n := rng.Intn(100) o.ObserveFloat64(gauge, float64(n)) return nil }, gauge) histogram, err := meter.Float64Histogram("histogram", api.WithDescription("直方图")) if err != nil { log.Fatal(err) } histogram.Record(ctx, 233) histogram.Record(ctx, 23) histogram.Record(ctx, 253) histogram.Record(ctx, 73) histogram.Record(ctx, 8) histogram.Record(ctx, 238)
cushistogram, err := meter.Float64Histogram("custom_histogram", api.WithDescription("自定义视图直方图")) if err != nil { log.Fatal(err) } cushistogram.Record(ctx, 233) cushistogram.Record(ctx, 23) cushistogram.Record(ctx, 253) cushistogram.Record(ctx, 73) cushistogram.Record(ctx, 8) cushistogram.Record(ctx, 238) }
func serveMetrics() { http.Handle("/metrics", promhttp.Handler()) err := http.ListenAndServe(":2223", nil) if err != nil { log.Fatal(err) } }
|