Skip to content

Commit 9b7c655

Browse files
author
Anatoli Kurtsevich
authored
Cenerating X-Request-Id for all HTTP requests (#98)
* generating X-Request-Id for all requests, using X-Request-Id in HTTPError, added tests * updated version for new release
1 parent f07f73f commit 9b7c655

File tree

4 files changed

+56
-3
lines changed

4 files changed

+56
-3
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## v0.5.10-alpha
4+
* `X-Request-Id` is generated for every HTTP request.
5+
* Stringified `HTTPError` includes the `X-Request-Id` instead of all headers from the response.
6+
37
## v0.5.9-alpha
48
* Increased auth token expiration token from 5s to 60s.
59

rai/client.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"time"
3131

3232
"github.com/apache/arrow/go/v7/arrow/ipc"
33+
"github.com/google/uuid"
3334
"github.com/pkg/errors"
3435
"github.com/relationalai/rai-sdk-go/rai/pb"
3536
"google.golang.org/protobuf/proto"
@@ -205,6 +206,9 @@ func (c *Client) ensureHeaders(req *http.Request, headers map[string]string) {
205206
if v := req.Header.Get("user-agent"); v == "" {
206207
req.Header.Set("User-Agent", userAgent)
207208
}
209+
if v := req.Header.Get("X-Request-Id"); v == "" {
210+
req.Header.Set("X-Request-Id", uuid.New().String())
211+
}
208212

209213
// add extra headers
210214
for h, v := range headers {
@@ -311,10 +315,11 @@ type HTTPError struct {
311315

312316
func (e HTTPError) Error() string {
313317
statusText := http.StatusText(e.StatusCode)
318+
xRequestId := e.Headers.Get("X-Request-Id")
314319
if e.Body != "" {
315-
return fmt.Sprintf("%d %s %s\n%s", e.StatusCode, e.Headers, statusText, e.Body)
320+
return fmt.Sprintf("%d %s %s\n%s", e.StatusCode, statusText, xRequestId, e.Body)
316321
}
317-
return fmt.Sprintf("%d %s %s", e.StatusCode, e.Headers, statusText)
322+
return fmt.Sprintf("%d %s %s", e.StatusCode, statusText, xRequestId)
318323
}
319324

320325
func newHTTPError(status int, headers http.Header, body string) error {

rai/client_test.go

+44
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ package rai
1717
import (
1818
"context"
1919
"fmt"
20+
"net/http"
2021
"strings"
2122
"testing"
2223
"time"
2324

25+
"github.com/google/uuid"
2426
"github.com/stretchr/testify/assert"
2527
)
2628

@@ -951,3 +953,45 @@ func TestTransactionAbort(t *testing.T) {
951953
assert.Nil(t, err)
952954
assert.Equal(t, "integrity constraint violation", rsp.Transaction.AbortReason)
953955
}
956+
957+
func TestXRequestId(t *testing.T) {
958+
query := `def output = 1 + 1`
959+
inputs := make([]interface{}, 0)
960+
961+
tx := TransactionRequest{
962+
Database: test.databaseName,
963+
Engine: test.engineName,
964+
Query: query,
965+
ReadOnly: true,
966+
Inputs: inputs,
967+
Tags: []string{o11yTag}}
968+
var rsp *http.Response
969+
err := test.client.request(http.MethodPost, PathTransactions, nil, nil, tx, &rsp)
970+
971+
// assert that the request id is set
972+
assert.Nil(t, err)
973+
assert.NotEmpty(t, rsp.Header.Get("X-Request-Id"))
974+
975+
// assert that the request id is returned in the response
976+
xRequestId := uuid.New().String()
977+
headers := map[string]string{"X-Request-Id": xRequestId}
978+
err = test.client.request(http.MethodPost, PathTransactions, headers, nil, tx, &rsp)
979+
980+
assert.Nil(t, err)
981+
assert.Equal(t, xRequestId, rsp.Header.Get("X-Request-Id"))
982+
983+
// assert that the request id is specified in the error
984+
tx = TransactionRequest{
985+
Database: test.databaseName,
986+
Engine: "fake-engine",
987+
Query: query,
988+
ReadOnly: true,
989+
Inputs: inputs,
990+
Tags: []string{o11yTag}}
991+
xRequestId = uuid.New().String()
992+
headers = map[string]string{"X-Request-Id": xRequestId}
993+
err = test.client.request(http.MethodPost, PathTransactions, headers, nil, tx, &rsp)
994+
995+
assert.NotNil(t, err)
996+
assert.Contains(t, err.Error(), xRequestId)
997+
}

rai/version.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@
1414

1515
package rai
1616

17-
const Version = "0.5.9-alpha"
17+
const Version = "0.5.10-alpha"

0 commit comments

Comments
 (0)