Skip to content

Commit

Permalink
Add experimental support for OTLP Exporter
Browse files Browse the repository at this point in the history
Currently blocked on open-telemetry/opentelemetry-go-contrib#5281

Signed-off-by: Goutham <gouthamve@gmail.com>
  • Loading branch information
gouthamve committed May 5, 2024
1 parent 692cbdf commit 87d371e
Show file tree
Hide file tree
Showing 4 changed files with 205 additions and 414 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ It also supports an interactive mode which allows to select and delete disks in
go install github.com/grafana/unused/cmd/unused@latest
```

### `unused-exporter` Prometheus exporter
### `unused-exporter` Prometheus / OTEL exporter
Web server exposing Prometheus metrics about each providers count of unused disks.

It exposes the following metrics:

| Metric | Description |
Expand All @@ -61,3 +62,13 @@ Information about each unused disk is currently logged to stdout given that it c
```
go install github.com/grafana/unused/cmd/unused-exporter@latest
```

#### EXPERIMENTAL: OpenTelemetry exporter
This exporter also supports exporting metrics to an OpenTelemetry Collector.

To enable this feature, set the `OTEL_EXPORTER_OTLP_ENDPOINT` environment variable to the address of the OpenTelemetry Collector. However, the OTEL SDK doesn't set an `instance` label yet, and we recommend manually setting the `service.instance.id` attribute to a unique value for each instance of the exporter.

For example:
```
OTEL_EXPORTER_OTLP_ENDPOINT=localhost:4317 OTEL_RESOURCE_ATTRIBUTES="service.instance.id=<value-of-instance-label>" unused-exporter -gcp.project=my-gcp-project -aws.profile=my-aws-profile -azure.sub=my-azure-sub
```
32 changes: 32 additions & 0 deletions cmd/unused-exporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ import (
"time"

"github.com/grafana/unused/cmd/internal"
"go.opentelemetry.io/contrib/exporters/autoexport"
"go.opentelemetry.io/otel"
otelmetric "go.opentelemetry.io/otel/sdk/metric"
)

func main() {
Expand Down Expand Up @@ -56,9 +59,38 @@ func realMain(ctx context.Context, cfg config) error {
return fmt.Errorf("registering exporter: %w", err)
}

if err := setupOTLPExport(cfg.Logger); err != nil {
return fmt.Errorf("setting up OTLP exporter: %w", err)
}

if err := runWebServer(ctx, cfg); err != nil {
return fmt.Errorf("running web server: %w", err)
}

return nil
}

func setupOTLPExport(logger *slog.Logger) error {
if os.Getenv("OTEL_EXPORTER_OTLP_ENDPOINT") == "" && os.Getenv("OTEL_EXPORTER_OTLP_METRICS_ENDPOINT") == "" {
logger.Debug("OTLP exporter is not enabled")
return nil
}
os.Setenv("OTEL_METRICS_PRODUCERS", "prometheus")

Check failure on line 78 in cmd/unused-exporter/main.go

View workflow job for this annotation

GitHub Actions / checks

Error return value of `os.Setenv` is not checked (errcheck)

otel.SetErrorHandler(otel.ErrorHandlerFunc(func(cause error) {
logger.Error("OTLP exporter error", "err", cause)
}))

if os.Getenv("OTEL_SERVICE_NAME") == "" {
os.Setenv("OTEL_SERVICE_NAME", "unused_exporter")

Check failure on line 85 in cmd/unused-exporter/main.go

View workflow job for this annotation

GitHub Actions / checks

Error return value of `os.Setenv` is not checked (errcheck)
}

reader, err := autoexport.NewMetricReader(context.Background())
if err != nil {
return err
}

otelmetric.NewMeterProvider(otelmetric.WithReader(reader))

return nil
}
65 changes: 45 additions & 20 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@ require (
github.com/charmbracelet/bubbletea v0.20.0
github.com/charmbracelet/lipgloss v0.5.0
github.com/evertras/bubble-table v0.12.0
github.com/prometheus/client_golang v1.12.1
google.golang.org/api v0.114.0
github.com/prometheus/client_golang v1.19.0
go.opentelemetry.io/contrib/exporters/autoexport v0.0.0-00010101000000-000000000000
go.opentelemetry.io/otel v1.26.0
go.opentelemetry.io/otel/sdk/metric v1.26.0
google.golang.org/api v0.162.0
)

require (
cloud.google.com/go/compute v1.19.1 // indirect
cloud.google.com/go/compute v1.24.0 // indirect
cloud.google.com/go/compute/metadata v0.2.3 // indirect
github.com/Azure/go-autorest v14.2.0+incompatible // indirect
github.com/Azure/go-autorest/autorest/adal v0.9.18 // indirect
Expand All @@ -39,38 +42,60 @@ require (
github.com/aws/aws-sdk-go-v2/service/sts v1.16.3 // indirect
github.com/aws/smithy-go v1.11.2 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/containerd/console v1.0.3 // indirect
github.com/dimchansky/utfbom v1.1.1 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/golang-jwt/jwt/v4 v4.2.0 // indirect
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.2.3 // indirect
github.com/googleapis/gax-go/v2 v2.7.1 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/s2a-go v0.1.7 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
github.com/googleapis/gax-go/v2 v2.12.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.1 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b // indirect
github.com/muesli/reflow v0.3.0 // indirect
github.com/muesli/termenv v0.11.1-0.20220212125758-44cd13922739 // 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
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.48.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/sahilm/fuzzy v0.1.0 // indirect
go.opencensus.io v0.24.0 // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/net v0.23.0 // indirect
golang.org/x/oauth2 v0.7.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/term v0.18.0 // indirect
go.opentelemetry.io/contrib/bridges/prometheus v0.50.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.47.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.26.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp v1.26.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.26.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.26.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.26.0 // indirect
go.opentelemetry.io/otel/exporters/prometheus v0.48.0 // indirect
go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v1.26.0 // indirect
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.26.0 // indirect
go.opentelemetry.io/otel/metric v1.26.0 // indirect
go.opentelemetry.io/otel/sdk v1.26.0 // indirect
go.opentelemetry.io/otel/trace v1.26.0 // indirect
go.opentelemetry.io/proto/otlp v1.2.0 // indirect
golang.org/x/crypto v0.22.0 // indirect
golang.org/x/net v0.24.0 // indirect
golang.org/x/oauth2 v0.17.0 // indirect
golang.org/x/sys v0.19.0 // indirect
golang.org/x/term v0.19.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 // indirect
google.golang.org/grpc v1.56.3 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240227224415-6ceb2ff114de // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240401170217-c3f982113cda // indirect
google.golang.org/grpc v1.63.2 // indirect
google.golang.org/protobuf v1.33.0 // indirect
)

replace go.opentelemetry.io/contrib/exporters/autoexport => github.com/gouthamve/opentelemetry-go-contrib/exporters/autoexport v0.0.0-20240430090043-7c327f4ec2e6
Loading

0 comments on commit 87d371e

Please sign in to comment.