Beispiel #1
0
func TestProto3SetDefaults(t *testing.T) {
	in := &pb.Message{
		Terrain: map[string]*pb.Nested{
			"meadow": new(pb.Nested),
		},
		Proto2Field: new(tpb.SubDefaults),
		Proto2Value: map[string]*tpb.SubDefaults{
			"badlands": new(tpb.SubDefaults),
		},
	}

	got := proto.Clone(in).(*pb.Message)
	proto.SetDefaults(got)

	// There are no defaults in proto3.  Everything should be the zero value, but
	// we need to remember to set defaults for nested proto2 messages.
	want := &pb.Message{
		Terrain: map[string]*pb.Nested{
			"meadow": new(pb.Nested),
		},
		Proto2Field: &tpb.SubDefaults{N: proto.Int64(7)},
		Proto2Value: map[string]*tpb.SubDefaults{
			"badlands": {N: proto.Int64(7)},
		},
	}

	if !proto.Equal(got, want) {
		t.Errorf("with in = %v\nproto.SetDefaults(in) =>\ngot %v\nwant %v", in, got, want)
	}
}
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())
	}
}
func createStartStopMessage(requestId uint64, peerType events.PeerType) *events.Envelope {
	return &events.Envelope{
		Origin:    proto.String("fake-origin-2"),
		EventType: events.Envelope_HttpStartStop.Enum(),
		HttpStartStop: &events.HttpStartStop{
			StartTimestamp: proto.Int64(1),
			StopTimestamp:  proto.Int64(100),
			RequestId: &events.UUID{
				Low:  proto.Uint64(requestId),
				High: proto.Uint64(requestId + 1),
			},
			PeerType:      &peerType,
			Method:        events.Method_GET.Enum(),
			Uri:           proto.String("fake-uri-1"),
			RemoteAddress: proto.String("fake-remote-addr-1"),
			UserAgent:     proto.String("fake-user-agent-1"),
			StatusCode:    proto.Int32(103),
			ContentLength: proto.Int64(104),
			ParentRequestId: &events.UUID{
				Low:  proto.Uint64(2),
				High: proto.Uint64(3),
			},
			ApplicationId: &events.UUID{
				Low:  proto.Uint64(105),
				High: proto.Uint64(106),
			},
			InstanceIndex: proto.Int32(6),
			InstanceId:    proto.String("fake-instance-id-1"),
		},
	}
}
func NewHttpStartStop(req *http.Request, statusCode int, contentLength int64, peerType events.PeerType, requestId *uuid.UUID) *events.HttpStartStop {
	now := proto.Int64(time.Now().UnixNano())
	httpStartStop := &events.HttpStartStop{
		StartTimestamp: now,
		StopTimestamp:  now,
		RequestId:      NewUUID(requestId),
		PeerType:       &peerType,
		Method:         events.Method(events.Method_value[req.Method]).Enum(),
		Uri:            proto.String(fmt.Sprintf("%s://%s%s", scheme(req), req.Host, req.URL.Path)),
		RemoteAddress:  proto.String(req.RemoteAddr),
		UserAgent:      proto.String(req.UserAgent()),
		StatusCode:     proto.Int(statusCode),
		ContentLength:  proto.Int64(contentLength),
	}

	if applicationId, err := uuid.ParseHex(req.Header.Get("X-CF-ApplicationID")); err == nil {
		httpStartStop.ApplicationId = NewUUID(applicationId)
	}

	if instanceIndex, err := strconv.Atoi(req.Header.Get("X-CF-InstanceIndex")); err == nil {
		httpStartStop.InstanceIndex = proto.Int(instanceIndex)
	}

	if instanceId := req.Header.Get("X-CF-InstanceID"); instanceId != "" {
		httpStartStop.InstanceId = proto.String(instanceId)
	}

	allForwards := req.Header[http.CanonicalHeaderKey("X-Forwarded-For")]
	for _, forwarded := range allForwards {
		httpStartStop.Forwarded = append(httpStartStop.Forwarded, parseXForwarded(forwarded)...)
	}

	return httpStartStop
}
Beispiel #5
0
func createDomain(fields []string, in *pb.Domain) error {
	if len(fields) > 5 {
		return fmt.Errorf("Too many argumets, expected 4 got %d", len(fields))
	}

	// FIXME make last 2 arguments optional
	capa, err := strconv.Atoi(fields[3])
	if err != nil {
		return fmt.Errorf("Expected 3rd argument to be of type int: %q", err)
	}

	size, err := strconv.Atoi(fields[4])
	if err != nil {
		return fmt.Errorf("Expected last argument to be of type int: %q", err)
	}

	types := []pb.SketchType{pb.SketchType_MEMB, pb.SketchType_FREQ, pb.SketchType_RANK, pb.SketchType_CARD}
	for _, ty := range types {
		sketch := &pb.Sketch{}
		sketch.Name = proto.String("")
		sketch.Type = &ty
		sketch.Properties = &pb.SketchProperties{
			Size:           proto.Int64(int64(size)),
			MaxUniqueItems: proto.Int64(int64(capa)),
		}
		in.Sketches = append(in.Sketches, sketch)
	}

	_, err = client.CreateDomain(context.Background(), in)
	return err
}
func (d *DroppedCounter) encodeLogMessage(droppedCount int64) []byte {
	now := time.Now()
	message := &events.Envelope{
		Origin:     proto.String(d.origin),
		Timestamp:  proto.Int64(now.UnixNano()),
		Ip:         proto.String(d.ip),
		Deployment: proto.String(d.conf.Deployment),
		Index:      proto.String(d.conf.Index),
		Job:        proto.String(d.conf.Job),
		EventType:  events.Envelope_LogMessage.Enum(),
		LogMessage: &events.LogMessage{
			MessageType: events.LogMessage_ERR.Enum(),
			Timestamp:   proto.Int64(now.UnixNano()),
			AppId:       proto.String(envelope_extensions.SystemAppId),
			Message:     []byte(fmt.Sprintf("Dropped %d message(s) from MetronAgent to Doppler", droppedCount)),
			SourceType:  metSourceType,
		},
	}

	bytes, err := message.Marshal()
	if err != nil {
		d.incrementer.BatchIncrementCounter("droppedCounter.sendErrors")
		d.timer.Reset(time.Millisecond)
		return nil
	}

	return prefixMessage(bytes)
}
Beispiel #7
0
func TestCreateAddDeleteAddSketch(t *testing.T) {
	config.Reset()
	testutils.SetupTests()
	defer testutils.TearDownTests()

	client, conn := setupClient()
	defer tearDownClient(conn)

	typ := pb.SketchType_CARD
	name := "yoyo"

	in := &pb.Sketch{
		Name: proto.String(name),
		Type: &typ,
		Properties: &pb.SketchProperties{
			MaxUniqueItems: proto.Int64(1337), // FIXME: Allow default as -1
			Size:           proto.Int64(7),
		},
	}

	addReq := &pb.AddRequest{
		Sketch: in,
		Values: []string{"a", "b", "c", "d", "a", "b"},
	}

	if _, err := client.CreateSketch(context.Background(), in); err != nil {
		t.Error("Did not expect error, got", err)
	}

	typ = pb.SketchType_RANK
	if _, err := client.CreateSketch(context.Background(), in); err != nil {
		t.Error("Did not expect error, got", err)
	}

	if _, err := client.Add(context.Background(), addReq); err != nil {
		t.Error("Did not expect error, got", err)
	}

	if res, err := client.ListAll(context.Background(), &pb.Empty{}); err != nil {
		t.Error("Did not expect error, got", err)
	} else if len(res.GetSketches()) != 2 {
		t.Error("Expected len(res) == 2, got ", len(res.GetSketches()))
	}

	if _, err := client.DeleteSketch(context.Background(), in); err != nil {
		t.Error("Did not expect error, got", err)
	}

	if _, err := client.Add(context.Background(), addReq); err == nil {
		t.Error("Expected error, got", err)
	}

	if res, err := client.ListAll(context.Background(), &pb.Empty{}); err != nil {
		t.Error("Did not expect error, got", err)
	} else if len(res.GetSketches()) != 1 {
		t.Error("Expected len(res) == 1, got ", len(res.GetSketches()))
	}

}
Beispiel #8
0
func getSketchInfo(in *pb.Sketch) error {
	in.Properties = &pb.SketchProperties{
		MaxUniqueItems: proto.Int64(0),
		Size:           proto.Int64(0),
	}
	reply, err := client.GetSketch(context.Background(), in)
	fmt.Println(reply)
	return err
}
Beispiel #9
0
func TestAddGetFreqSketch(t *testing.T) {
	config.Reset()
	testutils.SetupTests()
	defer testutils.TearDownTests()

	client, conn := setupClient()
	defer tearDownClient(conn)

	typ := pb.SketchType_FREQ
	name := "yoyo"

	in := &pb.Sketch{
		Name: proto.String(name),
		Type: &typ,
		Properties: &pb.SketchProperties{
			MaxUniqueItems: proto.Int64(1337), // FIXME: Allow default as -1
			Size:           proto.Int64(7),
		},
	}

	if res, err := client.CreateSketch(context.Background(), in); err != nil {
		t.Error("Did not expect error, got", err)
	} else if res.GetName() != in.GetName() {
		t.Errorf("Expected name == %s, got %s", in.GetName(), res.GetName())
	} else if res.GetType() != in.GetType() {
		t.Errorf("Expected name == %q, got %q", in.GetType(), res.GetType())
	}

	addReq := &pb.AddRequest{
		Sketch: in,
		Values: []string{"a", "a", "b", "c", "d"},
	}

	expected := map[string]int64{
		"a": 2, "b": 1, "c": 1, "d": 1, "e": 0,
	}

	if _, err := client.Add(context.Background(), addReq); err != nil {
		t.Error("Did not expect error, got", err)
	}

	getReq := &pb.GetRequest{
		Sketches: []*pb.Sketch{in},
		Values:   []string{"a", "b", "c", "d", "e", "b"},
	}

	if res, err := client.GetFrequency(context.Background(), getReq); err != nil {
		t.Error("Did not expect error, got", err)
	} else {
		for _, v := range res.GetResults()[0].GetFrequencies() {
			if expected[v.GetValue()] != v.GetCount() {
				t.Errorf("Expected %s == %d, got", v.GetValue(), v.GetCount())
			}
		}
	}
}
// Send sends the log message with the envelope timestamp set to now and the
// log message timestamp set to now if none was provided by SetTimestamp.
func (c logChainer) Send() error {
	metrics.BatchIncrementCounter("logSenderTotalMessagesRead")

	c.envelope.Timestamp = proto.Int64(time.Now().UnixNano())

	if c.envelope.LogMessage.Timestamp == nil {
		c.envelope.LogMessage.Timestamp = proto.Int64(time.Now().UnixNano())
	}
	return c.emitter.EmitEnvelope(c.envelope)
}
// 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
}
Beispiel #12
0
func encodeIntegerPoint(p *IntegerPoint) *internal.Point {
	return &internal.Point{
		Name: proto.String(p.Name),
		Tags: proto.String(p.Tags.ID()),
		Time: proto.Int64(p.Time),
		Nil:  proto.Bool(p.Nil),
		Aux:  encodeAux(p.Aux),

		IntegerValue: proto.Int64(p.Value),
	}
}
Beispiel #13
0
// MarshalBinary encodes r to a binary format.
func (r *CreateIteratorResponse) MarshalBinary() ([]byte, error) {
	var pb internal.CreateIteratorResponse
	if r.Err != nil {
		pb.Err = proto.String(r.Err.Error())
	}
	pb.Type = proto.Int32(int32(r.Type))
	pb.Stats = &internal.IteratorStats{
		SeriesN: proto.Int64(int64(r.Stats.SeriesN)),
		PointN:  proto.Int64(int64(r.Stats.PointN)),
	}
	return proto.Marshal(&pb)
}
Beispiel #14
0
func init() {
	f := float64(0.123)
	if err := proto.SetExtension(AContainer, E_FieldA, &f); err != nil {
		panic(err)
	}
	if err := proto.SetExtension(AContainer, E_FieldB, &Small{SmallField: proto.Int64(456)}); err != nil {
		panic(err)
	}
	if err := proto.SetExtension(AContainer, E_FieldC, &Big{BigField: proto.Int64(789)}); err != nil {
		panic(err)
	}
}
Beispiel #15
0
func createLogEnvelope(message, appId string) *events.Envelope {
	return &events.Envelope{
		EventType: events.Envelope_LogMessage.Enum(),
		Origin:    proto.String(helpers.ORIGIN_NAME),
		Timestamp: proto.Int64(time.Now().UnixNano()),
		LogMessage: &events.LogMessage{
			Message:     []byte(message),
			MessageType: events.LogMessage_OUT.Enum(),
			Timestamp:   proto.Int64(time.Now().UnixNano()),
			AppId:       proto.String(appId),
		},
	}
}
func (f *FakeFirehose) SendLog(logMessage string) {
	f.addEvent(events.Envelope{
		Origin:    proto.String("origin"),
		Timestamp: proto.Int64(1000000000),
		EventType: events.Envelope_LogMessage.Enum(),
		LogMessage: &events.LogMessage{
			Message:     []byte(logMessage),
			MessageType: events.LogMessage_OUT.Enum(),
			Timestamp:   proto.Int64(1000000000),
		},
		Deployment: proto.String("deployment-name"),
		Job:        proto.String("doppler"),
	})
}
Beispiel #17
0
func sendLog(message, appID string) {
	envelope := &events.Envelope{
		EventType: events.Envelope_LogMessage.Enum(),
		Timestamp: proto.Int64(time.Now().UnixNano()),
		Origin:    proto.String(DEFAULT_ORIGIN),
		LogMessage: &events.LogMessage{
			MessageType: events.LogMessage_OUT.Enum(),
			Message:     []byte(message),
			Timestamp:   proto.Int64(time.Now().UnixNano()),
			AppId:       proto.String(appID),
		},
	}
	sendEnvelope(envelope)
}
Beispiel #18
0
func TestAddGetCardSketch(t *testing.T) {
	config.Reset()
	testutils.SetupTests()
	defer testutils.TearDownTests()

	client, conn := setupClient()
	defer tearDownClient(conn)

	typ := pb.SketchType_CARD
	name := "yoyo"

	in := &pb.Sketch{
		Name: proto.String(name),
		Type: &typ,
		Properties: &pb.SketchProperties{
			MaxUniqueItems: proto.Int64(1337), // FIXME: Allow default as -1
			Size:           proto.Int64(7),
		},
	}

	if res, err := client.CreateSketch(context.Background(), in); err != nil {
		t.Error("Did not expect error, got", err)
	} else if res.GetName() != in.GetName() {
		t.Errorf("Expected name == %s, got %s", in.GetName(), res.GetName())
	} else if res.GetType() != in.GetType() {
		t.Errorf("Expected name == %q, got %q", in.GetType(), res.GetType())
	}

	addReq := &pb.AddRequest{
		Sketch: in,
		Values: []string{"a", "b", "c", "d", "a", "b"},
	}

	if _, err := client.Add(context.Background(), addReq); err != nil {
		t.Error("Did not expect error, got", err)
	}

	getReq := &pb.GetRequest{
		Sketches: []*pb.Sketch{in},
		Values:   []string{},
	}

	if res, err := client.GetCardinality(context.Background(), getReq); err != nil {
		t.Error("Did not expect error, got", err)

	} else if res.GetResults()[0].GetCardinality() != 4 {
		t.Error("Expected cardinality 4, got", res.GetResults()[0].GetCardinality())
	}
}
Beispiel #19
0
// marshal serializes to a protobuf representation.
func (sgi *ShardGroupInfo) marshal() *internal.ShardGroupInfo {
	pb := &internal.ShardGroupInfo{
		ID:        proto.Uint64(sgi.ID),
		StartTime: proto.Int64(MarshalTime(sgi.StartTime)),
		EndTime:   proto.Int64(MarshalTime(sgi.EndTime)),
		DeletedAt: proto.Int64(MarshalTime(sgi.DeletedAt)),
	}

	pb.Shards = make([]*internal.ShardInfo, len(sgi.Shards))
	for i := range sgi.Shards {
		pb.Shards[i] = sgi.Shards[i].marshal()
	}

	return pb
}
Beispiel #20
0
func basicHTTPStopMessageEnvelope(requestId string) *events.Envelope {
	uuid, _ := uuid.ParseHex(requestId)
	return &events.Envelope{
		Origin:    proto.String("fake-origin-2"),
		EventType: events.Envelope_HttpStop.Enum(),
		HttpStop: &events.HttpStop{
			Timestamp:     proto.Int64(12),
			Uri:           proto.String("some uri"),
			RequestId:     factories.NewUUID(uuid),
			PeerType:      events.PeerType_Client.Enum(),
			StatusCode:    proto.Int32(404),
			ContentLength: proto.Int64(98475189),
		},
	}
}
Beispiel #21
0
// marshal serializes to a protobuf representation.
func (rpi *RetentionPolicyInfo) marshal() *internal.RetentionPolicyInfo {
	pb := &internal.RetentionPolicyInfo{
		Name:               proto.String(rpi.Name),
		ReplicaN:           proto.Uint32(uint32(rpi.ReplicaN)),
		Duration:           proto.Int64(int64(rpi.Duration)),
		ShardGroupDuration: proto.Int64(int64(rpi.ShardGroupDuration)),
	}

	pb.ShardGroups = make([]*internal.ShardGroupInfo, len(rpi.ShardGroups))
	for i, sgi := range rpi.ShardGroups {
		pb.ShardGroups[i] = sgi.marshal()
	}

	return pb
}
func basicHTTPStopEventEnvelope() *events.Envelope {
	uuid, _ := uuid.ParseHex("9f45fa9d-dbf8-463e-425f-a79d30e1b56f")
	return &events.Envelope{
		Origin:    proto.String("fake-origin-2"),
		EventType: events.Envelope_HttpStop.Enum(),
		HttpStop: &events.HttpStop{
			Timestamp:     proto.Int64(12),
			Uri:           proto.String("some uri"),
			RequestId:     factories.NewUUID(uuid),
			PeerType:      events.PeerType_Client.Enum(),
			StatusCode:    proto.Int32(404),
			ContentLength: proto.Int64(98475189),
		},
	}
}
Beispiel #23
0
func NewHttpStartStop(req *http.Request, statusCode int, contentLength int64, peerType events.PeerType, requestId *uuid.UUID) *events.HttpStartStop {
	httpStartStop := &events.HttpStartStop{
		StartTimestamp: proto.Int64(time.Now().UnixNano()),
		StopTimestamp:  proto.Int64(time.Now().UnixNano()),
		RequestId:      NewUUID(requestId),
		PeerType:       &peerType,
		Method:         events.Method(events.Method_value[req.Method]).Enum(),
		Uri:            proto.String(fmt.Sprintf("%s%s", req.Host, req.URL.Path)),
		RemoteAddress:  proto.String(req.RemoteAddr),
		UserAgent:      proto.String(req.UserAgent()),
		StatusCode:     proto.Int(statusCode),
		ContentLength:  proto.Int64(contentLength),
	}
	return httpStartStop
}
Beispiel #24
0
func (w *WriteShardRequest) marshalPoints(points []tsdb.Point) []*internal.Point {
	pts := make([]*internal.Point, len(points))
	for i, p := range points {
		fields := []*internal.Field{}
		for k, v := range p.Fields() {
			name := k
			f := &internal.Field{
				Name: &name,
			}
			switch t := v.(type) {
			case int:
				f.Int64 = proto.Int64(int64(t))
			case int32:
				f.Int32 = proto.Int32(t)
			case int64:
				f.Int64 = proto.Int64(t)
			case float64:
				f.Float64 = proto.Float64(t)
			case bool:
				f.Bool = proto.Bool(t)
			case string:
				f.String_ = proto.String(t)
			case []byte:
				f.Bytes = t
			}
			fields = append(fields, f)
		}

		tags := []*internal.Tag{}
		for k, v := range p.Tags() {
			key := k
			value := v
			tags = append(tags, &internal.Tag{
				Key:   &key,
				Value: &value,
			})
		}
		name := p.Name()
		pts[i] = &internal.Point{
			Name:   &name,
			Time:   proto.Int64(p.Time().UnixNano()),
			Fields: fields,
			Tags:   tags,
		}

	}
	return pts
}
Beispiel #25
0
func encodeAux(aux []interface{}) []*internal.Aux {
	pb := make([]*internal.Aux, len(aux))
	for i := range aux {
		switch v := aux[i].(type) {
		case float64:
			pb[i] = &internal.Aux{DataType: proto.Int32(Float), FloatValue: proto.Float64(v)}
		case *float64:
			pb[i] = &internal.Aux{DataType: proto.Int32(Float)}
		case int64:
			pb[i] = &internal.Aux{DataType: proto.Int32(Integer), IntegerValue: proto.Int64(v)}
		case *int64:
			pb[i] = &internal.Aux{DataType: proto.Int32(Integer)}
		case string:
			pb[i] = &internal.Aux{DataType: proto.Int32(String), StringValue: proto.String(v)}
		case *string:
			pb[i] = &internal.Aux{DataType: proto.Int32(String)}
		case bool:
			pb[i] = &internal.Aux{DataType: proto.Int32(Boolean), BooleanValue: proto.Bool(v)}
		case *bool:
			pb[i] = &internal.Aux{DataType: proto.Int32(Boolean)}
		default:
			pb[i] = &internal.Aux{DataType: proto.Int32(int32(Unknown))}
		}
	}
	return pb
}
Beispiel #26
0
func transformProto(message string, host string) interface{} {
	Logger.Info("proto transform")

	logLine := new(pb.LogLine) //TODO set logtypeid, source, timings
	logLine.Line = proto.String(message)
	logLine.Logtypeid = proto.Int64(0)
	logLine.Source = proto.String(host)
	timing := &pb.LogLine_Timing{Value: proto.Int64(time.Now().UnixNano()), EventName: proto.String("received")}
	logLine.Timings = []*pb.LogLine_Timing{timing}

	serialized, err := proto.Marshal(logLine)
	if err != nil {
		Logger.Errorf("Proto marshal error: %s", err) //TODO what should we do?
	}
	return serialized
}
/*
Wraps the given http.Handler ServerHTTP function
Will provide accounting metrics for the http.Request / http.Response life-cycle
*/
func (ih *instrumentedHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
	requestId, err := uuid.ParseHex(req.Header.Get("X-Vcap-Request-Id"))
	if err != nil {
		requestId, err = GenerateUuid()
		if err != nil {
			log.Printf("failed to generated request ID: %v\n", err)
			requestId = &uuid.UUID{}
		}
		req.Header.Set("X-Vcap-Request-Id", requestId.String())
	}
	rw.Header().Set("X-Vcap-Request-Id", requestId.String())

	startTime := time.Now()

	instrumentedWriter := &instrumentedResponseWriter{writer: rw, statusCode: 200}
	ih.handler.ServeHTTP(instrumentedWriter, req)

	startStopEvent := factories.NewHttpStartStop(req, instrumentedWriter.statusCode, instrumentedWriter.contentLength, events.PeerType_Server, requestId)
	startStopEvent.StartTimestamp = proto.Int64(startTime.UnixNano())

	err = ih.emitter.Emit(startStopEvent)
	if err != nil {
		log.Printf("failed to emit startstop event: %v\n", err)
	}
}
Beispiel #28
0
func NewHttpStop(req *http.Request, statusCode int, contentLength int64, peerType events.PeerType, requestId *uuid.UUID) *events.HttpStop {
	httpStop := &events.HttpStop{
		Timestamp:     proto.Int64(time.Now().UnixNano()),
		Uri:           proto.String(fmt.Sprintf("%s%s", req.Host, req.URL.Path)),
		RequestId:     NewUUID(requestId),
		PeerType:      &peerType,
		StatusCode:    proto.Int(statusCode),
		ContentLength: proto.Int64(contentLength),
	}

	if applicationId, err := uuid.ParseHex(req.Header.Get("X-CF-ApplicationID")); err == nil {
		httpStop.ApplicationId = NewUUID(applicationId)
	}

	return httpStop
}
Beispiel #29
0
func NewHttpStart(req *http.Request, peerType events.PeerType, requestId *uuid.UUID) *events.HttpStart {
	httpStart := &events.HttpStart{
		Timestamp:     proto.Int64(time.Now().UnixNano()),
		RequestId:     NewUUID(requestId),
		PeerType:      &peerType,
		Method:        events.Method(events.Method_value[req.Method]).Enum(),
		Uri:           proto.String(fmt.Sprintf("%s%s", req.Host, req.URL.Path)),
		RemoteAddress: proto.String(req.RemoteAddr),
		UserAgent:     proto.String(req.UserAgent()),
	}

	if applicationId, err := uuid.ParseHex(req.Header.Get("X-CF-ApplicationID")); err == nil {
		httpStart.ApplicationId = NewUUID(applicationId)
	}

	if instanceIndex, err := strconv.Atoi(req.Header.Get("X-CF-InstanceIndex")); err == nil {
		httpStart.InstanceIndex = proto.Int(instanceIndex)
	}

	if instanceId := req.Header.Get("X-CF-InstanceID"); instanceId != "" {
		httpStart.InstanceId = &instanceId
	}

	return httpStart
}
Beispiel #30
0
// marshal serializes to a protobuf representation.
func (s *RetentionPolicySpec) marshal() *internal.RetentionPolicySpec {
	pb := &internal.RetentionPolicySpec{}
	if s.Name != "" {
		pb.Name = proto.String(s.Name)
	}
	if s.Duration != nil {
		pb.Duration = proto.Int64(int64(*s.Duration))
	}
	if s.ShardGroupDuration > 0 {
		pb.ShardGroupDuration = proto.Int64(int64(s.ShardGroupDuration))
	}
	if s.ReplicaN != nil {
		pb.ReplicaN = proto.Uint32(uint32(*s.ReplicaN))
	}
	return pb
}