[PR #61] [MERGED] Elapsed time in JSON output #83

Closed
opened 2026-03-14 01:12:25 +03:00 by kerem · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ameshkov/dnslookup/pull/61
Author: @mohshami
Created: 1/19/2024
Status: Merged
Merged: 1/24/2024
Merged by: @ameshkov

Base: masterHead: elapsed_time_in_json


📝 Commits (4)

📊 Changes

2 files changed (+21 additions, -3 deletions)

View changed files

📝 README.md (+7 -1)
📝 main.go (+14 -2)

📄 Description

First I would love to say thank you for this wonderful tool. I use it all the time.

One thing I use it for is to do health checks on our caching resolvers, and something I wanted to do was to monitor how long those queries took. But noticed ElapsedTime is only available in the human readable output.

Please forgive me as the last meaningful Go that I wrote was in 2018, and even then my Go skills were quite limited.

My first instinct was to modify the output string slice before printing as follows

var elapsed = fmt.Sprintf(",\n  \"ElapsedTime\": %f \n}", float64(time.Now().Sub(startTime)) / float64(time.Millisecond))
b = append(b[:len(b)-2], elapsed...)

But I didn't feel comfortable editing formatted JSON, because it could conflict with future changes. So I went with the sjson package as follows

import "github.com/tidwall/sjson"

c, err := sjson.Set(string(b), "ElapsedTime", float64(endTime.Sub(startTime)) / float64(time.Millisecond))
if err != nil {
    log.Fatalf("Cannot append elapsed time: %s", err)
}

if err := json.Indent(&prettyJSON, []byte(c), "", "  "); err != nil {
    log.Fatalf("Cannot pretty print reply: %s", err)
}

os.Stdout.WriteString(prettyJSON.String() + "\n")

After that I ended up dropping the sjson requirement, editing compact JSON directly and then formatting the JSON output.

Also, one thing I wanted to monitor was plain DNS-over-TCP, which my script used dig for since I didn't know it was supported by dnslookup. But when when I saw https://pkg.go.dev/github.com/AdguardTeam/dnsproxy/upstream#AddressToUpstream I realized it was possible and after testing it does indeed work. So I thought it would be a good idea to expose it in the README


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ameshkov/dnslookup/pull/61 **Author:** [@mohshami](https://github.com/mohshami) **Created:** 1/19/2024 **Status:** ✅ Merged **Merged:** 1/24/2024 **Merged by:** [@ameshkov](https://github.com/ameshkov) **Base:** `master` ← **Head:** `elapsed_time_in_json` --- ### 📝 Commits (4) - [`36b7dac`](https://github.com/ameshkov/dnslookup/commit/36b7dac847e245d2f14bac2077b0215481d1aec6) Add ElapsedTime to JSON output - [`95a2c0b`](https://github.com/ameshkov/dnslookup/commit/95a2c0b41851eaa4bfe4bb297a7409b0d0ad201c) Run go fmt - [`17d4fd3`](https://github.com/ameshkov/dnslookup/commit/17d4fd3b165f89cfd62b921670b09556a63e194b) Address comment from ameshkov - [`64bd60d`](https://github.com/ameshkov/dnslookup/commit/64bd60d0736c18bad02cfc835527ad3b00299423) Fix lint error ### 📊 Changes **2 files changed** (+21 additions, -3 deletions) <details> <summary>View changed files</summary> 📝 `README.md` (+7 -1) 📝 `main.go` (+14 -2) </details> ### 📄 Description First I would love to say thank you for this wonderful tool. I use it all the time. One thing I use it for is to do health checks on our caching resolvers, and something I wanted to do was to monitor how long those queries took. But noticed ElapsedTime is only available in the human readable output. Please forgive me as the last meaningful Go that I wrote was in 2018, and even then my Go skills were quite limited. My first instinct was to modify the output string slice before printing as follows ``` var elapsed = fmt.Sprintf(",\n \"ElapsedTime\": %f \n}", float64(time.Now().Sub(startTime)) / float64(time.Millisecond)) b = append(b[:len(b)-2], elapsed...) ``` But I didn't feel comfortable editing formatted JSON, because it could conflict with future changes. So I went with the sjson package as follows ``` import "github.com/tidwall/sjson" c, err := sjson.Set(string(b), "ElapsedTime", float64(endTime.Sub(startTime)) / float64(time.Millisecond)) if err != nil { log.Fatalf("Cannot append elapsed time: %s", err) } if err := json.Indent(&prettyJSON, []byte(c), "", " "); err != nil { log.Fatalf("Cannot pretty print reply: %s", err) } os.Stdout.WriteString(prettyJSON.String() + "\n") ``` After that I ended up dropping the sjson requirement, editing compact JSON directly and then formatting the JSON output. Also, one thing I wanted to monitor was plain DNS-over-TCP, which my script used dig for since I didn't know it was supported by dnslookup. But when when I saw https://pkg.go.dev/github.com/AdguardTeam/dnsproxy/upstream#AddressToUpstream I realized it was possible and after testing it does indeed work. So I thought it would be a good idea to expose it in the README --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
kerem 2026-03-14 01:12:25 +03:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/dnslookup#83
No description provided.