Vendor dependencies for GCS

This commit is contained in:
Alexander Neumann
2017-08-05 20:17:15 +02:00
parent ba75a3884c
commit 8ca6a9a240
1228 changed files with 1769186 additions and 1 deletions

View File

@@ -0,0 +1,83 @@
// Copyright 2017 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package tracecontext provides encoders and decoders for Stackdriver Trace contexts.
package tracecontext
import "encoding/binary"
const (
versionID = 0
traceIDField = 0
spanIDField = 1
optsField = 2
traceIDLen = 16
spanIDLen = 8
optsLen = 1
// Len represents the length of trace context.
Len = 1 + 1 + traceIDLen + 1 + spanIDLen + 1 + optsLen
)
// Encode encodes trace ID, span ID and options into dst. The number of bytes
// written will be returned. If len(dst) isn't big enough to fit the trace context,
// a negative number is returned.
func Encode(dst []byte, traceID []byte, spanID uint64, opts byte) (n int) {
if len(dst) < Len {
return -1
}
var offset = 0
putByte := func(b byte) { dst[offset] = b; offset++ }
putUint64 := func(u uint64) { binary.LittleEndian.PutUint64(dst[offset:], u); offset += 8 }
putByte(versionID)
putByte(traceIDField)
for _, b := range traceID {
putByte(b)
}
putByte(spanIDField)
putUint64(spanID)
putByte(optsField)
putByte(opts)
return offset
}
// Decode decodes the src into a trace ID, span ID and options. If src doesn't
// contain a valid trace context, ok = false is returned.
func Decode(src []byte) (traceID []byte, spanID uint64, opts byte, ok bool) {
if len(src) < Len {
return traceID, spanID, 0, false
}
var offset = 0
readByte := func() byte { b := src[offset]; offset++; return b }
readUint64 := func() uint64 { v := binary.LittleEndian.Uint64(src[offset:]); offset += 8; return v }
if readByte() != versionID {
return traceID, spanID, 0, false
}
for offset < len(src) {
switch readByte() {
case traceIDField:
traceID = src[offset : offset+traceIDLen]
offset += traceIDLen
case spanIDField:
spanID = readUint64()
case optsField:
opts = readByte()
}
}
return traceID, spanID, opts, true
}

View File

@@ -0,0 +1,136 @@
// Copyright 2014 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package tracecontext
import (
"testing"
"cloud.google.com/go/internal/testutil"
)
var validData = []byte{0, 0, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 1, 97, 98, 99, 100, 101, 102, 103, 104, 2, 1}
func TestDecode(t *testing.T) {
tests := []struct {
name string
data []byte
wantTraceID []byte
wantSpanID uint64
wantOpts byte
wantOk bool
}{
{
name: "nil data",
data: nil,
wantTraceID: nil,
wantSpanID: 0,
wantOpts: 0,
wantOk: false,
},
{
name: "short data",
data: []byte{0, 0, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77},
wantTraceID: nil,
wantSpanID: 0,
wantOpts: 0,
wantOk: false,
},
{
name: "wrong field number",
data: []byte{0, 1, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77},
wantTraceID: nil,
wantSpanID: 0,
wantOpts: 0,
wantOk: false,
},
{
name: "valid data",
data: validData,
wantTraceID: []byte{64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79},
wantSpanID: 0x6867666564636261,
wantOpts: 1,
wantOk: true,
},
}
for _, tt := range tests {
gotTraceID, gotSpanID, gotOpts, gotOk := Decode(tt.data)
if !testutil.Equal(gotTraceID, tt.wantTraceID) {
t.Errorf("%s: Decode() gotTraceID = %v, want %v", tt.name, gotTraceID, tt.wantTraceID)
}
if gotSpanID != tt.wantSpanID {
t.Errorf("%s: Decode() gotSpanID = %v, want %v", tt.name, gotSpanID, tt.wantSpanID)
}
if gotOpts != tt.wantOpts {
t.Errorf("%s: Decode() gotOpts = %v, want %v", tt.name, gotOpts, tt.wantOpts)
}
if gotOk != tt.wantOk {
t.Errorf("%s: Decode() gotOk = %v, want %v", tt.name, gotOk, tt.wantOk)
}
}
}
func TestEncode(t *testing.T) {
tests := []struct {
name string
dst []byte
traceID []byte
spanID uint64
opts byte
wantN int
wantData []byte
}{
{
name: "short data",
dst: make([]byte, 0),
traceID: []byte("00112233445566"),
spanID: 0x6867666564636261,
opts: 1,
wantN: -1,
wantData: make([]byte, 0),
},
{
name: "valid data",
dst: make([]byte, Len),
traceID: []byte{64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79},
spanID: 0x6867666564636261,
opts: 1,
wantN: Len,
wantData: validData,
},
}
for _, tt := range tests {
gotN := Encode(tt.dst, tt.traceID, tt.spanID, tt.opts)
if gotN != tt.wantN {
t.Errorf("%s: n = %v, want %v", tt.name, gotN, tt.wantN)
}
if gotData := tt.dst; !testutil.Equal(gotData, tt.wantData) {
t.Errorf("%s: dst = %v, want %v", tt.name, gotData, tt.wantData)
}
}
}
func BenchmarkDecode(b *testing.B) {
for i := 0; i < b.N; i++ {
Decode(validData)
}
}
func BenchmarkEncode(b *testing.B) {
for i := 0; i < b.N; i++ {
traceID := make([]byte, 16)
var opts byte
Encode(validData, traceID, 0, opts)
}
}