Skip to content

Commit

Permalink
Use WithFoo() arguments to autometrics.Init instead of long function …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
gagbo authored Dec 1, 2023
1 parent d79a1eb commit ebefae1
Show file tree
Hide file tree
Showing 14 changed files with 646 additions and 322 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,20 @@ versioning](https://go.dev/doc/modules/version-numbers).

### Changed

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

### Deprecated

### Removed

### Fixed

- Fix a bug where the repository provider label would overwrite the repository URL label
instead of using its own label.

### Security

## [0.9.0](https://github.com/autometrics-dev/autometrics-go/releases/tag/v0.9.0) 2023-11-17
Expand Down
76 changes: 38 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,21 +93,29 @@ import (
And then in your main function initialize the metrics

``` go
shutdown, err := autometrics.Init()
if err != nil {
log.Fatalf("could not initialize autometrics: %s", err)
}
defer shutdown(nil)
```

`Init` takes optional arguments to customize the metrics. The main ones are `WithBranch`,
`WithService`, `WithVersion`, and `WithCommit`; it will add relevant information on the
metrics for better intelligence:

```go
shutdown, err := autometrics.Init(
nil,
autometrics.DefBuckets,
autometrics.BuildInfo{Version: "0.4.0", Commit: "anySHA", Branch: "", Service: "myApp"},
nil,
nil,
autometrics.WithService("myApp"),
autometrics.WithVersion("0.4.0"),
)
if err != nil {
log.Fatalf("could not initialize autometrics: %s", err)
}
defer shutdown(nil)
```

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

> **Note**
Expand Down Expand Up @@ -270,11 +278,9 @@ import (

func main() {
shutdown, err := autometrics.Init(
nil,
autometrics.DefBuckets,
autometrics.BuildInfo{Version: "0.4.0", Commit: "anySHA", Branch: "", Service: "myApp"},
nil,
nil,
autometrics.WithVersion("0.4.0"),
autometrics.WithCommit("anySHA"),
autometrics.WithService("myApp"),
)
http.Handle("/metrics", promhttp.Handler())
}
Expand Down Expand Up @@ -375,18 +381,18 @@ import (
+ "github.com/autometrics-dev/autometrics-go/otel/autometrics"
)
```
- change the call to `autometrics.Init` to the new signature: instead of a registry,
- maybe change the call to `autometrics.Init` to the new signature: instead of a registry,
the `Init` function takes a meter name for the `otel_scope` label of the exported
metric. You can use the name of the application or its version for example
metric. That means `autometrics` won't have a `WithRegistry` option anymore, but a
`WithMeterName` instead.

``` patch
shutdown, err := autometrics.Init(
- nil,
+ "myApp/v2/prod",
autometrics.DefBuckets,
autometrics.BuildInfo{ Version: "2.1.37", Commit: "anySHA", Branch: "", Service: "myApp" },
nil,
nil,
- autometrics.WithRegistry(nil),
+ autometrics.WithMeterName("myApp/v2/prod"),
autometrics.WithVersion("2.1.37"),
autimetrics.WithCommit("anySHA"),
autometrics.WithService("myApp"),
)
```

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

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

Expand Down Expand Up @@ -480,12 +482,10 @@ the `Init` call:

``` patch
shutdown, err := autometrics.Init(
nil,
autometrics.DefBuckets,
autometrics.BuildInfo{ Version: "2.1.37", Commit: "anySHA", Branch: "", Service: "myApp" },
nil,
- nil,
+ autometrics.PrintLogger{},
autometrics.WithMeterName("myApp/v2/prod"),
autometrics.WithVersion("2.1.37"),
autometrics.WithService("myApp"),
+ autometrics.WithLogger(autometrics.PrintLogger{}),
)
```

Expand Down
35 changes: 17 additions & 18 deletions examples/otel/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,33 +26,32 @@ var (
func main() {
rand.Seed(time.Now().UnixNano())

var pushConfiguration *autometrics.PushConfiguration
autometricsInitOpts := make([]autometrics.InitOption, 0, 6)

if os.Getenv("AUTOMETRICS_OTLP_URL") != "" {
pushConfiguration = &autometrics.PushConfiguration{
CollectorURL: os.Getenv("AUTOMETRICS_OTLP_URL"),
autometricsInitOpts = append(autometricsInitOpts,
autometrics.WithPushCollectorURL(os.Getenv("AUTOMETRICS_OTLP_URL")),
// NOTE: Setting the JobName is useful when you fully control the instances that will run it.
// Otherwise (auto-scaling scenarii), it's better to leave this value out, and let
// autometrics generate an IP-based or Ulid-based identifier for you.
// JobName: "autometrics_go_otel_example",
Period: 1 * time.Second,
Timeout: 500 * time.Millisecond,
}
// autometrics.WithPushJobName("autometrics_go_otel_example"),
autometrics.WithPushPeriod(1*time.Second),
autometrics.WithPushTimeout(500*time.Millisecond),
)
}

// Everything in BuildInfo is optional.
// Every option customization is optional.
// You can also use any string variable whose value is
// injected at build time by ldflags.
shutdown, err := autometrics.Init(
"web-server-go-component",
autometrics.DefBuckets,
autometrics.BuildInfo{
Version: Version,
Commit: Commit,
Branch: Branch,
},
pushConfiguration,
autometrics.PrintLogger{},
autometricsInitOpts = append(autometricsInitOpts,
autometrics.WithMeterName("web-server-go-component"),
autometrics.WithBranch(Branch),
autometrics.WithCommit(Commit),
autometrics.WithVersion(Version),
autometrics.WithLogger(autometrics.PrintLogger{}),
)

shutdown, err := autometrics.Init(autometricsInitOpts...)
if err != nil {
log.Fatalf("Failed initialization of autometrics: %s", err)
}
Expand Down
33 changes: 17 additions & 16 deletions examples/web/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,31 @@ var (
func main() {
rand.Seed(time.Now().UnixNano())

autometricsInitOpts := make([]autometrics.InitOption, 0, 6)

// Allow the application to use a push gateway with an environment variable
// In production, you would do it with a command-line flag.
var pushConfiguration *autometrics.PushConfiguration
if os.Getenv("AUTOMETRICS_PUSH_GATEWAY_URL") != "" {
pushConfiguration = &autometrics.PushConfiguration{
CollectorURL: os.Getenv("AUTOMETRICS_PUSH_GATEWAY_URL"),
JobName: "autometrics_go_test",
}
autometricsInitOpts = append(autometricsInitOpts,
autometrics.WithPushCollectorURL(os.Getenv("AUTOMETRICS_PUSH_GATEWAY_URL")),
// NOTE: Setting the JobName is useful when you fully control the instances that will run it.
// Otherwise (auto-scaling scenarii), it's better to leave this value out, and let
// autometrics generate an IP-based or Ulid-based identifier for you.
autometrics.WithPushJobName("autometrics_go_test"),
)
}

// Everything in BuildInfo is optional.
// Every option customization is optional.
// You can also use any string variable whose value is
// injected at build time by ldflags.
shutdown, err := autometrics.Init(
nil,
autometrics.DefBuckets,
autometrics.BuildInfo{
Version: Version,
Commit: Commit,
Branch: Branch,
},
pushConfiguration,
autometrics.PrintLogger{},
autometricsInitOpts = append(autometricsInitOpts,
autometrics.WithVersion(Version),
autometrics.WithCommit(Commit),
autometrics.WithBranch(Branch),
autometrics.WithLogger(autometrics.PrintLogger{}),
)

shutdown, err := autometrics.Init(autometricsInitOpts...)
if err != nil {
log.Fatalf("Failed initialization of autometrics: %s", err)
}
Expand Down
34 changes: 17 additions & 17 deletions examples/web/cmd/main.go.orig
Original file line number Diff line number Diff line change
Expand Up @@ -27,31 +27,31 @@ var (
func main() {
rand.Seed(time.Now().UnixNano())

autometricsInitOpts := make([]autometrics.InitOption, 0, 6)

// Allow the application to use a push gateway with an environment variable
// In production, you would do it with a command-line flag.
var pushConfiguration *autometrics.PushConfiguration
if os.Getenv("AUTOMETRICS_PUSH_GATEWAY_URL") != "" {
pushConfiguration = &autometrics.PushConfiguration{
CollectorURL: os.Getenv("AUTOMETRICS_PUSH_GATEWAY_URL"),
JobName: "autometrics_go_test",
}
autometricsInitOpts = append(autometricsInitOpts,
autometrics.WithPushCollectorURL(os.Getenv("AUTOMETRICS_PUSH_GATEWAY_URL")),
// NOTE: Setting the JobName is useful when you fully control the instances that will run it.
// Otherwise (auto-scaling scenarii), it's better to leave this value out, and let
// autometrics generate an IP-based or Ulid-based identifier for you.
autometrics.WithPushJobName("autometrics_go_test"),
)
}

// Everything in BuildInfo is optional.
// Every option customization is optional.
// You can also use any string variable whose value is
// injected at build time by ldflags.
shutdown, err := autometrics.Init(
nil,
autometrics.DefBuckets,
autometrics.BuildInfo{
Version: Version,
Commit: Commit,
Branch: Branch,
Service: "autometrics-go-example-prometheus"
},
pushConfiguration,
autometrics.PrintLogger{},
autometricsInitOpts = append(autometricsInitOpts,
autometrics.WithVersion(Version),
autometrics.WithCommit(Commit),
autometrics.WithBranch(Branch),
autometrics.WithLogger(autometrics.PrintLogger{}),
)

shutdown, err := autometrics.Init(autometricsInitOpts...)
if err != nil {
log.Fatalf("Failed initialization of autometrics: %s", err)
}
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ require (
github.com/dave/jennifer v1.6.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.45.0
Expand Down
Loading

0 comments on commit ebefae1

Please sign in to comment.