Skip to content
This repository was archived by the owner on Mar 30, 2026. It is now read-only.

Commit c6465b5

Browse files
authored
Send User-Agent header with commit hash (#56)
This PR adds a User-Agent header on the format `node-indexer/<hash>`. We try follow the convention described [on Wikipedia](https://en.wikipedia.org/wiki/User-Agent_header#:~:text=Mozilla/%5Bversion%5D%20(%5Bsystem%20and%20browser%20information%5D)%20%5Bplatform%5D%20(%5Bplatform%20details%5D)%20%5Bextensions%5D). This will be very useful when debugging! We inject the Git hash using option 1 from Adam's excellent resource https://developers.redhat.com/articles/2022/11/14/3-ways-embed-commit-hash-go-programs#1__using__ldflags. Option 3 doesn't work because even when building with `-buildvcs`, a `go build <file>` doesn't embed the VCS info, as tracked in this open issue: golang/go#51279 (comment) Have verified this locally using Docker build.
1 parent 7095bdc commit c6465b5

3 files changed

Lines changed: 11 additions & 1 deletion

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,6 @@
2020
# Go workspace file
2121
go.work
2222
go.work.sum
23+
24+
# Binary
25+
indexer

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ bin/gofumpt: bin
1919
GOBIN=$(PWD)/bin go install mvdan.cc/gofumpt@v0.6.0
2020

2121
build: cmd/main.go
22-
CGO_ENABLED=0 go build -o indexer cmd/main.go
22+
CGO_ENABLED=0 go build -ldflags="-X github.com/duneanalytics/blockchain-ingester/client/duneapi.commitHash=$(shell git rev-parse --short HEAD)" -o indexer cmd/main.go
2323

2424
lint: bin/golangci-lint bin/gofumpt
2525
go fmt ./...

client/duneapi/client.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,12 @@ func (c *client) buildRequest(payloads []models.RPCBlock, buffer *bytes.Buffer)
138138
return request, nil
139139
}
140140

141+
// We inject the commit hash here at build time, using the -X linker flag, so we can use it in the User-Agent header
142+
var (
143+
commitHash string
144+
userAgent = fmt.Sprintf("node-indexer/%s", commitHash)
145+
)
146+
141147
func (c *client) sendRequest(ctx context.Context, request BlockchainIngestRequest) error {
142148
start := time.Now()
143149
var err error
@@ -172,6 +178,7 @@ func (c *client) sendRequest(ctx context.Context, request BlockchainIngestReques
172178
req.Header.Set("Content-Encoding", request.ContentEncoding)
173179
}
174180
req.Header.Set("Content-Type", "application/x-ndjson")
181+
req.Header.Set("User-Agent", userAgent)
175182
req.Header.Set("x-idempotency-key", request.IdempotencyKey)
176183
req.Header.Set("x-dune-evm-stack", request.EVMStack)
177184
req.Header.Set("x-dune-api-key", c.cfg.APIKey)

0 commit comments

Comments
 (0)