phase 2: mattes/migrate -> migratex (#848)

* move mattes migrations to migratex

* changes format of migrations to migratex format
* updates test runner to use new interface (double checked this with printlns,
the tests go fully down and then up, and work on pg/mysql)

* remove mattes/migrate

* update tests from deps

* update readme

* fix other file extensions
This commit is contained in:
Reed Allman
2018-03-13 14:12:34 -07:00
committed by GitHub
parent 1f43545b63
commit 4084b727c0
697 changed files with 16924 additions and 35406 deletions

View File

@@ -20,12 +20,6 @@ import (
"google.golang.org/grpc/stats"
)
// NewClientStatsHandler enables OpenCensus stats and trace
// for gRPC clients. Deprecated, construct a ClientHandler directly.
func NewClientStatsHandler() stats.Handler {
return &ClientHandler{}
}
// ClientHandler implements a gRPC stats.Handler for recording OpenCensus stats and
// traces. Use with gRPC clients only.
type ClientHandler struct {
@@ -38,11 +32,6 @@ type ClientHandler struct {
NoStats bool
}
var (
clientTrace clientTraceHandler
clientStats clientStatsHandler
)
func (c *ClientHandler) HandleConn(ctx context.Context, cs stats.ConnStats) {
// no-op
}
@@ -54,19 +43,19 @@ func (c *ClientHandler) TagConn(ctx context.Context, cti *stats.ConnTagInfo) con
func (c *ClientHandler) HandleRPC(ctx context.Context, rs stats.RPCStats) {
if !c.NoTrace {
clientTrace.HandleRPC(ctx, rs)
c.traceHandleRPC(ctx, rs)
}
if !c.NoStats {
clientStats.HandleRPC(ctx, rs)
c.statsHandleRPC(ctx, rs)
}
}
func (c *ClientHandler) TagRPC(ctx context.Context, rti *stats.RPCTagInfo) context.Context {
if !c.NoTrace {
ctx = clientTrace.TagRPC(ctx, rti)
ctx = c.traceTagRPC(ctx, rti)
}
if !c.NoStats {
ctx = clientStats.TagRPC(ctx, rti)
ctx = c.statsTagRPC(ctx, rti)
}
return ctx
}

View File

@@ -38,47 +38,53 @@ var (
// package. These are declared as a convenience only; none are subscribed by
// default.
var (
ClientErrorCountView, _ = view.New(
"grpc.io/client/error_count",
"RPC Errors",
[]tag.Key{KeyStatus, KeyMethod},
ClientErrorCount,
view.MeanAggregation{})
ClientErrorCountView = &view.View{
Name: "grpc.io/client/error_count",
Description: "RPC Errors",
TagKeys: []tag.Key{KeyStatus, KeyMethod},
Measure: ClientErrorCount,
Aggregation: view.MeanAggregation{},
}
ClientRoundTripLatencyView, _ = view.New(
"grpc.io/client/roundtrip_latency",
"Latency in msecs",
[]tag.Key{KeyMethod},
ClientRoundTripLatency,
DefaultMillisecondsDistribution)
ClientRoundTripLatencyView = &view.View{
Name: "grpc.io/client/roundtrip_latency",
Description: "Latency in msecs",
TagKeys: []tag.Key{KeyMethod},
Measure: ClientRoundTripLatency,
Aggregation: DefaultMillisecondsDistribution,
}
ClientRequestBytesView, _ = view.New(
"grpc.io/client/request_bytes",
"Request bytes",
[]tag.Key{KeyMethod},
ClientRequestBytes,
DefaultBytesDistribution)
ClientRequestBytesView = &view.View{
Name: "grpc.io/client/request_bytes",
Description: "Request bytes",
TagKeys: []tag.Key{KeyMethod},
Measure: ClientRequestBytes,
Aggregation: DefaultBytesDistribution,
}
ClientResponseBytesView, _ = view.New(
"grpc.io/client/response_bytes",
"Response bytes",
[]tag.Key{KeyMethod},
ClientResponseBytes,
DefaultBytesDistribution)
ClientResponseBytesView = &view.View{
Name: "grpc.io/client/response_bytes",
Description: "Response bytes",
TagKeys: []tag.Key{KeyMethod},
Measure: ClientResponseBytes,
Aggregation: DefaultBytesDistribution,
}
ClientRequestCountView, _ = view.New(
"grpc.io/client/request_count",
"Count of request messages per client RPC",
[]tag.Key{KeyMethod},
ClientRequestCount,
DefaultMessageCountDistribution)
ClientRequestCountView = &view.View{
Name: "grpc.io/client/request_count",
Description: "Count of request messages per client RPC",
TagKeys: []tag.Key{KeyMethod},
Measure: ClientRequestCount,
Aggregation: DefaultMessageCountDistribution,
}
ClientResponseCountView, _ = view.New(
"grpc.io/client/response_count",
"Count of response messages per client RPC",
[]tag.Key{KeyMethod},
ClientResponseCount,
DefaultMessageCountDistribution)
ClientResponseCountView = &view.View{
Name: "grpc.io/client/response_count",
Description: "Count of response messages per client RPC",
TagKeys: []tag.Key{KeyMethod},
Measure: ClientResponseCount,
Aggregation: DefaultMessageCountDistribution,
}
)
// All the default client views provided by this package:

View File

@@ -31,7 +31,7 @@ func TestViewsAggregationsConform(t *testing.T) {
// Add any other defined views to be type checked during tests to ensure we don't regress.
assertTypeOf := func(v *view.View, wantSample view.Aggregation) {
aggregation := v.Aggregation()
aggregation := v.Aggregation
gotValue := reflect.ValueOf(aggregation)
wantValue := reflect.ValueOf(wantSample)
if gotValue.Type() != wantValue.Type() {
@@ -52,14 +52,14 @@ func TestStrictViewNames(t *testing.T) {
alreadySeen := make(map[string]int)
assertName := func(v *view.View, want string) {
_, _, line, _ := runtime.Caller(1)
if prevLine, ok := alreadySeen[v.Name()]; ok {
if prevLine, ok := alreadySeen[v.Name]; ok {
t.Errorf("Item's Name on line %d was already used on line %d", line, prevLine)
return
}
if got := v.Name(); got != want {
if got := v.Name; got != want {
t.Errorf("Item on line: %d got %q want %q", line, got, want)
}
alreadySeen[v.Name()] = line
alreadySeen[v.Name] = line
}
assertName(ClientErrorCountView, "grpc.io/client/error_count")

View File

@@ -27,14 +27,9 @@ import (
"google.golang.org/grpc/status"
)
// clientStatsHandler is a stats.Handler implementation
// that collects stats for a gRPC client. Predefined
// measures and views can be used to access the collected data.
type clientStatsHandler struct{}
// TagRPC gets the tag.Map populated by the application code, serializes
// its tags into the GRPC metadata in order to be sent to the server.
func (h *clientStatsHandler) TagRPC(ctx context.Context, info *stats.RPCTagInfo) context.Context {
func (h *ClientHandler) statsTagRPC(ctx context.Context, info *stats.RPCTagInfo) context.Context {
startTime := time.Now()
if info == nil {
if grpclog.V(2) {
@@ -60,7 +55,7 @@ func (h *clientStatsHandler) TagRPC(ctx context.Context, info *stats.RPCTagInfo)
}
// HandleRPC processes the RPC events.
func (h *clientStatsHandler) HandleRPC(ctx context.Context, s stats.RPCStats) {
func (h *ClientHandler) statsHandleRPC(ctx context.Context, s stats.RPCStats) {
switch st := s.(type) {
case *stats.Begin, *stats.OutHeader, *stats.InHeader, *stats.InTrailer, *stats.OutTrailer:
// do nothing for client
@@ -75,7 +70,7 @@ func (h *clientStatsHandler) HandleRPC(ctx context.Context, s stats.RPCStats) {
}
}
func (h *clientStatsHandler) handleRPCOutPayload(ctx context.Context, s *stats.OutPayload) {
func (h *ClientHandler) handleRPCOutPayload(ctx context.Context, s *stats.OutPayload) {
d, ok := ctx.Value(grpcClientRPCKey).(*rpcData)
if !ok {
if grpclog.V(2) {
@@ -88,7 +83,7 @@ func (h *clientStatsHandler) handleRPCOutPayload(ctx context.Context, s *stats.O
atomic.AddInt64(&d.reqCount, 1)
}
func (h *clientStatsHandler) handleRPCInPayload(ctx context.Context, s *stats.InPayload) {
func (h *ClientHandler) handleRPCInPayload(ctx context.Context, s *stats.InPayload) {
d, ok := ctx.Value(grpcClientRPCKey).(*rpcData)
if !ok {
if grpclog.V(2) {
@@ -101,7 +96,7 @@ func (h *clientStatsHandler) handleRPCInPayload(ctx context.Context, s *stats.In
atomic.AddInt64(&d.respCount, 1)
}
func (h *clientStatsHandler) handleRPCEnd(ctx context.Context, s *stats.End) {
func (h *ClientHandler) handleRPCEnd(ctx context.Context, s *stats.End) {
d, ok := ctx.Value(grpcClientRPCKey).(*rpcData)
if !ok {
if grpclog.V(2) {

View File

@@ -298,13 +298,12 @@ func TestClientDefaultCollections(t *testing.T) {
for _, tc := range tcs {
// Register views.
for _, v := range DefaultClientViews {
if err := v.Subscribe(); err != nil {
t.Error(err)
}
err := view.Subscribe(DefaultClientViews...)
if err != nil {
t.Error(err)
}
h := &clientStatsHandler{}
h := &ClientHandler{NoTrace: true}
for _, rpc := range tc.rpcs {
mods := []tag.Mutator{}
for _, t := range rpc.tags {
@@ -327,33 +326,29 @@ func TestClientDefaultCollections(t *testing.T) {
}
for _, wantData := range tc.wants {
gotRows, err := wantData.v().RetrieveData()
gotRows, err := view.RetrieveData(wantData.v().Name)
if err != nil {
t.Errorf("%q: RetrieveData(%q) = %v", tc.label, wantData.v().Name(), err)
t.Errorf("%q: RetrieveData(%q) = %v", tc.label, wantData.v().Name, err)
continue
}
for _, gotRow := range gotRows {
if !containsRow(wantData.rows, gotRow) {
t.Errorf("%q: unwanted row for view %q = %v", tc.label, wantData.v().Name(), gotRow)
t.Errorf("%q: unwanted row for view %q = %v", tc.label, wantData.v().Name, gotRow)
break
}
}
for _, wantRow := range wantData.rows {
if !containsRow(gotRows, wantRow) {
t.Errorf("%q: row missing for view %q; want %v", tc.label, wantData.v().Name(), wantRow)
t.Errorf("%q: row missing for view %q; want %v", tc.label, wantData.v().Name, wantRow)
break
}
}
}
// Unregister views to cleanup.
for _, v := range DefaultClientViews {
if err := v.Unsubscribe(); err != nil {
t.Error(err)
}
}
view.Unsubscribe(DefaultClientViews...)
}
}

View File

@@ -18,12 +18,14 @@ import (
"log"
"go.opencensus.io/plugin/ocgrpc"
"go.opencensus.io/stats/view"
"google.golang.org/grpc"
)
func ExampleClientHandler() {
// Subscribe to collect client request count.
if err := ocgrpc.ClientRequestCountView.Subscribe(); err != nil {
// Subscribe views to collect data.
err := view.Subscribe(ocgrpc.DefaultClientViews...)
if err != nil {
log.Fatal(err)
}
@@ -37,8 +39,9 @@ func ExampleClientHandler() {
}
func ExampleServerHandler() {
// Subscribe to collect server request count.
if err := ocgrpc.ServerRequestCountView.Subscribe(); err != nil {
// Subscribe to views to collect data.
err := view.Subscribe(ocgrpc.DefaultServerViews...)
if err != nil {
log.Fatal(err)
}

View File

@@ -18,6 +18,7 @@ import (
"testing"
"time"
"go.opencensus.io/stats/view"
"golang.org/x/net/context"
"go.opencensus.io/trace"
@@ -25,11 +26,8 @@ import (
"google.golang.org/grpc/stats"
)
func TestNewClientStatsHandler(t *testing.T) {
func TestClientHandler(t *testing.T) {
ctx := context.Background()
handler := NewClientStatsHandler()
te := &traceExporter{}
trace.RegisterExporter(te)
if err := ClientRequestCountView.Subscribe(); err != nil {
@@ -41,6 +39,7 @@ func TestNewClientStatsHandler(t *testing.T) {
})
ctx = trace.WithSpan(ctx, span)
var handler ClientHandler
ctx = handler.TagRPC(ctx, &stats.RPCTagInfo{
FullMethodName: "/service.foo/method",
})
@@ -53,7 +52,7 @@ func TestNewClientStatsHandler(t *testing.T) {
EndTime: time.Now(),
})
stats, err := ClientRequestCountView.RetrieveData()
stats, err := view.RetrieveData(ClientRequestCountView.Name)
if err != nil {
t.Fatal(err)
}
@@ -67,26 +66,24 @@ func TestNewClientStatsHandler(t *testing.T) {
}
// Cleanup.
if err := ClientRequestCountView.Unsubscribe(); err != nil {
t.Fatal(err)
}
view.Unsubscribe(ClientErrorCountView)
}
func TestNewServerStatsHandler(t *testing.T) {
func TestServerHandler(t *testing.T) {
ctx := context.Background()
handler := NewServerStatsHandler()
te := &traceExporter{}
trace.RegisterExporter(te)
if err := ServerRequestCountView.Subscribe(); err != nil {
t.Fatal(err)
}
// Ensure we start tracing.
span := trace.NewSpan("/foo", nil, trace.StartOptions{
Sampler: trace.AlwaysSample(),
})
ctx = trace.WithSpan(ctx, span)
handler := &ServerHandler{}
ctx = handler.TagRPC(ctx, &stats.RPCTagInfo{
FullMethodName: "/service.foo/method",
})
@@ -97,7 +94,7 @@ func TestNewServerStatsHandler(t *testing.T) {
EndTime: time.Now(),
})
stats, err := ServerRequestCountView.RetrieveData()
stats, err := view.RetrieveData(ServerRequestCountView.Name)
if err != nil {
t.Fatal(err)
}
@@ -111,10 +108,7 @@ func TestNewServerStatsHandler(t *testing.T) {
}
// Cleanup.
if err := ServerRequestCountView.Unsubscribe(); err != nil {
t.Fatal(err)
}
view.Unsubscribe(ServerRequestCountView)
}
type traceExporter struct {

View File

@@ -2,7 +2,7 @@
// source: test.proto
/*
Package testdata is a generated protocol buffer package.
Package testpb is a generated protocol buffer package.
It is generated from these files:
test.proto
@@ -11,7 +11,7 @@ It has these top-level messages:
FooRequest
FooResponse
*/
package testdata
package testpb
import proto "github.com/golang/protobuf/proto"
import fmt "fmt"
@@ -58,8 +58,8 @@ func (*FooResponse) ProtoMessage() {}
func (*FooResponse) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{1} }
func init() {
proto.RegisterType((*FooRequest)(nil), "testdata.FooRequest")
proto.RegisterType((*FooResponse)(nil), "testdata.FooResponse")
proto.RegisterType((*FooRequest)(nil), "testpb.FooRequest")
proto.RegisterType((*FooResponse)(nil), "testpb.FooResponse")
}
// Reference imports to suppress errors if they are not otherwise used.
@@ -87,7 +87,7 @@ func NewFooClient(cc *grpc.ClientConn) FooClient {
func (c *fooClient) Single(ctx context.Context, in *FooRequest, opts ...grpc.CallOption) (*FooResponse, error) {
out := new(FooResponse)
err := grpc.Invoke(ctx, "/testdata.Foo/Single", in, out, c.cc, opts...)
err := grpc.Invoke(ctx, "/testpb.Foo/Single", in, out, c.cc, opts...)
if err != nil {
return nil, err
}
@@ -95,7 +95,7 @@ func (c *fooClient) Single(ctx context.Context, in *FooRequest, opts ...grpc.Cal
}
func (c *fooClient) Multiple(ctx context.Context, opts ...grpc.CallOption) (Foo_MultipleClient, error) {
stream, err := grpc.NewClientStream(ctx, &_Foo_serviceDesc.Streams[0], c.cc, "/testdata.Foo/Multiple", opts...)
stream, err := grpc.NewClientStream(ctx, &_Foo_serviceDesc.Streams[0], c.cc, "/testpb.Foo/Multiple", opts...)
if err != nil {
return nil, err
}
@@ -146,7 +146,7 @@ func _Foo_Single_Handler(srv interface{}, ctx context.Context, dec func(interfac
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: "/testdata.Foo/Single",
FullMethod: "/testpb.Foo/Single",
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(FooServer).Single(ctx, req.(*FooRequest))
@@ -181,7 +181,7 @@ func (x *fooMultipleServer) Recv() (*FooRequest, error) {
}
var _Foo_serviceDesc = grpc.ServiceDesc{
ServiceName: "testdata.Foo",
ServiceName: "testpb.Foo",
HandlerType: (*FooServer)(nil),
Methods: []grpc.MethodDesc{
{
@@ -203,16 +203,16 @@ var _Foo_serviceDesc = grpc.ServiceDesc{
func init() { proto.RegisterFile("test.proto", fileDescriptor0) }
var fileDescriptor0 = []byte{
// 139 bytes of a gzipped FileDescriptorProto
// 137 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0xe2, 0x2a, 0x49, 0x2d, 0x2e,
0xd1, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0xe2, 0x00, 0xb1, 0x53, 0x12, 0x4b, 0x12, 0x95, 0x14,
0xb8, 0xb8, 0xdc, 0xf2, 0xf3, 0x83, 0x52, 0x0b, 0x4b, 0x53, 0x8b, 0x4b, 0x84, 0x84, 0xb8, 0x58,
0xd2, 0x12, 0x33, 0x73, 0x24, 0x18, 0x15, 0x18, 0x35, 0x38, 0x82, 0xc0, 0x6c, 0x25, 0x5e, 0x2e,
0x6e, 0xb0, 0x8a, 0xe2, 0x82, 0xfc, 0xbc, 0xe2, 0x54, 0xa3, 0x4a, 0x2e, 0x66, 0xb7, 0xfc, 0x7c,
0x21, 0x53, 0x2e, 0xb6, 0xe0, 0xcc, 0xbc, 0xf4, 0x9c, 0x54, 0x21, 0x11, 0x3d, 0x98, 0x61, 0x7a,
0x08, 0x93, 0xa4, 0x44, 0xd1, 0x44, 0x21, 0xba, 0x85, 0xac, 0xb9, 0x38, 0x7c, 0x4b, 0x73, 0x4a,
0x32, 0x0b, 0x48, 0xd4, 0xa8, 0xc1, 0x68, 0xc0, 0x98, 0xc4, 0x06, 0x76, 0xbc, 0x31, 0x20, 0x00,
0x00, 0xff, 0xff, 0x10, 0x60, 0x13, 0xc6, 0xca, 0x00, 0x00, 0x00,
0xd1, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x62, 0x03, 0xb1, 0x0b, 0x92, 0x94, 0x14, 0xb8, 0xb8,
0xdc, 0xf2, 0xf3, 0x83, 0x52, 0x0b, 0x4b, 0x53, 0x8b, 0x4b, 0x84, 0x84, 0xb8, 0x58, 0xd2, 0x12,
0x33, 0x73, 0x24, 0x18, 0x15, 0x18, 0x35, 0x38, 0x82, 0xc0, 0x6c, 0x25, 0x5e, 0x2e, 0x6e, 0xb0,
0x8a, 0xe2, 0x82, 0xfc, 0xbc, 0xe2, 0x54, 0xa3, 0x42, 0x2e, 0x66, 0xb7, 0xfc, 0x7c, 0x21, 0x43,
0x2e, 0xb6, 0xe0, 0xcc, 0xbc, 0xf4, 0x9c, 0x54, 0x21, 0x21, 0x3d, 0x88, 0x51, 0x7a, 0x08, 0x73,
0xa4, 0x84, 0x51, 0xc4, 0x20, 0x3a, 0x85, 0xcc, 0xb9, 0x38, 0x7c, 0x4b, 0x73, 0x4a, 0x32, 0x0b,
0x48, 0xd0, 0xa4, 0xc1, 0x68, 0xc0, 0x98, 0xc4, 0x06, 0x76, 0xb2, 0x31, 0x20, 0x00, 0x00, 0xff,
0xff, 0xda, 0xc5, 0x9f, 0x2f, 0xc0, 0x00, 0x00, 0x00,
}
//go:generate ./generate.sh

View File

@@ -1,6 +1,6 @@
syntax = "proto3";
package testdata;
package testpb;
message FooRequest {
bool fail = 1;

View File

@@ -20,12 +20,6 @@ import (
"google.golang.org/grpc/stats"
)
// NewServerStatsHandler enables OpenCensus stats and trace
// for gRPC servers. Deprecated, construct a ServerHandler directly.
func NewServerStatsHandler() stats.Handler {
return &ServerHandler{}
}
// ServerHandler implements gRPC stats.Handler recording OpenCensus stats and
// traces. Use with gRPC servers.
type ServerHandler struct {
@@ -38,11 +32,6 @@ type ServerHandler struct {
NoStats bool
}
var (
serverTrace serverTraceHandler
serverStats serverStatsHandler
)
func (s *ServerHandler) HandleConn(ctx context.Context, cs stats.ConnStats) {
// no-op
}
@@ -54,19 +43,19 @@ func (s *ServerHandler) TagConn(ctx context.Context, cti *stats.ConnTagInfo) con
func (s *ServerHandler) HandleRPC(ctx context.Context, rs stats.RPCStats) {
if !s.NoTrace {
serverTrace.HandleRPC(ctx, rs)
s.traceHandleRPC(ctx, rs)
}
if !s.NoStats {
serverStats.HandleRPC(ctx, rs)
s.statsHandleRPC(ctx, rs)
}
}
func (s *ServerHandler) TagRPC(ctx context.Context, rti *stats.RPCTagInfo) context.Context {
if !s.NoTrace {
ctx = serverTrace.TagRPC(ctx, rti)
ctx = s.traceTagRPC(ctx, rti)
}
if !s.NoStats {
ctx = serverStats.TagRPC(ctx, rti)
ctx = s.statsTagRPC(ctx, rti)
}
return ctx
}

View File

@@ -42,47 +42,53 @@ var (
// package. These are declared as a convenience only; none are subscribed by
// default.
var (
ServerErrorCountView, _ = view.New(
"grpc.io/server/error_count",
"RPC Errors",
[]tag.Key{KeyMethod, KeyStatus},
ServerErrorCount,
view.CountAggregation{})
ServerErrorCountView = &view.View{
Name: "grpc.io/server/error_count",
Description: "RPC Errors",
TagKeys: []tag.Key{KeyMethod, KeyStatus},
Measure: ServerErrorCount,
Aggregation: view.CountAggregation{},
}
ServerServerElapsedTimeView, _ = view.New(
"grpc.io/server/server_elapsed_time",
"Server elapsed time in msecs",
[]tag.Key{KeyMethod},
ServerServerElapsedTime,
DefaultMillisecondsDistribution)
ServerServerElapsedTimeView = &view.View{
Name: "grpc.io/server/server_elapsed_time",
Description: "Server elapsed time in msecs",
TagKeys: []tag.Key{KeyMethod},
Measure: ServerServerElapsedTime,
Aggregation: DefaultMillisecondsDistribution,
}
ServerRequestBytesView, _ = view.New(
"grpc.io/server/request_bytes",
"Request bytes",
[]tag.Key{KeyMethod},
ServerRequestBytes,
DefaultBytesDistribution)
ServerRequestBytesView = &view.View{
Name: "grpc.io/server/request_bytes",
Description: "Request bytes",
TagKeys: []tag.Key{KeyMethod},
Measure: ServerRequestBytes,
Aggregation: DefaultBytesDistribution,
}
ServerResponseBytesView, _ = view.New(
"grpc.io/server/response_bytes",
"Response bytes",
[]tag.Key{KeyMethod},
ServerResponseBytes,
DefaultBytesDistribution)
ServerResponseBytesView = &view.View{
Name: "grpc.io/server/response_bytes",
Description: "Response bytes",
TagKeys: []tag.Key{KeyMethod},
Measure: ServerResponseBytes,
Aggregation: DefaultBytesDistribution,
}
ServerRequestCountView, _ = view.New(
"grpc.io/server/request_count",
"Count of request messages per server RPC",
[]tag.Key{KeyMethod},
ServerRequestCount,
DefaultMessageCountDistribution)
ServerRequestCountView = &view.View{
Name: "grpc.io/server/request_count",
Description: "Count of request messages per server RPC",
TagKeys: []tag.Key{KeyMethod},
Measure: ServerRequestCount,
Aggregation: DefaultMessageCountDistribution,
}
ServerResponseCountView, _ = view.New(
"grpc.io/server/response_count",
"Count of response messages per server RPC",
[]tag.Key{KeyMethod},
ServerResponseCount,
DefaultMessageCountDistribution)
ServerResponseCountView = &view.View{
Name: "grpc.io/server/response_count",
Description: "Count of response messages per server RPC",
TagKeys: []tag.Key{KeyMethod},
Measure: ServerResponseCount,
Aggregation: DefaultMessageCountDistribution,
}
)
// All default server views provided by this package:

View File

@@ -29,14 +29,9 @@ import (
"google.golang.org/grpc/status"
)
// serverStatsHandler is a stats.Handler implementation
// that collects stats for a gRPC server. Predefined
// measures and views can be used to access the collected data.
type serverStatsHandler struct{}
// TagRPC gets the metadata from gRPC context, extracts the encoded tags from
// it and creates a new tag.Map and puts them into the returned context.
func (h *serverStatsHandler) TagRPC(ctx context.Context, info *stats.RPCTagInfo) context.Context {
func (h *ServerHandler) statsTagRPC(ctx context.Context, info *stats.RPCTagInfo) context.Context {
startTime := time.Now()
if info == nil {
if grpclog.V(2) {
@@ -51,7 +46,7 @@ func (h *serverStatsHandler) TagRPC(ctx context.Context, info *stats.RPCTagInfo)
}
// HandleRPC processes the RPC events.
func (h *serverStatsHandler) HandleRPC(ctx context.Context, s stats.RPCStats) {
func (h *ServerHandler) statsHandleRPC(ctx context.Context, s stats.RPCStats) {
switch st := s.(type) {
case *stats.Begin, *stats.InHeader, *stats.InTrailer, *stats.OutHeader, *stats.OutTrailer:
// Do nothing for server
@@ -67,7 +62,7 @@ func (h *serverStatsHandler) HandleRPC(ctx context.Context, s stats.RPCStats) {
}
}
func (h *serverStatsHandler) handleRPCInPayload(ctx context.Context, s *stats.InPayload) {
func (h *ServerHandler) handleRPCInPayload(ctx context.Context, s *stats.InPayload) {
d, ok := ctx.Value(grpcServerRPCKey).(*rpcData)
if !ok {
if grpclog.V(2) {
@@ -80,7 +75,7 @@ func (h *serverStatsHandler) handleRPCInPayload(ctx context.Context, s *stats.In
atomic.AddInt64(&d.reqCount, 1)
}
func (h *serverStatsHandler) handleRPCOutPayload(ctx context.Context, s *stats.OutPayload) {
func (h *ServerHandler) handleRPCOutPayload(ctx context.Context, s *stats.OutPayload) {
d, ok := ctx.Value(grpcServerRPCKey).(*rpcData)
if !ok {
if grpclog.V(2) {
@@ -93,7 +88,7 @@ func (h *serverStatsHandler) handleRPCOutPayload(ctx context.Context, s *stats.O
atomic.AddInt64(&d.respCount, 1)
}
func (h *serverStatsHandler) handleRPCEnd(ctx context.Context, s *stats.End) {
func (h *ServerHandler) handleRPCEnd(ctx context.Context, s *stats.End) {
d, ok := ctx.Value(grpcServerRPCKey).(*rpcData)
if !ok {
if grpclog.V(2) {
@@ -128,7 +123,7 @@ func (h *serverStatsHandler) handleRPCEnd(ctx context.Context, s *stats.End) {
// createTags creates a new tag map containing the tags extracted from the
// gRPC metadata.
func (h *serverStatsHandler) createTags(ctx context.Context, fullinfo string) (context.Context, error) {
func (h *ServerHandler) createTags(ctx context.Context, fullinfo string) (context.Context, error) {
mods := []tag.Mutator{
tag.Upsert(KeyMethod, methodName(fullinfo)),
}

View File

@@ -302,7 +302,7 @@ func TestServerDefaultCollections(t *testing.T) {
}
}
h := &serverStatsHandler{}
h := &ServerHandler{NoTrace: true}
for _, rpc := range tc.rpcs {
mods := []tag.Mutator{}
for _, t := range rpc.tags {
@@ -326,33 +326,29 @@ func TestServerDefaultCollections(t *testing.T) {
}
for _, wantData := range tc.wants {
gotRows, err := wantData.v().RetrieveData()
gotRows, err := view.RetrieveData(wantData.v().Name)
if err != nil {
t.Errorf("%q: RetrieveData (%q) = %v", tc.label, wantData.v().Name(), err)
t.Errorf("%q: RetrieveData (%q) = %v", tc.label, wantData.v().Name, err)
continue
}
for _, gotRow := range gotRows {
if !containsRow(wantData.rows, gotRow) {
t.Errorf("%q: unwanted row for view %q: %v", tc.label, wantData.v().Name(), gotRow)
t.Errorf("%q: unwanted row for view %q: %v", tc.label, wantData.v().Name, gotRow)
break
}
}
for _, wantRow := range wantData.rows {
if !containsRow(gotRows, wantRow) {
t.Errorf("%q: missing row for view %q: %v", tc.label, wantData.v().Name(), wantRow)
t.Errorf("%q: missing row for view %q: %v", tc.label, wantData.v().Name, wantRow)
break
}
}
}
// Unregister views to cleanup.
for _, v := range DefaultServerViews {
if err := v.Unsubscribe(); err != nil {
t.Error(err)
}
}
view.Unsubscribe(DefaultServerViews...)
}
}

View File

@@ -39,9 +39,9 @@ type rpcData struct {
// The following variables define the default hard-coded auxiliary data used by
// both the default GRPC client and GRPC server metrics.
var (
DefaultBytesDistribution = view.DistributionAggregation([]float64{0, 1024, 2048, 4096, 16384, 65536, 262144, 1048576, 4194304, 16777216, 67108864, 268435456, 1073741824, 4294967296})
DefaultMillisecondsDistribution = view.DistributionAggregation([]float64{0, 1, 2, 3, 4, 5, 6, 8, 10, 13, 16, 20, 25, 30, 40, 50, 65, 80, 100, 130, 160, 200, 250, 300, 400, 500, 650, 800, 1000, 2000, 5000, 10000, 20000, 50000, 100000})
DefaultMessageCountDistribution = view.DistributionAggregation([]float64{0, 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536})
DefaultBytesDistribution = view.DistributionAggregation{0, 1024, 2048, 4096, 16384, 65536, 262144, 1048576, 4194304, 16777216, 67108864, 268435456, 1073741824, 4294967296}
DefaultMillisecondsDistribution = view.DistributionAggregation{0, 1, 2, 3, 4, 5, 6, 8, 10, 13, 16, 20, 25, 30, 40, 50, 65, 80, 100, 130, 160, 200, 250, 300, 400, 500, 650, 800, 1000, 2000, 5000, 10000, 20000, 50000, 100000}
DefaultMessageCountDistribution = view.DistributionAggregation{0, 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536}
)
var (

View File

@@ -17,29 +17,23 @@ package ocgrpc
import (
"strings"
"google.golang.org/grpc/codes"
"go.opencensus.io/trace"
"go.opencensus.io/trace/propagation"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/stats"
"google.golang.org/grpc/status"
)
// clientTraceHandler is a an implementation of grpc.StatsHandler
// that can be passed to grpc.Dial
// using grpc.WithStatsHandler to enable trace context propagation and
// automatic span creation for outgoing gRPC requests.
type clientTraceHandler struct{}
type serverTraceHandler struct{}
const traceContextKey = "grpc-trace-bin"
// TagRPC creates a new trace span for the client side of the RPC.
//
// It returns ctx with the new trace span added and a serialization of the
// SpanContext added to the outgoing gRPC metadata.
func (c *clientTraceHandler) TagRPC(ctx context.Context, rti *stats.RPCTagInfo) context.Context {
func (c *ClientHandler) traceTagRPC(ctx context.Context, rti *stats.RPCTagInfo) context.Context {
name := "Sent" + strings.Replace(rti.FullMethodName, "/", ".", -1)
ctx, _ = trace.StartSpan(ctx, name)
traceContextBinary := propagation.Binary(trace.FromContext(ctx).SpanContext())
@@ -55,26 +49,27 @@ func (c *clientTraceHandler) TagRPC(ctx context.Context, rti *stats.RPCTagInfo)
// it finds one, uses that SpanContext as the parent context of the new span.
//
// It returns ctx, with the new trace span added.
func (s *serverTraceHandler) TagRPC(ctx context.Context, rti *stats.RPCTagInfo) context.Context {
func (s *ServerHandler) traceTagRPC(ctx context.Context, rti *stats.RPCTagInfo) context.Context {
md, _ := metadata.FromIncomingContext(ctx)
name := "Recv" + strings.Replace(rti.FullMethodName, "/", ".", -1)
if s := md[traceContextKey]; len(s) > 0 {
if parent, ok := propagation.FromBinary([]byte(s[0])); ok {
ctx, _ = trace.StartSpanWithRemoteParent(ctx, name, parent, trace.StartOptions{})
return ctx
span := trace.NewSpanWithRemoteParent(name, parent, trace.StartOptions{})
return trace.WithSpan(ctx, span)
}
}
// TODO(ramonza): should we ignore the in-process parent here?
ctx, _ = trace.StartSpan(ctx, name)
return ctx
}
// HandleRPC processes the RPC stats, adding information to the current trace span.
func (c *clientTraceHandler) HandleRPC(ctx context.Context, rs stats.RPCStats) {
func (c *ClientHandler) traceHandleRPC(ctx context.Context, rs stats.RPCStats) {
handleRPC(ctx, rs)
}
// HandleRPC processes the RPC stats, adding information to the current trace span.
func (s *serverTraceHandler) HandleRPC(ctx context.Context, rs stats.RPCStats) {
func (s *ServerHandler) traceHandleRPC(ctx context.Context, rs stats.RPCStats) {
handleRPC(ctx, rs)
}
@@ -84,16 +79,20 @@ func handleRPC(ctx context.Context, rs stats.RPCStats) {
switch rs := rs.(type) {
case *stats.Begin:
span.SetAttributes(
trace.BoolAttribute{Key: "Client", Value: rs.Client},
trace.BoolAttribute{Key: "FailFast", Value: rs.FailFast})
trace.BoolAttribute("Client", rs.Client),
trace.BoolAttribute("FailFast", rs.FailFast))
case *stats.InPayload:
span.AddMessageReceiveEvent(0 /* TODO: messageID */, int64(rs.Length), int64(rs.WireLength))
case *stats.OutPayload:
span.AddMessageSendEvent(0, int64(rs.Length), int64(rs.WireLength))
case *stats.End:
if rs.Error != nil {
code, desc := grpc.Code(rs.Error), grpc.ErrorDesc(rs.Error)
span.SetStatus(trace.Status{Code: int32(code), Message: desc})
s, ok := status.FromError(rs.Error)
if ok {
span.SetStatus(trace.Status{Code: int32(s.Code()), Message: s.Message()})
} else {
span.SetStatus(trace.Status{Code: int32(codes.Internal), Message: rs.Error.Error()})
}
}
span.End()
}

View File

@@ -21,7 +21,7 @@ import (
"testing"
"time"
testpb "go.opencensus.io/plugin/ocgrpc/testdata"
"go.opencensus.io/plugin/ocgrpc/internal/testpb"
"go.opencensus.io/trace"
"golang.org/x/net/context"
"google.golang.org/grpc"
@@ -124,7 +124,7 @@ func TestStreaming(t *testing.T) {
s1 := <-te.ch
s2 := <-te.ch
checkSpanData(t, s1, s2, ".testdata.Foo.Multiple", true)
checkSpanData(t, s1, s2, ".testpb.Foo.Multiple", true)
select {
case <-te.ch:
@@ -167,7 +167,7 @@ func TestStreamingFail(t *testing.T) {
s1 := <-te.ch
s2 := <-te.ch
checkSpanData(t, s1, s2, ".testdata.Foo.Multiple", false)
checkSpanData(t, s1, s2, ".testpb.Foo.Multiple", false)
cleanup()
select {
@@ -196,7 +196,7 @@ func TestSingle(t *testing.T) {
s1 := <-te.ch
s2 := <-te.ch
checkSpanData(t, s1, s2, ".testdata.Foo.Single", true)
checkSpanData(t, s1, s2, ".testpb.Foo.Single", true)
cleanup()
select {
@@ -225,7 +225,7 @@ func TestSingleFail(t *testing.T) {
s1 := <-te.ch
s2 := <-te.ch
checkSpanData(t, s1, s2, ".testdata.Foo.Single", false)
checkSpanData(t, s1, s2, ".testpb.Foo.Single", false)
cleanup()
select {