Skip to content

Commit

Permalink
jmacd's draft otel-go metrics SDK current
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua MacDonald committed May 5, 2022
1 parent 7f0afe7 commit beb6166
Show file tree
Hide file tree
Showing 64 changed files with 8,484 additions and 2 deletions.
91 changes: 91 additions & 0 deletions example/prometheus/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
module go.opentelemetry.io/otel/example/prometheus

go 1.18

replace (
go.opentelemetry.io/otel => ../..
go.opentelemetry.io/otel/exporters/prometheus => ../../exporters/prometheus
go.opentelemetry.io/otel/sdk => ../../sdk
)

require (
go.opentelemetry.io/otel v1.6.3
go.opentelemetry.io/otel/exporters/prometheus v0.28.0
go.opentelemetry.io/otel/metric v0.28.0
go.opentelemetry.io/otel/sdk v1.6.1
go.opentelemetry.io/otel/sdk/metric v0.28.0
)

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/prometheus/client_golang v1.12.1 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.32.1 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
go.opentelemetry.io/otel/trace v1.6.3 // indirect
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9 // indirect
google.golang.org/protobuf v1.26.0 // indirect
)

replace go.opentelemetry.io/otel/bridge/opencensus => ../../bridge/opencensus

replace go.opentelemetry.io/otel/bridge/opentracing => ../../bridge/opentracing

replace go.opentelemetry.io/otel/example/jaeger => ../jaeger

replace go.opentelemetry.io/otel/example/namedtracer => ../namedtracer

replace go.opentelemetry.io/otel/example/opencensus => ../opencensus

replace go.opentelemetry.io/otel/example/otel-collector => ../otel-collector

replace go.opentelemetry.io/otel/example/prometheus => ./

replace go.opentelemetry.io/otel/example/zipkin => ../zipkin

replace go.opentelemetry.io/otel/exporters/jaeger => ../../exporters/jaeger

replace go.opentelemetry.io/otel/exporters/zipkin => ../../exporters/zipkin

replace go.opentelemetry.io/otel/internal/tools => ../../internal/tools

replace go.opentelemetry.io/otel/metric => ../../metric

replace go.opentelemetry.io/otel/sdk/export/metric => ../../sdk/export/metric

replace go.opentelemetry.io/otel/sdk/metric => ../../sdk/metric

replace go.opentelemetry.io/otel/trace => ../../trace

replace go.opentelemetry.io/otel/example/passthrough => ../passthrough

replace go.opentelemetry.io/otel/exporters/otlp/otlptrace => ../../exporters/otlp/otlptrace

replace go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc => ../../exporters/otlp/otlptrace/otlptracegrpc

replace go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp => ../../exporters/otlp/otlptrace/otlptracehttp

replace go.opentelemetry.io/otel/internal/metric => ../../internal/metric

replace go.opentelemetry.io/otel/exporters/otlp/otlpmetric => ../../exporters/otlp/otlpmetric

replace go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc => ../../exporters/otlp/otlpmetric/otlpmetricgrpc

replace go.opentelemetry.io/otel/exporters/stdout/stdoutmetric => ../../exporters/stdout/stdoutmetric

replace go.opentelemetry.io/otel/exporters/stdout/stdouttrace => ../../exporters/stdout/stdouttrace

replace go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp => ../../exporters/otlp/otlpmetric/otlpmetrichttp

replace go.opentelemetry.io/otel/bridge/opencensus/test => ../../bridge/opencensus/test

replace go.opentelemetry.io/otel/example/fib => ../fib

replace go.opentelemetry.io/otel/schema => ../../schema

replace go.opentelemetry.io/otel/exporters/otlp/internal/retry => ../../exporters/otlp/internal/retry
476 changes: 476 additions & 0 deletions example/prometheus/go.sum

Large diffs are not rendered by default.

138 changes: 138 additions & 0 deletions example/prometheus/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
// 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 main

import (
"context"
"fmt"
"log"
"net/http"
"sync"
"time"

"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/exporters/prometheus"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/instrument"
sdkmetric "go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/metric/view"
"go.opentelemetry.io/otel/sdk/resource"
)

var (
lemonsKey = attribute.Key("ex.com/lemons")

meterProvider metric.MeterProvider
)

func initMeter() metric.MeterProvider {
exporter, err := prometheus.New(prometheus.Config{})
if err != nil {
log.Panicf("failed to initialize prometheus exporter %v", err)
}

sdk := sdkmetric.New(
sdkmetric.WithResource(resource.NewSchemaless(attribute.String("resource", "etc"))),
sdkmetric.WithReader(
exporter,
view.WithClause(
view.MatchInstrumentName("ex.com.one"),
view.WithName("example_one"),
),
view.WithClause(
view.MatchInstrumentName("ex.com.two"),
view.WithName("example_two"),
),
view.WithClause(
view.MatchInstrumentName("ex.com.three"),
view.WithName("example_three"),
),
),
)

http.HandleFunc("/", exporter.ServeHTTP)
go func() {
_ = http.ListenAndServe(":2222", nil)
}()

fmt.Println("Prometheus server running on :2222")

return sdk
}

func main() {
sdk := initMeter()

meter := sdk.Meter("ex.com/basic")
observerLock := new(sync.RWMutex)
observerValueToReport := new(float64)
observerLabelsToReport := new([]attribute.KeyValue)

gaugeObserver, err := meter.AsyncFloat64().Gauge("ex.com.one")
if err != nil {
log.Panicf("failed to initialize instrument: %v", err)
}
_ = meter.RegisterCallback([]instrument.Asynchronous{gaugeObserver}, func(ctx context.Context) {
(*observerLock).RLock()
value := *observerValueToReport
labels := *observerLabelsToReport
(*observerLock).RUnlock()
gaugeObserver.Observe(ctx, value, labels...)
})

histogram, err := meter.SyncFloat64().Histogram("ex.com.two")
if err != nil {
log.Panicf("failed to initialize instrument: %v", err)
}
counter, err := meter.SyncFloat64().Counter("ex.com.three")
if err != nil {
log.Panicf("failed to initialize instrument: %v", err)
}

commonLabels := []attribute.KeyValue{lemonsKey.Int(10), attribute.String("A", "1"), attribute.String("B", "2"), attribute.String("C", "3")}
notSoCommonLabels := []attribute.KeyValue{lemonsKey.Int(13)}

ctx := context.Background()

(*observerLock).Lock()
*observerValueToReport = 1.0
*observerLabelsToReport = commonLabels
(*observerLock).Unlock()

histogram.Record(ctx, 2.0, commonLabels...)
counter.Add(ctx, 12.0, commonLabels...)

time.Sleep(5 * time.Second)

(*observerLock).Lock()
*observerValueToReport = 1.0
*observerLabelsToReport = notSoCommonLabels
(*observerLock).Unlock()
histogram.Record(ctx, 2.0, notSoCommonLabels...)
counter.Add(ctx, 22.0, notSoCommonLabels...)

time.Sleep(5 * time.Second)

(*observerLock).Lock()
*observerValueToReport = 13.0
*observerLabelsToReport = commonLabels
(*observerLock).Unlock()
histogram.Record(ctx, 12.0, commonLabels...)
counter.Add(ctx, 13.0, commonLabels...)

fmt.Println("Example finished updating, please visit :2222")

select {}
}
9 changes: 9 additions & 0 deletions exporters/prometheus/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# OpenTelemetry-Go Prometheus Exporter

OpenTelemetry Prometheus exporter

## Installation

```
go get -u go.opentelemetry.io/otel/exporters/prometheus
```
76 changes: 76 additions & 0 deletions exporters/prometheus/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
module go.opentelemetry.io/otel/exporters/prometheus

go 1.16

require (
github.com/prometheus/client_golang v1.12.1
github.com/stretchr/testify v1.7.1
go.opentelemetry.io/otel v1.6.3
go.opentelemetry.io/otel/metric v0.28.0
go.opentelemetry.io/otel/sdk v1.6.1
go.opentelemetry.io/otel/sdk/metric v0.28.0
)

replace go.opentelemetry.io/otel => ../..

replace go.opentelemetry.io/otel/bridge/opencensus => ../../bridge/opencensus

replace go.opentelemetry.io/otel/bridge/opentracing => ../../bridge/opentracing

replace go.opentelemetry.io/otel/example/jaeger => ../../example/jaeger

replace go.opentelemetry.io/otel/example/namedtracer => ../../example/namedtracer

replace go.opentelemetry.io/otel/example/opencensus => ../../example/opencensus

replace go.opentelemetry.io/otel/example/otel-collector => ../../example/otel-collector

replace go.opentelemetry.io/otel/example/passthrough => ../../example/passthrough

replace go.opentelemetry.io/otel/example/prometheus => ../../example/prometheus

replace go.opentelemetry.io/otel/example/zipkin => ../../example/zipkin

replace go.opentelemetry.io/otel/exporters/otlp/otlptrace => ../otlp/otlptrace

replace go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc => ../otlp/otlptrace/otlptracegrpc

replace go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp => ../otlp/otlptrace/otlptracehttp

replace go.opentelemetry.io/otel/exporters/prometheus => ./

replace go.opentelemetry.io/otel/exporters/jaeger => ../jaeger

replace go.opentelemetry.io/otel/exporters/zipkin => ../zipkin

replace go.opentelemetry.io/otel/internal/metric => ../../internal/metric

replace go.opentelemetry.io/otel/internal/tools => ../../internal/tools

replace go.opentelemetry.io/otel/metric => ../../metric

replace go.opentelemetry.io/otel/sdk => ../../sdk

replace go.opentelemetry.io/otel/sdk/export/metric => ../../sdk/export/metric

replace go.opentelemetry.io/otel/sdk/metric => ../../sdk/metric

replace go.opentelemetry.io/otel/trace => ../../trace

replace go.opentelemetry.io/otel/exporters/otlp/otlpmetric => ../otlp/otlpmetric

replace go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc => ../otlp/otlpmetric/otlpmetricgrpc

replace go.opentelemetry.io/otel/exporters/stdout/stdoutmetric => ../stdout/stdoutmetric

replace go.opentelemetry.io/otel/exporters/stdout/stdouttrace => ../stdout/stdouttrace

replace go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp => ../otlp/otlpmetric/otlpmetrichttp

replace go.opentelemetry.io/otel/bridge/opencensus/test => ../../bridge/opencensus/test

replace go.opentelemetry.io/otel/example/fib => ../../example/fib

replace go.opentelemetry.io/otel/schema => ../../schema

replace go.opentelemetry.io/otel/exporters/otlp/internal/retry => ../otlp/internal/retry
Loading

0 comments on commit beb6166

Please sign in to comment.