Skip to content

Commit

Permalink
Update README and example codes so it compiles correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
gagbo committed Nov 27, 2023
1 parent 7bfdb15 commit 5d8e304
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 89 deletions.
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)

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
Loading

0 comments on commit 5d8e304

Please sign in to comment.