Skip to content

Commit 0a62cf8

Browse files
authored
Miscellanous compliance fixes (#63)
* Rename function_calls_count to function_calls * Add unit to histogram duration * Replace `caller` with `caller.(function|module)` * Add `service.name` label * Update CHANGELOG.md * Fix tests * Fix service name not being exported * Update code with API changes outside of docker builds * Tweak demo apps to fail faster with cleaner logs * Update autometrics-shared rules
1 parent dea8bbf commit 0a62cf8

File tree

19 files changed

+196
-121
lines changed

19 files changed

+196
-121
lines changed

CHANGELOG.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,19 @@ versioning](https://go.dev/doc/modules/version-numbers).
2727
- The Go generator now removes all the old defer statements in function bodies before re-adding
2828
only the necessary ones. This means calling `go generate` on a file that has no annotation
2929
at all effectively cleans up the whole file from autometrics.
30-
30+
- All metrics now have a `service_name` label, which can either be compiled in `Init` call, or
31+
filled at runtime from environment variables (in order of precedence):
32+
+ `AUTOMETRICS_SERVICE_NAME`
33+
+ `OTEL_SERVICE_NAME`
34+
3135
### Changed
3236

3337
- Instead of returning an error when the go generator does not find the autometrics import
3438
in a file, it will add the needed import itself in the file.
39+
- Function calls metric has been renamed from `function_calls_count_total` to `function_calls_total`
40+
- Function calls duration histogram has been renamed from `function_calls_duration`
41+
to `function_calls_duration_seconds`
42+
- Function caller label has been split from `caller` to `caller_function` and `caller_label`
3543

3644
### Fixed
3745

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,19 @@ And then in your main function initialize the metrics
9696
autometrics.Init(
9797
nil,
9898
autometrics.DefBuckets,
99-
autometrics.BuildInfo{Version: "0.4.0", Commit: "anySHA", Branch: ""},
99+
autometrics.BuildInfo{Version: "0.4.0", Commit: "anySHA", Branch: "", Service: "myApp"},
100100
)
101101
```
102102

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

107+
> **Note**
108+
> Instead of hardcoding the service in the code, you can simply have environment variables set to fill the "Service" name.
109+
`AUTOMETRICS_SERVICE_NAME` will be used if set, otherwise `OTEL_SERVICE_NAME` will be attempted (so OpenTelemetry
110+
compatibility comes out of the box).
111+
107112
### 3. Add directives for each function you want to instrument
108113

109114
> **Warning**
@@ -235,7 +240,7 @@ func main() {
235240
autometrics.Init(
236241
nil,
237242
autometrics.DefBuckets,
238-
autometrics.BuildInfo{Version: "0.4.0", Commit: "anySHA", Branch: ""},
243+
autometrics.BuildInfo{Version: "0.4.0", Commit: "anySHA", Branch: "", Service: "myApp"},
239244
)
240245
http.Handle("/metrics", promhttp.Handler())
241246
}
@@ -345,7 +350,7 @@ metric. You can use the name of the application or its version for example
345350
- nil,
346351
+ "myApp/v2/prod",
347352
autometrics.DefBuckets,
348-
autometrics.BuildInfo{ Version: "2.1.37", Commit: "anySHA", Branch: "" },
353+
autometrics.BuildInfo{ Version: "2.1.37", Commit: "anySHA", Branch: "", Service: "myApp" },
349354
)
350355
```
351356

docker-compose.open-telemetry-example.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ services:
5151
build:
5252
context: .
5353
dockerfile: examples/otel/Dockerfile
54+
environment:
55+
AUTOMETRICS_SERVICE_NAME: open-telemetry-autometrics-demo
5456
container_name: web-server-otel
5557
restart: unless-stopped
5658
expose:

docker-compose.prometheus-example.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ services:
5252
build:
5353
context: .
5454
dockerfile: examples/web/Dockerfile
55+
environment:
56+
AUTOMETRICS_SERVICE_NAME: prometheus-autometrics-demo
5557
container_name: web-server-prom
5658
restart: unless-stopped
5759
expose:

examples/otel/cmd/main.go

Lines changed: 22 additions & 19 deletions
Large diffs are not rendered by default.

examples/otel/scripts/poll_server

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ TARGET_HOST="${TARGET_HOST:-localhost}"
66

77
while true
88
do
9-
if [ "$(($RANDOM % 2))" == "0" ]; then
10-
curl "http://${TARGET_HOST}:62086/random-error"
9+
if [ "$(($RANDOM % 3))" == "0" ]; then
10+
curl -s "http://${TARGET_HOST}:62086/random-error"
1111
fi
1212
if [ "$(($RANDOM % 4))" == "0" ]; then
13-
curl "http://${TARGET_HOST}:62086/"
13+
curl -s "http://${TARGET_HOST}:62086/"
1414
fi
15-
sleep 0.2
15+
sleep 0.05
1616
done

examples/web/cmd/main.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,21 +89,22 @@ func main() {
8989
// [Request Rate Callee]: http://localhost:9090/graph?g0.expr=%23+Rate+of+function+calls+emanating+from+%60indexHandler%60+function+per+second%2C+averaged+over+5+minute+windows%0A%0Asum+by+%28function%2C+module%2C+version%2C+commit%29+%28rate%28function_calls_count%7Bcaller%3D%22main.indexHandler%22%7D%5B5m%5D%29+%2A+on+%28instance%2C+job%29+group_left%28version%2C+commit%29+last_over_time%28build_info%5B1s%5D%29%29&g0.tab=0
9090
// [Error Ratio Callee]: http://localhost:9090/graph?g0.expr=%23+Percentage+of+function+emanating+from+%60indexHandler%60+function+that+return+errors%2C+averaged+over+5+minute+windows%0A%0A%28sum+by+%28function%2C+module%2C+version%2C+commit%29+%28rate%28function_calls_count%7Bcaller%3D%22main.indexHandler%22%2Cresult%3D%22error%22%7D%5B5m%5D%29+%2A+on+%28instance%2C+job%29+group_left%28version%2C+commit%29+last_over_time%28build_info%5B1s%5D%29%29%29+%2F+%28sum+by+%28function%2C+module%2C+version%2C+commit%29+%28rate%28function_calls_count%7Bcaller%3D%22main.indexHandler%22%7D%5B5m%5D%29+%2A+on+%28instance%2C+job%29+group_left%28version%2C+commit%29+last_over_time%28build_info%5B1s%5D%29%29%29&g0.tab=0
9191
//
92-
//autometrics:inst --slo "API" --latency-target 99 --latency-ms 100
92+
//autometrics:inst --slo "API" --latency-target 99 --latency-ms 5
9393
func indexHandler(w http.ResponseWriter, r *http.Request) error {
9494
defer autometrics.Instrument(autometrics.PreInstrument(autometrics.NewContext(
9595
r.Context(),
9696
autometrics.WithConcurrentCalls(true),
9797
autometrics.WithCallerName(true),
9898
autometrics.WithSloName("API"),
99-
autometrics.WithAlertLatency(100000000*time.Nanosecond, 99),
99+
autometrics.WithAlertLatency(5000000*time.Nanosecond, 99),
100100
)), nil) //autometrics:defer
101101

102-
time.Sleep(time.Duration(rand.Intn(200)) * time.Millisecond)
102+
msSleep := rand.Intn(200)
103+
time.Sleep(time.Duration(msSleep) * time.Millisecond)
103104

104-
fmt.Fprintf(w, "Hello, World!\n")
105+
_, err := fmt.Fprintf(w, "Slept %v ms\n", msSleep)
105106

106-
return nil
107+
return err
107108
}
108109

109110
var handlerError = errors.New("failed to handle request")

examples/web/cmd/main.go.orig

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,15 @@ func main() {
6161
//
6262
// It always succeeds and says hello.
6363
//
64-
//autometrics:inst --slo "API" --latency-target 99 --latency-ms 100
64+
//autometrics:inst --slo "API" --latency-target 99 --latency-ms 5
6565
func indexHandler(w http.ResponseWriter, r *http.Request) error {
6666

67-
time.Sleep(time.Duration(rand.Intn(200)) * time.Millisecond)
67+
msSleep := rand.Intn(200)
68+
time.Sleep(time.Duration(msSleep) * time.Millisecond)
6869

69-
fmt.Fprintf(w, "Hello, World!\n")
70+
_, err := fmt.Fprintf(w, "Slept %v ms\n", msSleep)
7071

71-
return nil
72+
return err
7273
}
7374

7475
var handlerError = errors.New("failed to handle request")

examples/web/scripts/poll_server

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ TARGET_HOST="${TARGET_HOST:-localhost}"
66

77
while true
88
do
9-
if [ "$(($RANDOM % 2))" == "0" ]; then
10-
curl "http://${TARGET_HOST}:62086/random-error"
9+
if [ "$(($RANDOM % 3))" == "0" ]; then
10+
curl -s "http://${TARGET_HOST}:62086/random-error"
1111
fi
1212
if [ "$(($RANDOM % 4))" == "0" ]; then
13-
curl "http://${TARGET_HOST}:62086/"
13+
curl -s "http://${TARGET_HOST}:62086/"
1414
fi
15-
sleep 0.2
15+
sleep 0.05
1616
done

internal/autometrics/prometheus_link_gen.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,10 @@ func addBuildInfoLabels() string {
5555
}
5656

5757
func requestRateQuery(counterName, labelKey, labelValue string) string {
58-
return fmt.Sprintf("sum by (%s, %s, %s, %s) (rate(%s{%s=\"%s\"}[5m]) %s)",
58+
return fmt.Sprintf("sum by (%s, %s, %s, %s, %s) (rate(%s{%s=\"%s\"}[5m]) %s)",
5959
prometheus.FunctionLabel,
6060
prometheus.ModuleLabel,
61+
prometheus.ServiceNameLabel,
6162
prometheus.VersionLabel,
6263
prometheus.CommitLabel,
6364
counterName,
@@ -68,9 +69,10 @@ func requestRateQuery(counterName, labelKey, labelValue string) string {
6869
}
6970

7071
func errorRatioQuery(counterName, labelKey, labelValue string) string {
71-
return fmt.Sprintf("(sum by (%s, %s, %s, %s) (rate(%s{%s=\"%s\",%s=\"error\"}[5m]) %s)) / (%s)",
72+
return fmt.Sprintf("(sum by (%s, %s, %s, %s, %s) (rate(%s{%s=\"%s\",%s=\"error\"}[5m]) %s)) / (%s)",
7273
prometheus.FunctionLabel,
7374
prometheus.ModuleLabel,
75+
prometheus.ServiceNameLabel,
7476
prometheus.VersionLabel,
7577
prometheus.CommitLabel,
7678
counterName,
@@ -83,9 +85,10 @@ func errorRatioQuery(counterName, labelKey, labelValue string) string {
8385
}
8486

8587
func latencyQuery(bucketName, labelKey, labelValue string) string {
86-
latency := fmt.Sprintf("sum by (le, %s, %s, %s, %s) (rate(%s_bucket{%s=\"%s\"}[5m]) %s)",
88+
latency := fmt.Sprintf("sum by (le, %s, %s, %s, %s, %s) (rate(%s_bucket{%s=\"%s\"}[5m]) %s)",
8789
prometheus.FunctionLabel,
8890
prometheus.ModuleLabel,
91+
prometheus.ServiceNameLabel,
8992
prometheus.VersionLabel,
9093
prometheus.CommitLabel,
9194
bucketName,
@@ -103,9 +106,10 @@ func latencyQuery(bucketName, labelKey, labelValue string) string {
103106
}
104107

105108
func concurrentCallsQuery(gaugeName, labelKey, labelValue string) string {
106-
return fmt.Sprintf("sum by (%s, %s, %s, %s) (%s{%s=\"%s\"} %s)",
109+
return fmt.Sprintf("sum by (%s, %s, %s, %s, %s) (%s{%s=\"%s\"} %s)",
107110
prometheus.FunctionLabel,
108111
prometheus.ModuleLabel,
112+
prometheus.ServiceNameLabel,
109113
prometheus.VersionLabel,
110114
prometheus.CommitLabel,
111115
gaugeName,
@@ -119,11 +123,11 @@ func (p Prometheus) GenerateAutometricsComment(ctx GeneratorContext, funcName, m
119123
requestRateUrl := p.makePrometheusUrl(
120124
requestRateQuery(prometheus.FunctionCallsCountName, prometheus.FunctionLabel, funcName), fmt.Sprintf("Rate of calls to the `%s` function per second, averaged over 5 minute windows", funcName))
121125
calleeRequestRateUrl := p.makePrometheusUrl(
122-
requestRateQuery(prometheus.FunctionCallsCountName, prometheus.CallerLabel, fmt.Sprintf("%s.%s", moduleName, funcName)), fmt.Sprintf("Rate of function calls emanating from `%s` function per second, averaged over 5 minute windows", funcName))
126+
requestRateQuery(prometheus.FunctionCallsCountName, prometheus.CallerFunctionLabel, funcName), fmt.Sprintf("Rate of function calls emanating from `%s` function per second, averaged over 5 minute windows", funcName))
123127
errorRatioUrl := p.makePrometheusUrl(
124128
errorRatioQuery(prometheus.FunctionCallsCountName, prometheus.FunctionLabel, funcName), fmt.Sprintf("Percentage of calls to the `%s` function that return errors, averaged over 5 minute windows", funcName))
125129
calleeErrorRatioUrl := p.makePrometheusUrl(
126-
errorRatioQuery(prometheus.FunctionCallsCountName, prometheus.CallerLabel, fmt.Sprintf("%s.%s", moduleName, funcName)), fmt.Sprintf("Percentage of function emanating from `%s` function that return errors, averaged over 5 minute windows", funcName))
130+
errorRatioQuery(prometheus.FunctionCallsCountName, prometheus.CallerFunctionLabel, funcName), fmt.Sprintf("Percentage of function emanating from `%s` function that return errors, averaged over 5 minute windows", funcName))
127131
latencyUrl := p.makePrometheusUrl(
128132
latencyQuery(prometheus.FunctionCallsDurationName, prometheus.FunctionLabel, funcName), fmt.Sprintf("95th and 99th percentile latencies (in seconds) for the `%s` function", funcName))
129133
concurrentCallsUrl := p.makePrometheusUrl(

0 commit comments

Comments
 (0)