From 86d3c9a6fec1d87b3bd032ab812037be0e8c8b1b Mon Sep 17 00:00:00 2001 From: Vojtech Vitek Date: Tue, 5 Mar 2024 15:08:55 +0100 Subject: [PATCH] Add README & CLI tool to inspect TraceId timestamp --- README.md | 25 +++++++++++++++++++++++++ cmd/traceid/main.go | 25 +++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 README.md create mode 100644 cmd/traceid/main.go diff --git a/README.md b/README.md new file mode 100644 index 0000000..072d80a --- /dev/null +++ b/README.md @@ -0,0 +1,25 @@ +# TraceId + +Go package that helps create and pass `TraceId` header among microservices for simple tracing capabilities and log grouping. + +The generated `TraceId` value is [UUIDv7](https://datatracker.ietf.org/doc/html/draft-peabody-dispatch-new-uuid-format-03#name-uuid-version-7), which lets you infer the time of the trace creation from its value. + +## Example + +TODO + +## Get time from UUIDv7 value + +``` +$ go run github.com/go-chi/traceid/cmd/traceid 018e0ee7-3605-7d75-b344-01062c6fd8bc +2024-03-05 14:56:57.477 +0100 CET +``` + +### Create new UUIDv7: +``` +$ go run github.com/go-chi/traceid/cmd/traceid +018e0ee7-3605-7d75-b344-01062c6fd8bc +``` + +## License +[MIT](./LICENSE) \ No newline at end of file diff --git a/cmd/traceid/main.go b/cmd/traceid/main.go new file mode 100644 index 0000000..bad6e70 --- /dev/null +++ b/cmd/traceid/main.go @@ -0,0 +1,25 @@ +package main + +import ( + "fmt" + "log" + "os" + "time" + + "github.com/google/uuid" +) + +func main() { + if len(os.Args) <= 1 { + fmt.Println(uuid.Must(uuid.NewV7())) + os.Exit(0) + } + + uuid, err := uuid.Parse(os.Args[1]) + if err != nil { + log.Fatal(err) + } + + t := time.Unix(uuid.Time().UnixTime()) + fmt.Println(t) +}