Ejemplo n.º 1
0
// Code taken from github.com/bigdatadev/goryman
func EventToPbEvent(event *Event) (*proto.Event, error) {
	var e proto.Event
	t := reflect.ValueOf(&e).Elem()
	s := reflect.ValueOf(event).Elem()
	typeOfEvent := s.Type()

	for i := 0; i < s.NumField(); i++ {
		f := s.Field(i)
		value := reflect.ValueOf(f.Interface())
		if reflect.Zero(f.Type()) != value && f.Interface() != nil {
			name := typeOfEvent.Field(i).Name
			switch name {
			case "State", "Service", "Host", "Description":
				tmp := reflect.ValueOf(pb.String(value.String()))
				t.FieldByName(name).Set(tmp)
			case "Ttl":
				tmp := reflect.ValueOf(pb.Float32(float32(value.Float())))
				t.FieldByName(name).Set(tmp)
			case "Time":
				tmp := reflect.ValueOf(pb.Int64(value.Int()))
				t.FieldByName(name).Set(tmp)
			case "Tags":
				tmp := reflect.ValueOf(value.Interface().([]string))
				t.FieldByName(name).Set(tmp)
			case "Metric":
				switch reflect.TypeOf(f.Interface()).Kind() {
				case reflect.Int:
					tmp := reflect.ValueOf(pb.Int64(int64(value.Int())))
					t.FieldByName("MetricSint64").Set(tmp)
				case reflect.Float32:
					tmp := reflect.ValueOf(pb.Float32(float32(value.Float())))
					t.FieldByName("MetricF").Set(tmp)
				case reflect.Float64:
					tmp := reflect.ValueOf(pb.Float64(value.Float()))
					t.FieldByName("MetricD").Set(tmp)
				default:
					return nil, fmt.Errorf("Metric of invalid type (type %v)",
						reflect.TypeOf(f.Interface()).Kind())
				}
			case "Attributes":
				var attrs []*proto.Attribute
				for k, v := range value.Interface().(map[string]string) {
					k_, v_ := k, v
					attrs = append(attrs, &proto.Attribute{
						Key:   &k_,
						Value: &v_,
					})
				}
				t.FieldByName(name).Set(reflect.ValueOf(attrs))
			}
		}
	}
	return &e, nil
}
func TestUnmarshalPartiallyPopulatedOptionalFieldsFails(t *testing.T) {
	// Fill in all fields, then randomly remove one.
	dataOut := &test.NinOptNative{
		Field1:  proto.Float64(0),
		Field2:  proto.Float32(0),
		Field3:  proto.Int32(0),
		Field4:  proto.Int64(0),
		Field5:  proto.Uint32(0),
		Field6:  proto.Uint64(0),
		Field7:  proto.Int32(0),
		Field8:  proto.Int64(0),
		Field9:  proto.Uint32(0),
		Field10: proto.Int32(0),
		Field11: proto.Uint64(0),
		Field12: proto.Int64(0),
		Field13: proto.Bool(false),
		Field14: proto.String("0"),
		Field15: []byte("0"),
	}
	r := rand.New(rand.NewSource(time.Now().UnixNano()))
	fieldName := "Field" + strconv.Itoa(r.Intn(15)+1)
	field := reflect.ValueOf(dataOut).Elem().FieldByName(fieldName)
	fieldType := field.Type()
	field.Set(reflect.Zero(fieldType))
	encodedMessage, err := proto.Marshal(dataOut)
	if err != nil {
		t.Fatalf("Unexpected error when marshalling dataOut: %v", err)
	}
	dataIn := NidOptNative{}
	err = proto.Unmarshal(encodedMessage, &dataIn)
	if err.Error() != `proto: required field "`+fieldName+`" not set` {
		t.Fatalf(`err.Error() != "proto: required field "`+fieldName+`" not set"; was "%s" instead`, err.Error())
	}
}
Ejemplo n.º 3
0
func newTestMessage() *pb.MyMessage {
	msg := &pb.MyMessage{
		Count: proto.Int32(42),
		Name:  proto.String("Dave"),
		Quote: proto.String(`"I didn't want to go."`),
		Pet:   []string{"bunny", "kitty", "horsey"},
		Inner: &pb.InnerMessage{
			Host:      proto.String("footrest.syd"),
			Port:      proto.Int32(7001),
			Connected: proto.Bool(true),
		},
		Others: []*pb.OtherMessage{
			{
				Key:   proto.Int64(0xdeadbeef),
				Value: []byte{1, 65, 7, 12},
			},
			{
				Weight: proto.Float32(6.022),
				Inner: &pb.InnerMessage{
					Host: proto.String("lesha.mtv"),
					Port: proto.Int32(8002),
				},
			},
		},
		Bikeshed: pb.MyMessage_BLUE.Enum(),
		Somegroup: &pb.MyMessage_SomeGroup{
			GroupField: proto.Int32(8),
		},
		// One normally wouldn't do this.
		// This is an undeclared tag 13, as a varint (wire type 0) with value 4.
		XXX_unrecognized: []byte{13<<3 | 0, 4},
	}
	ext := &pb.Ext{
		Data: proto.String("Big gobs for big rats"),
	}
	if err := proto.SetExtension(msg, pb.E_Ext_More, ext); err != nil {
		panic(err)
	}
	greetings := []string{"adg", "easy", "cow"}
	if err := proto.SetExtension(msg, pb.E_Greeting, greetings); err != nil {
		panic(err)
	}

	// Add an unknown extension. We marshal a pb.Ext, and fake the ID.
	b, err := proto.Marshal(&pb.Ext{Data: proto.String("3G skiing")})
	if err != nil {
		panic(err)
	}
	b = append(proto.EncodeVarint(201<<3|proto.WireBytes), b...)
	proto.SetRawExtension(msg, 201, b)

	// Extensions can be plain fields, too, so let's test that.
	b = append(proto.EncodeVarint(202<<3|proto.WireVarint), 19)
	proto.SetRawExtension(msg, 202, b)

	return msg
}
Ejemplo n.º 4
0
// timeSeriesFloat generates a simple InternalTimeSeriesData object which starts
// at the given timestamp and has samples of the given duration. Samples have
// float values.
func timeSeriesFloat(start int64, duration int64, samples ...tsFloatSample) []byte {
	ts := &proto.InternalTimeSeriesData{
		StartTimestampNanos: start,
		SampleDurationNanos: duration,
	}
	for _, sample := range samples {
		newSample := &proto.InternalTimeSeriesSample{
			Offset:     sample.offset,
			FloatCount: sample.count,
			FloatSum:   gogoproto.Float32(sample.sum),
		}
		if sample.count > 1 {
			newSample.FloatMax = gogoproto.Float32(sample.max)
			newSample.FloatMin = gogoproto.Float32(sample.min)
		}
		ts.Samples = append(ts.Samples, newSample)
	}
	v, err := ts.ToValue()
	if err != nil {
		panic(err)
	}
	return mustMarshal(&proto.MVCCMetadata{Value: v})
}
Ejemplo n.º 5
0
var (
	marshaller = Marshaller{}

	marshallerAllOptions = Marshaller{
		EnumsAsString: true,
		Indent:        "  ",
	}

	simpleObject = &pb.Simple{
		OInt32:  proto.Int32(-32),
		OInt64:  proto.Int64(-6400000000),
		OUint32: proto.Uint32(32),
		OUint64: proto.Uint64(6400000000),
		OSint32: proto.Int32(-13),
		OSint64: proto.Int64(-2600000000),
		OFloat:  proto.Float32(3.14),
		ODouble: proto.Float64(6.02214179e23),
		OBool:   proto.Bool(true),
		OString: proto.String("hello \"there\""),
		OBytes:  []byte("beep boop"),
	}

	simpleObjectJSON = `{` +
		`"o_bool":true,` +
		`"o_int32":-32,` +
		`"o_int64":"-6400000000",` +
		`"o_uint32":32,` +
		`"o_uint64":"6400000000",` +
		`"o_sint32":-13,` +
		`"o_sint64":"-2600000000",` +
		`"o_float":3.14,` +
Ejemplo n.º 6
0
func GatherServiceStats() (*badoo_service.ResponseStats, error) {
	ru, err := Getrusage(syscall.RUSAGE_SELF)
	if nil != err {
		return nil, fmt.Errorf("getrusage: %v", err)
	}

	// ports stats first
	ports := make([]*badoo_service.ResponseStatsPortStats, len(StartedServers))

	i := 0
	total_connections := uint32(0)
	for _, srv := range StartedServers {
		port_stats := &badoo_service.ResponseStatsPortStats{}

		stats := srv.Server.Stats

		// listen queue information
		unacked, sacked, err := GetLqInfo(srv)
		if err == nil {
			port_stats.LqCur = proto.Uint32(unacked)
			port_stats.LqMax = proto.Uint32(sacked)
		}

		port_connections := atomic.LoadUint64(&stats.ConnCur)
		total_connections += uint32(port_connections)

		// general stats
		port_stats.Proto = proto.String(srv.Name)
		port_stats.Address = proto.String(srv.Address)
		port_stats.ConnCur = proto.Uint64(port_connections)
		port_stats.ConnTotal = proto.Uint64(atomic.LoadUint64(&stats.ConnTotal))
		port_stats.Requests = proto.Uint64(atomic.LoadUint64(&stats.Requests))
		port_stats.BytesRead = proto.Uint64(atomic.LoadUint64(&stats.BytesRead))
		port_stats.BytesWritten = proto.Uint64(atomic.LoadUint64(&stats.BytesWritten))

		// per request stats
		port_stats.RequestStats = make([]*badoo_service.ResponseStatsPortStatsRequestStatsT, 0, len(badoo_service.RequestMsgid_name))
		for msg_id, msg_name := range srv.Server.Proto.GetRequestIdToNameMap() {
			port_stats.RequestStats = append(port_stats.RequestStats, &badoo_service.ResponseStatsPortStatsRequestStatsT{
				Name:  proto.String(msg_name),
				Count: proto.Uint64(atomic.LoadUint64(&stats.RequestsIdStat[msg_id])),
			})
		}

		ports[i] = port_stats
		i++
	}

	r := &badoo_service.ResponseStats{
		Uptime: proto.Uint32(uint32(time.Since(GetStartupTime()).Seconds())),
		RusageSelf: &badoo_service.ResponseStatsRusage{
			RuUtime:   proto.Float32(timevalToFloat32(&ru.Utime)),
			RuStime:   proto.Float32(timevalToFloat32(&ru.Stime)),
			RuMaxrss:  proto.Uint64(uint64(ru.Maxrss)),
			RuMinflt:  proto.Uint64(uint64(ru.Minflt)),
			RuMajflt:  proto.Uint64(uint64(ru.Majflt)),
			RuInblock: proto.Uint64(uint64(ru.Inblock)),
			RuOublock: proto.Uint64(uint64(ru.Oublock)),
			RuNvcsw:   proto.Uint64(uint64(ru.Nvcsw)),
			RuNivcsw:  proto.Uint64(uint64(ru.Nivcsw)),
		},
		Ports:             ports,
		Connections:       proto.Uint32(total_connections),
		InitPhaseDuration: proto.Uint32(uint32(GetInitPhaseDuration().Seconds())),
	}

	return r, nil
}
Ejemplo n.º 7
0
func setAnyValue(value interface{}, m *Any) (err error) {
	defer func() {
		if r := recover(); r != nil {
			switch x := r.(type) {
			case runtime.Error:
				panic(r)
			case string:
				err = errors.New(x)
			case error:
				err = x
			default:
				err = errors.New("Unknown panic")
			}
		}
	}()
	if m != nil {
		if m.Type != nil {
			m.Reset()
		}
		var val reflect.Value
		if v, ok := value.(reflect.Value); ok {
			val = v
		} else if v, ok := value.(*Any); ok {
			if v != nil {
				*m = *v
			} else {
				anyType := Any_NilType
				m.Type = &anyType
			}
			return
		} else {
			val = reflect.ValueOf(value)
		}
		if !val.IsValid() {
			anyType := Any_NilType
			m.Type = &anyType
			return
		}
		kind := val.Kind()
		if (kind == reflect.Chan || kind == reflect.Func || kind == reflect.Interface || kind == reflect.Map || kind == reflect.Ptr || kind == reflect.Slice) && val.IsNil() {
			anyType := Any_NilType
			m.Type = &anyType
			return
		}
		if kind == reflect.Ptr {
			kind = val.Elem().Kind()
		}
		switch kind {
		case reflect.Interface:
			err = setAnyValue(val.Interface(), m)
		case reflect.Struct:
			if val.Type().String() == "time.Time" {
				t := val.Interface()
				if v, ok := t.(time.Time); ok {
					tStr := v.Format(time.RFC3339Nano)
					err = setData(reflect.ValueOf(&m.StringValue), Any_TimeType, m, reflect.ValueOf(&tStr))
					break
				}
			}
			err = errors.New("Error: Unsupported value type")
		case reflect.String:
			if kind != reflect.Ptr {
				val = reflect.ValueOf(proto.String(val.String()))
			}
			err = setData(reflect.ValueOf(&m.StringValue), Any_StringType, m, val)
		case reflect.Uint:
			if kind != reflect.Ptr {
				val = reflect.ValueOf(proto.Uint64(val.Uint()))
			}
			err = setData(reflect.ValueOf(&m.Uint64Value), Any_UintType, m, val)
		case reflect.Uint32:
			if kind != reflect.Ptr {
				val = reflect.ValueOf(proto.Uint32(uint32(val.Uint())))
			}
			err = setData(reflect.ValueOf(&m.Uint32Value), Any_Uint32Type, m, val)
		case reflect.Uint64:
			if kind != reflect.Ptr {
				val = reflect.ValueOf(proto.Uint64(val.Uint()))
			}
			err = setData(reflect.ValueOf(&m.Uint64Value), Any_Uint64Type, m, val)
		case reflect.Int:
			if kind != reflect.Ptr {
				val = reflect.ValueOf(proto.Int64(val.Int()))
			}
			err = setData(reflect.ValueOf(&m.Int64Value), Any_IntType, m, val)
		case reflect.Int32:
			if kind != reflect.Ptr {
				val = reflect.ValueOf(proto.Int32(int32(val.Int())))
			}
			err = setData(reflect.ValueOf(&m.Int32Value), Any_Int32Type, m, val)
		case reflect.Int64:
			if kind != reflect.Ptr {
				val = reflect.ValueOf(proto.Int64(val.Int()))
			}
			err = setData(reflect.ValueOf(&m.Int64Value), Any_Int64Type, m, val)
		case reflect.Float32:
			if kind != reflect.Ptr {
				val = reflect.ValueOf(proto.Float32(float32(val.Float())))
			}
			err = setData(reflect.ValueOf(&m.Float32Value), Any_Float32Type, m, val)
		case reflect.Float64:
			if kind != reflect.Ptr {
				val = reflect.ValueOf(proto.Float64(val.Float()))
			}
			err = setData(reflect.ValueOf(&m.Float64Value), Any_Float64Type, m, val)
		case reflect.Bool:
			if kind != reflect.Ptr {
				val = reflect.ValueOf(proto.Bool(val.Bool()))
			}
			err = setData(reflect.ValueOf(&m.BoolValue), Any_BoolType, m, val)
		case reflect.Slice:
			if val.Type() == typeOfBytes {
				err = setData(reflect.ValueOf(&m.ByteValue), Any_ByteType, m, val)
				break
			}
			err = errors.New("Error: Unsupported value type")
		default:
			err = errors.New("Error: Unsupported value type")
		}
	}
	return
}
Ejemplo n.º 8
0
// TestToInternal verifies the conversion of TimeSeriesData to internal storage
// format is correct.
func TestToInternal(t *testing.T) {
	tcases := []struct {
		keyDuration    int64
		sampleDuration int64
		expectsError   bool
		input          *TimeSeriesData
		expected       []*InternalTimeSeriesData
	}{
		{
			time.Minute.Nanoseconds(),
			101,
			true,
			ts("error.series"),
			nil,
		},
		{
			time.Minute.Nanoseconds(),
			time.Hour.Nanoseconds(),
			true,
			ts("error.series"),
			nil,
		},
		{
			time.Hour.Nanoseconds(),
			time.Second.Nanoseconds(),
			true,
			ts("error.series",
				tsdpi((time.Hour*50)+(time.Second*5), 1),
				&TimeSeriesDatapoint{},
			),
			nil,
		},
		{
			time.Hour.Nanoseconds(),
			time.Second.Nanoseconds(),
			true,
			ts("error.series",
				tsdpi((time.Hour*50)+(time.Second*5), 1),
				&TimeSeriesDatapoint{
					IntValue:   gogoproto.Int64(5),
					FloatValue: gogoproto.Float32(5.0),
				},
			),
			nil,
		},
		{
			time.Hour.Nanoseconds(),
			time.Second.Nanoseconds(),
			false,
			ts("test.series",
				tsdpi((time.Hour*50)+(time.Second*5), 1),
				tsdpi((time.Hour*51)+(time.Second*3), 2),
				tsdpi((time.Hour*50)+(time.Second*10), 3),
				tsdpi((time.Hour*53), 4),
				tsdpi((time.Hour*50)+(time.Second*5)+1, 5),
				tsdpi((time.Hour*53)+(time.Second*15), 0),
			),
			[]*InternalTimeSeriesData{
				{
					StartTimestampNanos: int64(time.Hour * 50),
					SampleDurationNanos: int64(time.Second),
					Samples: []*InternalTimeSeriesSample{
						{
							Offset:   5,
							IntCount: 1,
							IntSum:   gogoproto.Int64(1),
						},
						{
							Offset:   10,
							IntCount: 1,
							IntSum:   gogoproto.Int64(3),
						},
						{
							Offset:   5,
							IntCount: 1,
							IntSum:   gogoproto.Int64(5),
						},
					},
				},
				{
					StartTimestampNanos: int64(time.Hour * 51),
					SampleDurationNanos: int64(time.Second),
					Samples: []*InternalTimeSeriesSample{
						{
							Offset:   3,
							IntCount: 1,
							IntSum:   gogoproto.Int64(2),
						},
					},
				},
				{
					StartTimestampNanos: int64(time.Hour * 53),
					SampleDurationNanos: int64(time.Second),
					Samples: []*InternalTimeSeriesSample{
						{
							Offset:   0,
							IntCount: 1,
							IntSum:   gogoproto.Int64(4),
						},
						{
							Offset:   15,
							IntCount: 1,
							IntSum:   gogoproto.Int64(0),
						},
					},
				},
			},
		},
		{
			(time.Hour * 24).Nanoseconds(),
			(time.Minute * 20).Nanoseconds(),
			false,
			ts("test.series",
				tsdpf((time.Hour*5)+(time.Minute*5), 1.0),
				tsdpf((time.Hour*24)+(time.Minute*39), 2.0),
				tsdpf((time.Hour*10)+(time.Minute*10), 3.0),
				tsdpf((time.Hour*48), 4.0),
				tsdpf((time.Hour*15)+(time.Minute*22)+1, 5.0),
				tsdpf((time.Hour*52)+(time.Minute*15), 0.0),
			),
			[]*InternalTimeSeriesData{
				{
					StartTimestampNanos: 0,
					SampleDurationNanos: int64(time.Minute * 20),
					Samples: []*InternalTimeSeriesSample{
						{
							Offset:     15,
							FloatCount: 1,
							FloatSum:   gogoproto.Float32(1.0),
						},
						{
							Offset:     30,
							FloatCount: 1,
							FloatSum:   gogoproto.Float32(3.0),
						},
						{
							Offset:     46,
							FloatCount: 1,
							FloatSum:   gogoproto.Float32(5.0),
						},
					},
				},
				{
					StartTimestampNanos: int64(time.Hour * 24),
					SampleDurationNanos: int64(time.Minute * 20),
					Samples: []*InternalTimeSeriesSample{
						{
							Offset:     1,
							FloatCount: 1,
							FloatSum:   gogoproto.Float32(2.0),
						},
					},
				},
				{
					StartTimestampNanos: int64(time.Hour * 48),
					SampleDurationNanos: int64(time.Minute * 20),
					Samples: []*InternalTimeSeriesSample{
						{
							Offset:     0,
							FloatCount: 1,
							FloatSum:   gogoproto.Float32(4.0),
						},
						{
							Offset:     12,
							FloatCount: 1,
							FloatSum:   gogoproto.Float32(0.0),
						},
					},
				},
			},
		},
	}

	for i, tc := range tcases {
		actual, err := tc.input.ToInternal(tc.keyDuration, tc.sampleDuration)
		if err != nil {
			if !tc.expectsError {
				t.Errorf("unexpected error from case %d: %s", i, err.Error())
			}
			continue
		} else if tc.expectsError {
			t.Errorf("expected error from case %d, none encountered", i)
			continue
		}

		if !reflect.DeepEqual(actual, tc.expected) {
			t.Errorf("case %d fails: ToInternal result was %v, expected %v", i, actual, tc.expected)
		}
	}
}
Ejemplo n.º 9
0
func tsdpf(ts time.Duration, val float32) *TimeSeriesDatapoint {
	return &TimeSeriesDatapoint{
		TimestampNanos: int64(ts),
		FloatValue:     gogoproto.Float32(val),
	}
}