Skip to content

Commit f810a86

Browse files
authored
GT-352 Add x-arango-driver header flag (#473)
1 parent 1c4b9f1 commit f810a86

File tree

11 files changed

+76
-1
lines changed

11 files changed

+76
-1
lines changed

Makefile

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
DRIVER_VERSION := 1.5.0
2+
13
PROJECT := go-driver
24
SCRIPTDIR := $(shell pwd)
35

@@ -564,4 +566,9 @@ run-v2-tests-resilientsingle: run-v2-tests-resilientsingle-with-auth
564566

565567
run-v2-tests-resilientsingle-with-auth:
566568
@echo "Resilient Single, with authentication, v2"
567-
@${MAKE} TEST_MODE="resilientsingle" TEST_AUTH="rootpw" __run_v2_tests
569+
@${MAKE} TEST_MODE="resilientsingle" TEST_AUTH="rootpw" __run_v2_tests
570+
571+
apply-version:
572+
@echo "Updating version to: $(DRIVER_VERSION)"
573+
@VERSION=$(DRIVER_VERSION) go generate version-driver.go
574+
@VERSION=$(DRIVER_VERSION) go generate ./v2/connection/version-driver.go

context.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"fmt"
2929
"reflect"
3030
"strconv"
31+
"strings"
3132
"time"
3233

3334
"github.com/arangodb/go-driver/util"
@@ -66,6 +67,7 @@ const (
6667
keyOverwrite ContextKey = "arangodb-overwrite"
6768
keyUseQueueTimeout ContextKey = "arangodb-use-queue-timeout"
6869
keyMaxQueueTime ContextKey = "arangodb-max-queue-time-seconds"
70+
keyDriverFlags ContextKey = "arangodb-driver-flags"
6971
)
7072

7173
type OverwriteMode string
@@ -274,6 +276,11 @@ func WithOverwrite(parent context.Context) context.Context {
274276
return context.WithValue(contextOrBackground(parent), keyOverwrite, true)
275277
}
276278

279+
// WithDriverFlags is used to configure additional flags for the `x-arango-driver` header.
280+
func WithDriverFlags(parent context.Context, value []string) context.Context {
281+
return context.WithValue(contextOrBackground(parent), keyDriverFlags, value)
282+
}
283+
277284
type contextSettings struct {
278285
Silent bool
279286
WaitForSync bool
@@ -322,6 +329,19 @@ func setDirtyReadFlagIfRequired(ctx context.Context, wasDirty bool) {
322329
}
323330
}
324331

332+
// ApplyVersionHeader adds the driver version to the request.
333+
func ApplyVersionHeader(ctx context.Context, req Request) {
334+
val := fmt.Sprintf("go-driver-v1/%s", driverVersion)
335+
if ctx != nil {
336+
if v := ctx.Value(keyDriverFlags); v != nil {
337+
if flags, ok := v.([]string); ok {
338+
val = fmt.Sprintf("%s (%s)", val, strings.Join(flags, ","))
339+
}
340+
}
341+
}
342+
req.SetHeader("x-arango-driver", val)
343+
}
344+
325345
// applyContextSettings returns the settings configured in the context in the given request.
326346
// It then returns information about the applied settings that may be needed later in API implementation functions.
327347
func applyContextSettings(ctx context.Context, req Request) contextSettings {

http/connection.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,8 @@ func (c *httpConnection) Do(ctx context.Context, req driver.Request) (driver.Res
235235
return nil, driver.WithStack(driver.InvalidArgumentError{Message: "request is not a httpRequest type"})
236236
}
237237

238+
driver.ApplyVersionHeader(ctx, req)
239+
238240
r, err := request.createHTTPRequest(c.endpoint)
239241
rctx := ctx
240242
if rctx == nil {

scripts/update-version.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cat << EOF > ./version-driver.go
2+
// Code generated by go generate; DO NOT EDIT.
3+
package driver
4+
5+
//go:generate bash ./scripts/update-version.sh
6+
var driverVersion = "$VERSION"
7+
EOF

test/context_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ func TestContextParentNil(t *testing.T) {
5151
testValue(driver.WithRawResponse(nil, &[]byte{}))
5252
testValue(driver.WithArangoQueueTimeout(nil, true))
5353
testValue(driver.WithArangoQueueTime(nil, time.Second*5))
54+
testValue(driver.WithDriverFlags(nil, []string{"foo", "bar"}))
5455
}
5556

5657
func TestContextWithArangoQueueTimeoutParams(t *testing.T) {

v2/connection/context.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ type ContextKey string
3232
const (
3333
keyUseQueueTimeout ContextKey = "arangodb-use-queue-timeout"
3434
keyMaxQueueTime ContextKey = "arangodb-max-queue-time-seconds"
35+
keyDriverFlags ContextKey = "arangodb-driver-flags"
3536
)
3637

3738
// contextOrBackground returns the given context if it is not nil.
@@ -53,3 +54,8 @@ func WithArangoQueueTimeout(parent context.Context, useQueueTimeout bool) contex
5354
func WithArangoQueueTime(parent context.Context, duration time.Duration) context.Context {
5455
return context.WithValue(contextOrBackground(parent), keyMaxQueueTime, duration)
5556
}
57+
58+
// WithDriverFlags is used to configure additional flags for the `x-arango-driver` header.
59+
func WithDriverFlags(parent context.Context, value []string) context.Context {
60+
return context.WithValue(contextOrBackground(parent), keyDriverFlags, value)
61+
}

v2/connection/modifiers.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ package connection
2525
import (
2626
"context"
2727
"fmt"
28+
"strings"
2829
"time"
2930
)
3031

@@ -53,6 +54,17 @@ func WithQuery(s, value string) RequestModifier {
5354
func applyGlobalSettings(ctx context.Context) RequestModifier {
5455
return func(r Request) error {
5556

57+
// Set version header
58+
val := fmt.Sprintf("go-driver-v2/%s", driverVersion)
59+
if ctx != nil {
60+
if v := ctx.Value(keyDriverFlags); v != nil {
61+
if flags, ok := v.([]string); ok {
62+
val = fmt.Sprintf("%s (%s)", val, strings.Join(flags, ","))
63+
}
64+
}
65+
}
66+
r.AddHeader("x-arango-driver", val)
67+
5668
// Enable Queue timeout
5769
if v := ctx.Value(keyUseQueueTimeout); v != nil {
5870
if useQueueTimeout, ok := v.(bool); ok && useQueueTimeout {

v2/connection/version-driver.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

v2/scripts/update-version.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cat << EOF > ../connection/version-driver.go
2+
// Code generated by go generate; DO NOT EDIT.
3+
package connection
4+
5+
//go:generate bash ../scripts/update-version.sh
6+
var driverVersion = "$VERSION"
7+
EOF

version-driver.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)