Skip to content

Commit acf04b8

Browse files
author
amanbolat
committed
chore: update readme
1 parent 5375dbf commit acf04b8

1 file changed

Lines changed: 33 additions & 27 deletions

File tree

README.md

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,56 +8,62 @@ A very simple utility library written in Go to log and trace a function lifecycl
88

99
## Why?
1010

11-
Because I'm tired of adding the same boilerplate code to every function I write, that usually requires three or more lines of code to log and trace a function and some metrics.
11+
Because I'm tired of adding the same boilerplate code to every function I write, that usually requires three or more
12+
lines of code to log and trace a function and some metrics.
1213

1314
## Usage
1415

15-
Just add `defer ft.Trace(ctx, "<method name>").WithError(&err).Log()` to the beginning of the function.
16+
Just add these two lines to the beginning of the function:
1617

1718
```go
18-
func Do(ctx context.Context) (err error) {
19-
defer ft.Trace(ctx, "main.Do").WithError(&err).Log()
20-
err = errors.New("unexpected error")
19+
ctx, span := ft.Start(ctx, "package.Function", ft.WithErr(&err))
20+
defer span.End()
21+
```
2122

22-
return
23+
```go
24+
func Do(ctx context.Context) (err error) {
25+
ctx, span := ft.Start(ctx, "main.Do", ft.WithErr(&err))
26+
defer span.End()
27+
28+
err := callAnotherFn()
29+
if err != nil {
30+
return err
31+
}
32+
33+
return nil
2334
}
2435
```
2536

2637
If you run the code above, you will see the following output:
2738

2839
```shell
29-
2024/01/18 01:05:07 INFO action started action=main.Do
30-
2024/01/18 01:05:07 ERROR action ended action=main.Do duration=472.583µs error=unexpected error
40+
time=2025-01-20T00:32:20.025+01:00 level=INFO msg="action started" action=main.Do
41+
time=2025-01-20T00:32:20.330+01:00 level=ERROR msg="action ended" action=main.Do duration_ms=137.546 error="error from callAnotherFn"
3142
```
3243

33-
## Tracing
3444

35-
Add the following to the main function to enable tracing:
45+
## Configuration
3646

37-
```go
38-
func main() {
39-
ft.EnableTracing()
40-
}
41-
```
42-
43-
## Metrics
44-
45-
Add the following to the main function to enable metrics:
46-
47-
```go
48-
func main() {
49-
ft.EnableMetrics()
50-
}
51-
```
47+
`ft` package provides many functions to configure its behaviour. See the table below:
5248

53-
It uses [go-metrics](github.com/hashicorp/go-metrics) package under the hood.
49+
Here's a markdown table with all the `Set` functions and their descriptions:
5450

51+
| Function | Description |
52+
|------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------|
53+
| `SetDurationMetricUnit(unit string)` | Sets the global duration metric unit. Accepts either millisecond (`ms`) or second (`s`) as valid units. Defaults to millisecond if invalid unit is provided. |
54+
| `SetDefaultLogger(l *slog.Logger)` | Sets the global logger instance. Does nothing if nil logger is provided. |
55+
| `SetLogLevelOnFailure(level slog.Level)` | Sets the global log level for failure scenarios. |
56+
| `SetLogLevelOnSuccess(level slog.Level)` | Sets the global log level for success scenarios. |
57+
| `SetTracingEnabled(v bool)` | Enables or disables global tracing functionality. |
58+
| `SetMetricsEnabled(v bool)` | Enables or disables global metrics collection. |
59+
| `SetClock(c clockwork.Clock)` | Sets the global clock instance used for time-related operations. |
60+
| `SetAppendOtelAttrs(v bool)` | Enables or disables the appending of OpenTelemetry attributes globally. |
5561

5662
## License
5763

5864
BSD Zero Clause License
5965

60-
Copyright (c) [2024] [Amanbolat Balabekov]
66+
Copyright (c) 2024 Amanbolat Balabekov
6167

6268
Permission to use, copy, modify, and/or distribute this software for any
6369
purpose with or without fee is hereby granted.

0 commit comments

Comments
 (0)