Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add experimental support for OTLP Exporter #98

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
"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 @@
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
Loading