Skip to content

Commit

Permalink
Cenerating X-Request-Id for all HTTP requests (#98)
Browse files Browse the repository at this point in the history
* generating X-Request-Id for all requests, using X-Request-Id in HTTPError, added tests

* updated version for new release
  • Loading branch information
antikus authored Sep 7, 2023
1 parent f07f73f commit 9b7c655
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## v0.5.10-alpha
* `X-Request-Id` is generated for every HTTP request.
* Stringified `HTTPError` includes the `X-Request-Id` instead of all headers from the response.

## v0.5.9-alpha
* Increased auth token expiration token from 5s to 60s.

Expand Down
9 changes: 7 additions & 2 deletions rai/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"time"

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

// add extra headers
for h, v := range headers {
Expand Down Expand Up @@ -311,10 +315,11 @@ type HTTPError struct {

func (e HTTPError) Error() string {
statusText := http.StatusText(e.StatusCode)
xRequestId := e.Headers.Get("X-Request-Id")
if e.Body != "" {
return fmt.Sprintf("%d %s %s\n%s", e.StatusCode, e.Headers, statusText, e.Body)
return fmt.Sprintf("%d %s %s\n%s", e.StatusCode, statusText, xRequestId, e.Body)
}
return fmt.Sprintf("%d %s %s", e.StatusCode, e.Headers, statusText)
return fmt.Sprintf("%d %s %s", e.StatusCode, statusText, xRequestId)
}

func newHTTPError(status int, headers http.Header, body string) error {
Expand Down
44 changes: 44 additions & 0 deletions rai/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ package rai
import (
"context"
"fmt"
"net/http"
"strings"
"testing"
"time"

"github.com/google/uuid"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -951,3 +953,45 @@ func TestTransactionAbort(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, "integrity constraint violation", rsp.Transaction.AbortReason)
}

func TestXRequestId(t *testing.T) {
query := `def output = 1 + 1`
inputs := make([]interface{}, 0)

tx := TransactionRequest{
Database: test.databaseName,
Engine: test.engineName,
Query: query,
ReadOnly: true,
Inputs: inputs,
Tags: []string{o11yTag}}
var rsp *http.Response
err := test.client.request(http.MethodPost, PathTransactions, nil, nil, tx, &rsp)

// assert that the request id is set
assert.Nil(t, err)
assert.NotEmpty(t, rsp.Header.Get("X-Request-Id"))

// assert that the request id is returned in the response
xRequestId := uuid.New().String()
headers := map[string]string{"X-Request-Id": xRequestId}
err = test.client.request(http.MethodPost, PathTransactions, headers, nil, tx, &rsp)

assert.Nil(t, err)
assert.Equal(t, xRequestId, rsp.Header.Get("X-Request-Id"))

// assert that the request id is specified in the error
tx = TransactionRequest{
Database: test.databaseName,
Engine: "fake-engine",
Query: query,
ReadOnly: true,
Inputs: inputs,
Tags: []string{o11yTag}}
xRequestId = uuid.New().String()
headers = map[string]string{"X-Request-Id": xRequestId}
err = test.client.request(http.MethodPost, PathTransactions, headers, nil, tx, &rsp)

assert.NotNil(t, err)
assert.Contains(t, err.Error(), xRequestId)
}
2 changes: 1 addition & 1 deletion rai/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@

package rai

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

0 comments on commit 9b7c655

Please sign in to comment.