Skip to content

Commit ebefae1

Browse files
authored
Use WithFoo() arguments to autometrics.Init instead of long function signature (#83)
* Use WithFoo() arguments to autometrics.Init instead of long function signature * Update README and example codes so it compiles correctly * Update Changelog * Remove dead code * Small array capacity optimization * Read Otel metric export configuration from env vars
1 parent d79a1eb commit ebefae1

File tree

14 files changed

+646
-322
lines changed

14 files changed

+646
-322
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,20 @@ versioning](https://go.dev/doc/modules/version-numbers).
1212

1313
### Changed
1414

15+
- [All] The `Init` API has changed, to use arguments of type `InitOption` instead of using
16+
separate types. This means all default arguments do not need to be mentioned in the
17+
call of `Init`, and for the rest `autometrics` provides `With...` functions that allow
18+
customization.
19+
1520
### Deprecated
1621

1722
### Removed
1823

1924
### Fixed
2025

26+
- Fix a bug where the repository provider label would overwrite the repository URL label
27+
instead of using its own label.
28+
2129
### Security
2230

2331
## [0.9.0](https://github.com/autometrics-dev/autometrics-go/releases/tag/v0.9.0) 2023-11-17

README.md

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -93,21 +93,29 @@ import (
9393
And then in your main function initialize the metrics
9494

9595
``` go
96+
shutdown, err := autometrics.Init()
97+
if err != nil {
98+
log.Fatalf("could not initialize autometrics: %s", err)
99+
}
100+
defer shutdown(nil)
101+
```
102+
103+
`Init` takes optional arguments to customize the metrics. The main ones are `WithBranch`,
104+
`WithService`, `WithVersion`, and `WithCommit`; it will add relevant information on the
105+
metrics for better intelligence:
106+
107+
```go
96108
shutdown, err := autometrics.Init(
97-
nil,
98-
autometrics.DefBuckets,
99-
autometrics.BuildInfo{Version: "0.4.0", Commit: "anySHA", Branch: "", Service: "myApp"},
100-
nil,
101-
nil,
109+
autometrics.WithService("myApp"),
110+
autometrics.WithVersion("0.4.0"),
102111
)
103112
if err != nil {
104113
log.Fatalf("could not initialize autometrics: %s", err)
105114
}
106115
defer shutdown(nil)
107116
```
108117

109-
Everything in `BuildInfo` is optional. It will add relevant information on the
110-
metrics for better intelligence. You can use any string variable whose value is
118+
You can use any string variable whose value is
111119
injected at build time by `ldflags` for example, or use environment variables.
112120

113121
> **Note**
@@ -270,11 +278,9 @@ import (
270278

271279
func main() {
272280
shutdown, err := autometrics.Init(
273-
nil,
274-
autometrics.DefBuckets,
275-
autometrics.BuildInfo{Version: "0.4.0", Commit: "anySHA", Branch: "", Service: "myApp"},
276-
nil,
277-
nil,
281+
autometrics.WithVersion("0.4.0"),
282+
autometrics.WithCommit("anySHA"),
283+
autometrics.WithService("myApp"),
278284
)
279285
http.Handle("/metrics", promhttp.Handler())
280286
}
@@ -375,18 +381,18 @@ import (
375381
+ "github.com/autometrics-dev/autometrics-go/otel/autometrics"
376382
)
377383
```
378-
- change the call to `autometrics.Init` to the new signature: instead of a registry,
384+
- maybe change the call to `autometrics.Init` to the new signature: instead of a registry,
379385
the `Init` function takes a meter name for the `otel_scope` label of the exported
380-
metric. You can use the name of the application or its version for example
386+
metric. That means `autometrics` won't have a `WithRegistry` option anymore, but a
387+
`WithMeterName` instead.
381388

382389
``` patch
383390
shutdown, err := autometrics.Init(
384-
- nil,
385-
+ "myApp/v2/prod",
386-
autometrics.DefBuckets,
387-
autometrics.BuildInfo{ Version: "2.1.37", Commit: "anySHA", Branch: "", Service: "myApp" },
388-
nil,
389-
nil,
391+
- autometrics.WithRegistry(nil),
392+
+ autometrics.WithMeterName("myApp/v2/prod"),
393+
autometrics.WithVersion("2.1.37"),
394+
autimetrics.WithCommit("anySHA"),
395+
autometrics.WithService("myApp"),
390396
)
391397
```
392398

@@ -436,21 +442,17 @@ If you have a Prometheus [push
436442
gateway](https://prometheus.io/docs/instrumenting/pushing/) or an OTLP
437443
[collector](https://opentelemetry.io/docs/collector/) setup with an accessible
438444
URL, then you can directly switch from metric polling to metric pushing by
439-
passing a non `nil` argument to `autometrics.Init` for the `pushConfiguration`:
445+
passing the push-related options to `autometrics.Init`:
440446

441447
``` patch
442448
shutdown, err := autometrics.Init(
443-
"myApp/v2/prod",
444-
autometrics.DefBuckets,
445-
autometrics.BuildInfo{ Version: "2.1.37", Commit: "anySHA", Branch: "", Service: "myApp" },
446-
- nil,
447-
+ &autometrics.PushConfiguration{
448-
+ CollectorURL: "https://collector.example.com",
449-
+ JobName: "instance_2", // You can leave the JobName out to let autometrics generate one
450-
+ Period: 1 * time.Second, // Period is only relevant when using OpenTelemetry implementation
451-
+ Timeout: 500 * time.Millisecond, // Timeout is only relevant when using OpenTelementry implementation
452-
+ },
453-
nil,
449+
autometrics.WithMeterName("myApp/v2/prod"),
450+
autometrics.WithVersion("2.1.37"),
451+
autometrics.WithService("myApp"),
452+
+ autometrics.WithPushCollectorURL("https://collector.example.com"),
453+
+ autometrics.WithPushJobName("instance_2"), // You can leave the JobName out to let autometrics generate one
454+
+ autometrics.WithPushPeriod(1 * time.Second), // Period is only relevant (and available) when using OpenTelemetry implementation
455+
+ autometrics.WithPushTimeout(500 * time.Millisecond), // Timeout is only relevant (and available) when using OpenTelementry implementation
454456
)
455457
```
456458

@@ -480,12 +482,10 @@ the `Init` call:
480482

481483
``` patch
482484
shutdown, err := autometrics.Init(
483-
nil,
484-
autometrics.DefBuckets,
485-
autometrics.BuildInfo{ Version: "2.1.37", Commit: "anySHA", Branch: "", Service: "myApp" },
486-
nil,
487-
- nil,
488-
+ autometrics.PrintLogger{},
485+
autometrics.WithMeterName("myApp/v2/prod"),
486+
autometrics.WithVersion("2.1.37"),
487+
autometrics.WithService("myApp"),
488+
+ autometrics.WithLogger(autometrics.PrintLogger{}),
489489
)
490490
```
491491

examples/otel/cmd/main.go

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,33 +26,32 @@ var (
2626
func main() {
2727
rand.Seed(time.Now().UnixNano())
2828

29-
var pushConfiguration *autometrics.PushConfiguration
29+
autometricsInitOpts := make([]autometrics.InitOption, 0, 6)
30+
3031
if os.Getenv("AUTOMETRICS_OTLP_URL") != "" {
31-
pushConfiguration = &autometrics.PushConfiguration{
32-
CollectorURL: os.Getenv("AUTOMETRICS_OTLP_URL"),
32+
autometricsInitOpts = append(autometricsInitOpts,
33+
autometrics.WithPushCollectorURL(os.Getenv("AUTOMETRICS_OTLP_URL")),
3334
// NOTE: Setting the JobName is useful when you fully control the instances that will run it.
3435
// Otherwise (auto-scaling scenarii), it's better to leave this value out, and let
3536
// autometrics generate an IP-based or Ulid-based identifier for you.
36-
// JobName: "autometrics_go_otel_example",
37-
Period: 1 * time.Second,
38-
Timeout: 500 * time.Millisecond,
39-
}
37+
// autometrics.WithPushJobName("autometrics_go_otel_example"),
38+
autometrics.WithPushPeriod(1*time.Second),
39+
autometrics.WithPushTimeout(500*time.Millisecond),
40+
)
4041
}
4142

42-
// Everything in BuildInfo is optional.
43+
// Every option customization is optional.
4344
// You can also use any string variable whose value is
4445
// injected at build time by ldflags.
45-
shutdown, err := autometrics.Init(
46-
"web-server-go-component",
47-
autometrics.DefBuckets,
48-
autometrics.BuildInfo{
49-
Version: Version,
50-
Commit: Commit,
51-
Branch: Branch,
52-
},
53-
pushConfiguration,
54-
autometrics.PrintLogger{},
46+
autometricsInitOpts = append(autometricsInitOpts,
47+
autometrics.WithMeterName("web-server-go-component"),
48+
autometrics.WithBranch(Branch),
49+
autometrics.WithCommit(Commit),
50+
autometrics.WithVersion(Version),
51+
autometrics.WithLogger(autometrics.PrintLogger{}),
5552
)
53+
54+
shutdown, err := autometrics.Init(autometricsInitOpts...)
5655
if err != nil {
5756
log.Fatalf("Failed initialization of autometrics: %s", err)
5857
}

examples/web/cmd/main.go

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,30 +28,31 @@ var (
2828
func main() {
2929
rand.Seed(time.Now().UnixNano())
3030

31+
autometricsInitOpts := make([]autometrics.InitOption, 0, 6)
32+
3133
// Allow the application to use a push gateway with an environment variable
3234
// In production, you would do it with a command-line flag.
33-
var pushConfiguration *autometrics.PushConfiguration
3435
if os.Getenv("AUTOMETRICS_PUSH_GATEWAY_URL") != "" {
35-
pushConfiguration = &autometrics.PushConfiguration{
36-
CollectorURL: os.Getenv("AUTOMETRICS_PUSH_GATEWAY_URL"),
37-
JobName: "autometrics_go_test",
38-
}
36+
autometricsInitOpts = append(autometricsInitOpts,
37+
autometrics.WithPushCollectorURL(os.Getenv("AUTOMETRICS_PUSH_GATEWAY_URL")),
38+
// NOTE: Setting the JobName is useful when you fully control the instances that will run it.
39+
// Otherwise (auto-scaling scenarii), it's better to leave this value out, and let
40+
// autometrics generate an IP-based or Ulid-based identifier for you.
41+
autometrics.WithPushJobName("autometrics_go_test"),
42+
)
3943
}
4044

41-
// Everything in BuildInfo is optional.
45+
// Every option customization is optional.
4246
// You can also use any string variable whose value is
4347
// injected at build time by ldflags.
44-
shutdown, err := autometrics.Init(
45-
nil,
46-
autometrics.DefBuckets,
47-
autometrics.BuildInfo{
48-
Version: Version,
49-
Commit: Commit,
50-
Branch: Branch,
51-
},
52-
pushConfiguration,
53-
autometrics.PrintLogger{},
48+
autometricsInitOpts = append(autometricsInitOpts,
49+
autometrics.WithVersion(Version),
50+
autometrics.WithCommit(Commit),
51+
autometrics.WithBranch(Branch),
52+
autometrics.WithLogger(autometrics.PrintLogger{}),
5453
)
54+
55+
shutdown, err := autometrics.Init(autometricsInitOpts...)
5556
if err != nil {
5657
log.Fatalf("Failed initialization of autometrics: %s", err)
5758
}

examples/web/cmd/main.go.orig

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,31 +27,31 @@ var (
2727
func main() {
2828
rand.Seed(time.Now().UnixNano())
2929

30+
autometricsInitOpts := make([]autometrics.InitOption, 0, 6)
31+
3032
// Allow the application to use a push gateway with an environment variable
3133
// In production, you would do it with a command-line flag.
32-
var pushConfiguration *autometrics.PushConfiguration
3334
if os.Getenv("AUTOMETRICS_PUSH_GATEWAY_URL") != "" {
34-
pushConfiguration = &autometrics.PushConfiguration{
35-
CollectorURL: os.Getenv("AUTOMETRICS_PUSH_GATEWAY_URL"),
36-
JobName: "autometrics_go_test",
37-
}
35+
autometricsInitOpts = append(autometricsInitOpts,
36+
autometrics.WithPushCollectorURL(os.Getenv("AUTOMETRICS_PUSH_GATEWAY_URL")),
37+
// NOTE: Setting the JobName is useful when you fully control the instances that will run it.
38+
// Otherwise (auto-scaling scenarii), it's better to leave this value out, and let
39+
// autometrics generate an IP-based or Ulid-based identifier for you.
40+
autometrics.WithPushJobName("autometrics_go_test"),
41+
)
3842
}
3943

40-
// Everything in BuildInfo is optional.
44+
// Every option customization is optional.
4145
// You can also use any string variable whose value is
4246
// injected at build time by ldflags.
43-
shutdown, err := autometrics.Init(
44-
nil,
45-
autometrics.DefBuckets,
46-
autometrics.BuildInfo{
47-
Version: Version,
48-
Commit: Commit,
49-
Branch: Branch,
50-
Service: "autometrics-go-example-prometheus"
51-
},
52-
pushConfiguration,
53-
autometrics.PrintLogger{},
47+
autometricsInitOpts = append(autometricsInitOpts,
48+
autometrics.WithVersion(Version),
49+
autometrics.WithCommit(Commit),
50+
autometrics.WithBranch(Branch),
51+
autometrics.WithLogger(autometrics.PrintLogger{}),
5452
)
53+
54+
shutdown, err := autometrics.Init(autometricsInitOpts...)
5555
if err != nil {
5656
log.Fatalf("Failed initialization of autometrics: %s", err)
5757
}

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ require (
4242
github.com/dave/jennifer v1.6.0 // indirect
4343
github.com/davecgh/go-spew v1.1.1 // indirect
4444
github.com/golang/protobuf v1.5.3 // indirect
45-
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
4645
github.com/pmezard/go-difflib v1.0.0 // indirect
4746
github.com/prometheus/client_model v0.5.0 // indirect
4847
github.com/prometheus/common v0.45.0

0 commit comments

Comments
 (0)