Skip to content

Commit 87d371e

Browse files
committed
Add experimental support for OTLP Exporter
Currently blocked on open-telemetry/opentelemetry-go-contrib#5281 Signed-off-by: Goutham <[email protected]>
1 parent 692cbdf commit 87d371e

File tree

4 files changed

+205
-414
lines changed

4 files changed

+205
-414
lines changed

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ It also supports an interactive mode which allows to select and delete disks in
3939
go install github.com/grafana/unused/cmd/unused@latest
4040
```
4141

42-
### `unused-exporter` Prometheus exporter
42+
### `unused-exporter` Prometheus / OTEL exporter
4343
Web server exposing Prometheus metrics about each providers count of unused disks.
44+
4445
It exposes the following metrics:
4546

4647
| Metric | Description |
@@ -61,3 +62,13 @@ Information about each unused disk is currently logged to stdout given that it c
6162
```
6263
go install github.com/grafana/unused/cmd/unused-exporter@latest
6364
```
65+
66+
#### EXPERIMENTAL: OpenTelemetry exporter
67+
This exporter also supports exporting metrics to an OpenTelemetry Collector.
68+
69+
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.
70+
71+
For example:
72+
```
73+
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
74+
```

cmd/unused-exporter/main.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ import (
1818
"time"
1919

2020
"github.com/grafana/unused/cmd/internal"
21+
"go.opentelemetry.io/contrib/exporters/autoexport"
22+
"go.opentelemetry.io/otel"
23+
otelmetric "go.opentelemetry.io/otel/sdk/metric"
2124
)
2225

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

62+
if err := setupOTLPExport(cfg.Logger); err != nil {
63+
return fmt.Errorf("setting up OTLP exporter: %w", err)
64+
}
65+
5966
if err := runWebServer(ctx, cfg); err != nil {
6067
return fmt.Errorf("running web server: %w", err)
6168
}
6269

6370
return nil
6471
}
72+
73+
func setupOTLPExport(logger *slog.Logger) error {
74+
if os.Getenv("OTEL_EXPORTER_OTLP_ENDPOINT") == "" && os.Getenv("OTEL_EXPORTER_OTLP_METRICS_ENDPOINT") == "" {
75+
logger.Debug("OTLP exporter is not enabled")
76+
return nil
77+
}
78+
os.Setenv("OTEL_METRICS_PRODUCERS", "prometheus")
79+
80+
otel.SetErrorHandler(otel.ErrorHandlerFunc(func(cause error) {
81+
logger.Error("OTLP exporter error", "err", cause)
82+
}))
83+
84+
if os.Getenv("OTEL_SERVICE_NAME") == "" {
85+
os.Setenv("OTEL_SERVICE_NAME", "unused_exporter")
86+
}
87+
88+
reader, err := autoexport.NewMetricReader(context.Background())
89+
if err != nil {
90+
return err
91+
}
92+
93+
otelmetric.NewMeterProvider(otelmetric.WithReader(reader))
94+
95+
return nil
96+
}

go.mod

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,15 @@ require (
1515
github.com/charmbracelet/bubbletea v0.20.0
1616
github.com/charmbracelet/lipgloss v0.5.0
1717
github.com/evertras/bubble-table v0.12.0
18-
github.com/prometheus/client_golang v1.12.1
19-
google.golang.org/api v0.114.0
18+
github.com/prometheus/client_golang v1.19.0
19+
go.opentelemetry.io/contrib/exporters/autoexport v0.0.0-00010101000000-000000000000
20+
go.opentelemetry.io/otel v1.26.0
21+
go.opentelemetry.io/otel/sdk/metric v1.26.0
22+
google.golang.org/api v0.162.0
2023
)
2124

2225
require (
23-
cloud.google.com/go/compute v1.19.1 // indirect
26+
cloud.google.com/go/compute v1.24.0 // indirect
2427
cloud.google.com/go/compute/metadata v0.2.3 // indirect
2528
github.com/Azure/go-autorest v14.2.0+incompatible // indirect
2629
github.com/Azure/go-autorest/autorest/adal v0.9.18 // indirect
@@ -39,38 +42,60 @@ require (
3942
github.com/aws/aws-sdk-go-v2/service/sts v1.16.3 // indirect
4043
github.com/aws/smithy-go v1.11.2 // indirect
4144
github.com/beorn7/perks v1.0.1 // indirect
45+
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
4246
github.com/cespare/xxhash/v2 v2.2.0 // indirect
4347
github.com/containerd/console v1.0.3 // indirect
4448
github.com/dimchansky/utfbom v1.1.1 // indirect
49+
github.com/felixge/httpsnoop v1.0.4 // indirect
50+
github.com/go-logr/logr v1.4.1 // indirect
51+
github.com/go-logr/stdr v1.2.2 // indirect
4552
github.com/golang-jwt/jwt/v4 v4.2.0 // indirect
46-
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
47-
github.com/golang/protobuf v1.5.3 // indirect
48-
github.com/google/uuid v1.3.0 // indirect
49-
github.com/googleapis/enterprise-certificate-proxy v0.2.3 // indirect
50-
github.com/googleapis/gax-go/v2 v2.7.1 // indirect
53+
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
54+
github.com/golang/protobuf v1.5.4 // indirect
55+
github.com/google/s2a-go v0.1.7 // indirect
56+
github.com/google/uuid v1.6.0 // indirect
57+
github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
58+
github.com/googleapis/gax-go/v2 v2.12.0 // indirect
59+
github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.1 // indirect
5160
github.com/jmespath/go-jmespath v0.4.0 // indirect
5261
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
5362
github.com/mattn/go-isatty v0.0.14 // indirect
5463
github.com/mattn/go-runewidth v0.0.13 // indirect
55-
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
5664
github.com/mitchellh/go-homedir v1.1.0 // indirect
5765
github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b // indirect
5866
github.com/muesli/reflow v0.3.0 // indirect
5967
github.com/muesli/termenv v0.11.1-0.20220212125758-44cd13922739 // indirect
60-
github.com/prometheus/client_model v0.2.0 // indirect
61-
github.com/prometheus/common v0.32.1 // indirect
62-
github.com/prometheus/procfs v0.7.3 // indirect
68+
github.com/prometheus/client_model v0.6.1 // indirect
69+
github.com/prometheus/common v0.48.0 // indirect
70+
github.com/prometheus/procfs v0.12.0 // indirect
6371
github.com/rivo/uniseg v0.2.0 // indirect
6472
github.com/sahilm/fuzzy v0.1.0 // indirect
6573
go.opencensus.io v0.24.0 // indirect
66-
golang.org/x/crypto v0.21.0 // indirect
67-
golang.org/x/net v0.23.0 // indirect
68-
golang.org/x/oauth2 v0.7.0 // indirect
69-
golang.org/x/sys v0.18.0 // indirect
70-
golang.org/x/term v0.18.0 // indirect
74+
go.opentelemetry.io/contrib/bridges/prometheus v0.50.0 // indirect
75+
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.47.0 // indirect
76+
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.26.0 // indirect
77+
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp v1.26.0 // indirect
78+
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.26.0 // indirect
79+
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.26.0 // indirect
80+
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.26.0 // indirect
81+
go.opentelemetry.io/otel/exporters/prometheus v0.48.0 // indirect
82+
go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v1.26.0 // indirect
83+
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.26.0 // indirect
84+
go.opentelemetry.io/otel/metric v1.26.0 // indirect
85+
go.opentelemetry.io/otel/sdk v1.26.0 // indirect
86+
go.opentelemetry.io/otel/trace v1.26.0 // indirect
87+
go.opentelemetry.io/proto/otlp v1.2.0 // indirect
88+
golang.org/x/crypto v0.22.0 // indirect
89+
golang.org/x/net v0.24.0 // indirect
90+
golang.org/x/oauth2 v0.17.0 // indirect
91+
golang.org/x/sys v0.19.0 // indirect
92+
golang.org/x/term v0.19.0 // indirect
7193
golang.org/x/text v0.14.0 // indirect
72-
google.golang.org/appengine v1.6.7 // indirect
73-
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 // indirect
74-
google.golang.org/grpc v1.56.3 // indirect
94+
google.golang.org/appengine v1.6.8 // indirect
95+
google.golang.org/genproto/googleapis/api v0.0.0-20240227224415-6ceb2ff114de // indirect
96+
google.golang.org/genproto/googleapis/rpc v0.0.0-20240401170217-c3f982113cda // indirect
97+
google.golang.org/grpc v1.63.2 // indirect
7598
google.golang.org/protobuf v1.33.0 // indirect
7699
)
100+
101+
replace go.opentelemetry.io/contrib/exporters/autoexport => github.com/gouthamve/opentelemetry-go-contrib/exporters/autoexport v0.0.0-20240430090043-7c327f4ec2e6

0 commit comments

Comments
 (0)