diff --git a/api/rpc/logs/logs.go b/api/rpc/logs/logs.go index 76680518f..0cfdac86e 100644 --- a/api/rpc/logs/logs.go +++ b/api/rpc/logs/logs.go @@ -42,24 +42,8 @@ func (logs *Logs) Get(ctx context.Context, in *GetRequest) (*GetReply, error) { } masterQuery := elastic.NewBoolQuery() - if in.ServiceId != "" { - masterQuery.Must(elastic.NewPrefixQuery("service_id", in.ServiceId)) - } - if in.ServiceName != "" { - masterQuery.Must(elastic.NewPrefixQuery("service_name", in.ServiceName)) - } - if in.ContainerId != "" { - masterQuery.Must(elastic.NewPrefixQuery("container_id", in.ContainerId)) - } - if in.NodeId != "" { - masterQuery.Must(elastic.NewPrefixQuery("node_id", in.NodeId)) - } - if in.ServiceIsh != "" { - queryString := elastic.NewQueryStringQuery(in.ServiceIsh + "*") - queryString.Field("service_id") - queryString.Field("service_name") - queryString.AnalyzeWildcard(true) - masterQuery.Must(queryString) + if in.Container != "" { + masterQuery.Must(elastic.NewPrefixQuery("container_id", in.Container)) } if in.Message != "" { queryString := elastic.NewQueryStringQuery(in.Message + "*") @@ -67,6 +51,17 @@ func (logs *Logs) Get(ctx context.Context, in *GetRequest) (*GetReply, error) { queryString.AnalyzeWildcard(true) masterQuery.Must(queryString) } + if in.Node != "" { + masterQuery.Must(elastic.NewPrefixQuery("node_id", in.Node)) + } + if in.Service != "" { + masterQuery.Should(elastic.NewPrefixQuery("service_id", in.Service)) + masterQuery.Should(elastic.NewPrefixQuery("service_name", in.Service)) + } + if in.Stack != "" { + masterQuery.Should(elastic.NewPrefixQuery("stack_id", in.Stack)) + masterQuery.Should(elastic.NewPrefixQuery("stack_name", in.Stack)) + } // TODO timestamp queries // Perform request @@ -134,25 +129,24 @@ func parseProtoLogEntry(data []byte) (logEntry LogEntry, err error) { func filter(entry *LogEntry, in *GetRequest) bool { match := true - if in.ServiceId != "" { - match = strings.EqualFold(entry.ServiceId, in.ServiceId) + if in.Container != "" { + match = strings.HasPrefix(strings.ToLower(entry.ContainerId), strings.ToLower(in.Container)) } - if in.ServiceName != "" { - match = strings.EqualFold(entry.ServiceName, in.ServiceName) - } - if in.ContainerId != "" { - match = strings.EqualFold(entry.ContainerId, in.ContainerId) + if in.Message != "" { + match = strings.Contains(strings.ToLower(entry.Message), strings.ToLower(in.Message)) } - if in.NodeId != "" { - match = strings.EqualFold(entry.NodeId, in.NodeId) + if in.Node != "" { + match = strings.HasPrefix(strings.ToLower(entry.NodeId), strings.ToLower(in.Node)) } - if in.ServiceIsh != "" { + if in.Service != "" { serviceID := strings.ToLower(entry.ServiceId) serviceName := strings.ToLower(entry.ServiceName) - match = strings.HasPrefix(serviceID, strings.ToLower(in.ServiceIsh)) || strings.HasPrefix(serviceName, strings.ToLower(in.ServiceIsh)) + match = strings.HasPrefix(serviceID, strings.ToLower(in.Service)) || strings.HasPrefix(serviceName, strings.ToLower(in.Service)) } - if in.Message != "" { - match = strings.Contains(strings.ToLower(entry.Message), strings.ToLower(in.Message)) + if in.Stack != "" { + stackID := strings.ToLower(entry.StackId) + stackName := strings.ToLower(entry.StackName) + match = strings.HasPrefix(stackID, strings.ToLower(in.Stack)) || strings.HasPrefix(stackName, strings.ToLower(in.Stack)) } return match } diff --git a/api/rpc/logs/logs.pb.go b/api/rpc/logs/logs.pb.go index 33bf017bc..88d82594d 100644 --- a/api/rpc/logs/logs.pb.go +++ b/api/rpc/logs/logs.pb.go @@ -55,14 +55,12 @@ func (*LogEntry) ProtoMessage() {} func (*LogEntry) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} } type GetRequest struct { - Timestamp string `protobuf:"bytes,1,opt,name=timestamp" json:"timestamp,omitempty"` - ServiceId string `protobuf:"bytes,2,opt,name=service_id,json=serviceId" json:"service_id,omitempty"` - ServiceName string `protobuf:"bytes,3,opt,name=service_name,json=serviceName" json:"service_name,omitempty"` - Message string `protobuf:"bytes,4,opt,name=message" json:"message,omitempty"` - ContainerId string `protobuf:"bytes,5,opt,name=container_id,json=containerId" json:"container_id,omitempty"` - NodeId string `protobuf:"bytes,6,opt,name=node_id,json=nodeId" json:"node_id,omitempty"` - Size int64 `protobuf:"zigzag64,7,opt,name=size" json:"size,omitempty"` - ServiceIsh string `protobuf:"bytes,8,opt,name=service_ish,json=serviceIsh" json:"service_ish,omitempty"` + Container string `protobuf:"bytes,1,opt,name=container" json:"container,omitempty"` + Message string `protobuf:"bytes,2,opt,name=message" json:"message,omitempty"` + Node string `protobuf:"bytes,3,opt,name=node" json:"node,omitempty"` + Size int64 `protobuf:"zigzag64,4,opt,name=size" json:"size,omitempty"` + Service string `protobuf:"bytes,5,opt,name=service" json:"service,omitempty"` + Stack string `protobuf:"bytes,6,opt,name=stack" json:"stack,omitempty"` } func (m *GetRequest) Reset() { *m = GetRequest{} } @@ -230,30 +228,30 @@ func init() { } var fileDescriptor0 = []byte{ - // 394 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0x84, 0x52, 0xcb, 0xae, 0xd3, 0x30, - 0x10, 0x55, 0x9a, 0xdc, 0x3c, 0x26, 0xe8, 0x0a, 0x79, 0x43, 0x78, 0x89, 0x4b, 0x37, 0x74, 0xd5, - 0xa0, 0x7b, 0xf9, 0x05, 0x54, 0x45, 0xaa, 0x58, 0x84, 0x0f, 0x00, 0x37, 0x19, 0xa5, 0x56, 0xeb, - 0xd8, 0xd8, 0x2e, 0x52, 0xf9, 0x43, 0xbe, 0x88, 0x2d, 0xf2, 0x24, 0x69, 0xe9, 0x82, 0x76, 0x63, - 0x79, 0xce, 0x99, 0x63, 0x9f, 0x39, 0x1a, 0x78, 0xea, 0x84, 0xdb, 0x1e, 0x36, 0xcb, 0x46, 0xc9, - 0x92, 0x6b, 0xdd, 0xe0, 0x1e, 0x0d, 0x77, 0xca, 0x94, 0x5c, 0xea, 0x92, 0x6b, 0x51, 0x1a, 0xdd, - 0x94, 0x7b, 0xd5, 0x59, 0x3a, 0x96, 0xda, 0x28, 0xa7, 0x58, 0xe4, 0xef, 0xf3, 0xdf, 0x33, 0x48, - 0xd7, 0xaa, 0xfb, 0xdc, 0x3b, 0x73, 0x64, 0x6f, 0x20, 0x73, 0x42, 0xa2, 0x75, 0x5c, 0xea, 0x22, - 0x78, 0x08, 0x16, 0x59, 0x7d, 0x06, 0xd8, 0x0b, 0x48, 0x7c, 0xf1, 0x4d, 0xb4, 0xc5, 0x8c, 0xb8, - 0xd8, 0x97, 0x55, 0xcb, 0xde, 0x02, 0x58, 0x34, 0x3f, 0x45, 0x43, 0x5c, 0x38, 0xe8, 0x46, 0xa4, - 0x6a, 0xd9, 0x7b, 0x78, 0x36, 0xd1, 0x3d, 0x97, 0x58, 0x44, 0xd4, 0x90, 0x8f, 0xd8, 0x17, 0x2e, - 0x91, 0x15, 0x90, 0x48, 0xb4, 0x96, 0x77, 0x58, 0xdc, 0x11, 0x3b, 0x95, 0x5e, 0xdc, 0xa8, 0xde, - 0x71, 0xd1, 0xa3, 0xf1, 0xaf, 0xc7, 0x83, 0xf8, 0x84, 0x55, 0xad, 0xf7, 0xd5, 0xab, 0x96, 0xfe, - 0x4e, 0x06, 0x5f, 0xbe, 0x1c, 0x08, 0xc7, 0xed, 0xce, 0x13, 0xe9, 0x68, 0x98, 0xdb, 0x5d, 0xd5, - 0xb2, 0xd7, 0x90, 0x11, 0x41, 0x76, 0x32, 0xa2, 0x52, 0x0f, 0x90, 0x97, 0x97, 0x90, 0x5a, 0xc7, - 0x1b, 0x92, 0xc1, 0x60, 0x86, 0xea, 0x71, 0x50, 0xa2, 0x48, 0x98, 0x8f, 0x83, 0x7a, 0xc4, 0x2b, - 0xe7, 0x7f, 0x02, 0x80, 0x15, 0xba, 0x1a, 0x7f, 0x1c, 0xd0, 0xba, 0x1b, 0x69, 0x5e, 0x86, 0x36, - 0xbb, 0x15, 0x5a, 0x78, 0x35, 0xb4, 0xe8, 0x7a, 0x68, 0x77, 0x57, 0x43, 0x8b, 0x2f, 0x42, 0x63, - 0x10, 0x59, 0xf1, 0x0b, 0x29, 0x4a, 0x56, 0xd3, 0x9d, 0xbd, 0x83, 0xfc, 0xe4, 0xd5, 0x6e, 0xc7, - 0x30, 0x27, 0xfb, 0x95, 0xdd, 0xce, 0x3f, 0x41, 0x4a, 0x83, 0xeb, 0xfd, 0x91, 0x2d, 0x20, 0xc1, - 0xde, 0x19, 0x81, 0xb6, 0x08, 0x1e, 0xc2, 0x45, 0xfe, 0x78, 0xbf, 0xa4, 0xad, 0x9b, 0xb6, 0xac, - 0x9e, 0xe8, 0xc7, 0xef, 0x10, 0xad, 0x55, 0x67, 0xd9, 0x07, 0x08, 0x57, 0xe8, 0xd8, 0xf3, 0xa1, - 0xef, 0x9c, 0xe0, 0xab, 0xfb, 0x7f, 0x10, 0xff, 0x74, 0x09, 0xd9, 0x0a, 0xdd, 0x57, 0x67, 0x90, - 0xcb, 0xff, 0xb7, 0x4f, 0x1f, 0x7d, 0x0c, 0x36, 0x31, 0xad, 0xfa, 0xd3, 0xdf, 0x00, 0x00, 0x00, - 0xff, 0xff, 0x0e, 0x37, 0x37, 0x11, 0x21, 0x03, 0x00, 0x00, + // 395 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0x74, 0x52, 0xed, 0x8e, 0xd3, 0x30, + 0x10, 0x54, 0xda, 0x5c, 0x93, 0x6c, 0xd1, 0x09, 0x59, 0x48, 0x84, 0x2f, 0xe9, 0xe8, 0x1f, 0xfa, + 0xab, 0x41, 0x77, 0xbc, 0x02, 0xaa, 0x22, 0x9d, 0xf8, 0x11, 0x1e, 0x00, 0x7c, 0xc9, 0x2a, 0x58, + 0x57, 0xc7, 0xc6, 0xf6, 0x21, 0x1d, 0xcf, 0xc2, 0x0b, 0xf1, 0x56, 0x68, 0xd7, 0x4e, 0x5b, 0x7e, + 0xdc, 0x9f, 0xc8, 0x33, 0xe3, 0x75, 0x66, 0x46, 0x0b, 0x37, 0xa3, 0x0a, 0x3f, 0x1e, 0xee, 0x76, + 0xbd, 0xd1, 0x8d, 0xb4, 0xb6, 0xc7, 0x03, 0x3a, 0x19, 0x8c, 0x6b, 0xa4, 0xb6, 0x8d, 0xb4, 0xaa, + 0x71, 0xb6, 0x6f, 0x0e, 0x66, 0xf4, 0xfc, 0xd9, 0x59, 0x67, 0x82, 0x11, 0x39, 0x9d, 0x37, 0x7f, + 0x17, 0x50, 0xde, 0x9a, 0xf1, 0xf3, 0x14, 0xdc, 0xa3, 0x78, 0x0b, 0x55, 0x50, 0x1a, 0x7d, 0x90, + 0xda, 0xd6, 0xd9, 0x55, 0xb6, 0xad, 0xba, 0x13, 0x21, 0x5e, 0x42, 0x41, 0xe0, 0x9b, 0x1a, 0xea, + 0x05, 0x6b, 0x2b, 0x82, 0xed, 0x20, 0xde, 0x01, 0x78, 0x74, 0xbf, 0x54, 0xcf, 0xda, 0x32, 0xce, + 0x25, 0xa6, 0x1d, 0xc4, 0x7b, 0x78, 0x36, 0xcb, 0x93, 0xd4, 0x58, 0xe7, 0x7c, 0x61, 0x9d, 0xb8, + 0x2f, 0x52, 0xa3, 0xa8, 0xa1, 0xd0, 0xe8, 0xbd, 0x1c, 0xb1, 0xbe, 0x60, 0x75, 0x86, 0x34, 0xdc, + 0x9b, 0x29, 0x48, 0x35, 0xa1, 0xa3, 0xd7, 0x57, 0x71, 0xf8, 0xc8, 0xb5, 0x03, 0xf9, 0x9a, 0xcc, + 0xc0, 0xff, 0x2e, 0xa2, 0x2f, 0x82, 0x51, 0x08, 0xd2, 0xdf, 0x93, 0x50, 0x26, 0xc3, 0xd2, 0xdf, + 0xb7, 0x83, 0x78, 0x03, 0x15, 0x0b, 0x6c, 0xa7, 0x62, 0xa9, 0x24, 0x82, 0xbd, 0xbc, 0x82, 0xd2, + 0x07, 0xd9, 0xf3, 0x18, 0x44, 0x33, 0x8c, 0x53, 0x50, 0x96, 0x78, 0x70, 0x9d, 0x82, 0x12, 0x43, + 0x93, 0x9b, 0x3f, 0x19, 0xc0, 0x1e, 0x43, 0x87, 0x3f, 0x1f, 0xd0, 0x07, 0x6a, 0xf3, 0x68, 0x73, + 0x6e, 0xf3, 0x48, 0x9c, 0x47, 0x5e, 0xfc, 0x1f, 0x59, 0x40, 0x4e, 0x01, 0x52, 0x91, 0x7c, 0x26, + 0xce, 0xab, 0xdf, 0xb1, 0x3b, 0xd1, 0xf1, 0x99, 0x5e, 0x48, 0x1d, 0xce, 0xa5, 0x25, 0x28, 0x5e, + 0xc0, 0x05, 0xbb, 0x4a, 0x6d, 0x45, 0xb0, 0xf9, 0x04, 0x25, 0xbb, 0xb3, 0x87, 0x47, 0xb1, 0x85, + 0x02, 0xa7, 0xe0, 0x14, 0xfa, 0x3a, 0xbb, 0x5a, 0x6e, 0xd7, 0xd7, 0x97, 0x3b, 0x5e, 0x8d, 0x79, + 0x15, 0xba, 0x59, 0xbe, 0xfe, 0x0e, 0xf9, 0xad, 0x19, 0xbd, 0xf8, 0x00, 0xcb, 0x3d, 0x06, 0xf1, + 0x3c, 0xde, 0x3b, 0xc5, 0x7c, 0x7d, 0x79, 0xc6, 0xd0, 0xd3, 0x0d, 0x54, 0x7b, 0x0c, 0x5f, 0x83, + 0x43, 0xa9, 0x9f, 0xbe, 0x3e, 0xff, 0xe8, 0x63, 0x76, 0xb7, 0xe2, 0x7d, 0xbc, 0xf9, 0x17, 0x00, + 0x00, 0xff, 0xff, 0xe5, 0x02, 0xcd, 0xb2, 0xc6, 0x02, 0x00, 0x00, } diff --git a/api/rpc/logs/logs.proto b/api/rpc/logs/logs.proto index 4ea0d0e40..7cd3ccb36 100644 --- a/api/rpc/logs/logs.proto +++ b/api/rpc/logs/logs.proto @@ -22,14 +22,12 @@ message LogEntry { } message GetRequest { - string timestamp = 1; - string service_id = 2; - string service_name = 3; - string message = 4; - string container_id = 5; - string node_id = 6; - sint64 size = 7; - string service_ish = 8; + string container = 1; + string message = 2; + string node = 3; + sint64 size = 4; + string service = 5; + string stack = 6; } message GetReply { diff --git a/api/rpc/logs/logs_test.go b/api/rpc/logs/logs_test.go index af333e093..a5577cc42 100644 --- a/api/rpc/logs/logs_test.go +++ b/api/rpc/logs/logs_test.go @@ -19,11 +19,13 @@ import ( const ( defaultNumberOfEntries = 50 - testServiceId = "testServiceId" - testServiceName = "testServiceName" - testNodeId = "testNodeId" testContainerId = "testContainerId" testMessage = "test message " + testNodeId = "testNodeId" + testServiceId = "testServiceId" + testServiceName = "testServiceName" + testStackId = "testStackId" + testStackName = "testStackName" ) var ( @@ -47,6 +49,8 @@ func TestMain(m *testing.M) { producer.Close() }() + produceLogEntries(1000) + os.Exit(m.Run()) } @@ -68,7 +72,7 @@ func TestShouldGetAHundredLogEntriesByDefault(t *testing.T) { assert.Equal(t, expected, actual) } -func TestShouldFilterByContainerId(t *testing.T) { +func TestShouldFilterByContainer(t *testing.T) { // First, get a random container id r, err := client.Get(ctx, &logs.GetRequest{}) if err != nil { @@ -78,7 +82,7 @@ func TestShouldFilterByContainerId(t *testing.T) { randomContainerId := r.Entries[0].ContainerId // Then filter by this container id - r, err = client.Get(ctx, &logs.GetRequest{ContainerId: randomContainerId}) + r, err = client.Get(ctx, &logs.GetRequest{Container: randomContainerId}) if err != nil { t.Error(err) } @@ -88,7 +92,7 @@ func TestShouldFilterByContainerId(t *testing.T) { } } -func TestShouldFilterByNodeId(t *testing.T) { +func TestShouldFilterByNode(t *testing.T) { // First, get a random node id r, err := client.Get(ctx, &logs.GetRequest{}) if err != nil { @@ -98,7 +102,7 @@ func TestShouldFilterByNodeId(t *testing.T) { randomNodeId := r.Entries[0].NodeId // Then filter by this node id - r, err = client.Get(ctx, &logs.GetRequest{NodeId: randomNodeId}) + r, err = client.Get(ctx, &logs.GetRequest{Node: randomNodeId}) if err != nil { t.Error(err) } @@ -108,54 +112,36 @@ func TestShouldFilterByNodeId(t *testing.T) { } } -func TestShouldFilterByServiceId(t *testing.T) { - // First, get a random service id - r, err := client.Get(ctx, &logs.GetRequest{}) - if err != nil { - t.Error(err) - } - assert.NotEmpty(t, r.Entries, "We should have at least one entry") - randomServiceId := r.Entries[0].ServiceId - - // Then filter by this service id - r, err = client.Get(ctx, &logs.GetRequest{ServiceId: randomServiceId}) +func TestShouldFilterByService(t *testing.T) { + r, err := client.Get(ctx, &logs.GetRequest{Service: "et"}) if err != nil { t.Error(err) } assert.NotEmpty(t, r.Entries, "We should have at least one entry") for _, entry := range r.Entries { - assert.Equal(t, randomServiceId, entry.ServiceId) + assert.True(t, strings.HasPrefix(entry.ServiceName, "et") || strings.HasPrefix(entry.ServiceId, "et")) } } -func TestShouldFilterByServiceName(t *testing.T) { - // First, get a random service name - r, err := client.Get(ctx, &logs.GetRequest{}) - if err != nil { - t.Error(err) - } - assert.NotEmpty(t, r.Entries, "We should have at least one entry") - randomServiceName := r.Entries[0].ServiceName - - // Then filter by this service name - r, err = client.Get(ctx, &logs.GetRequest{ServiceName: randomServiceName}) +func TestShouldFilterByMessage(t *testing.T) { + r, err := client.Get(ctx, &logs.GetRequest{Message: "info"}) if err != nil { t.Error(err) } assert.NotEmpty(t, r.Entries, "We should have at least one entry") for _, entry := range r.Entries { - assert.Equal(t, randomServiceName, entry.ServiceName) + assert.Contains(t, strings.ToLower(entry.Message), "info") } } -func TestShouldFilterByMessage(t *testing.T) { - r, err := client.Get(ctx, &logs.GetRequest{Message: "info"}) +func TestShouldFilterByStack(t *testing.T) { + r, err := client.Get(ctx, &logs.GetRequest{Stack: "testStack"}) if err != nil { t.Error(err) } assert.NotEmpty(t, r.Entries, "We should have at least one entry") for _, entry := range r.Entries { - assert.Contains(t, strings.ToLower(entry.Message), "info") + assert.True(t, strings.HasPrefix(entry.StackName, "testStack") || strings.HasPrefix(entry.StackId, "testStack")) } } @@ -169,25 +155,28 @@ func TestShouldFetchGivenNumberOfEntries(t *testing.T) { } } -func produceLogEntries(t *testing.T, howMany int) { +func produceLogEntries(howMany int) error { for i := 0; i < howMany; i++ { message, err := proto.Marshal(&logs.LogEntry{ - Timestamp: time.Now().Format(time.RFC3339Nano), - TimeId: time.Now().Format(time.RFC3339Nano), - ServiceId: testServiceId, - ServiceName: testServiceName, - NodeId: testNodeId, ContainerId: testContainerId, Message: testMessage + strconv.Itoa(rand.Int()), + NodeId: testNodeId, + ServiceId: testServiceId, + ServiceName: testServiceName, + StackId: testStackId, + StackName: testStackName, + Timestamp: time.Now().Format("2006-01-02T15:04:05.999"), + TimeId: time.Now().Format(time.RFC3339Nano), }) _, _, err = producer.SendMessage(&sarama.ProducerMessage{ Topic: "amp-logs", Value: sarama.ByteEncoder(message), }) if err != nil { - t.Error(err) + return err } } + return nil } func listenToLogEntries(t *testing.T, stream logs.Logs_GetStreamClient, howMany int) chan *logs.LogEntry { @@ -221,17 +210,17 @@ func TestShouldStreamLogs(t *testing.T) { if err != nil { t.Error(err) } - go produceLogEntries(t, 100) + go produceLogEntries(100) entries := listenToLogEntries(t, stream, defaultNumberOfEntries) assert.Equal(t, defaultNumberOfEntries, len(entries)) } -func TestShouldStreamAndFilterByContainerId(t *testing.T) { - stream, err := client.GetStream(ctx, &logs.GetRequest{ContainerId: testContainerId}) +func TestShouldStreamAndFilterByContainer(t *testing.T) { + stream, err := client.GetStream(ctx, &logs.GetRequest{Container: testContainerId}) if err != nil { t.Error(err) } - go produceLogEntries(t, 100) + go produceLogEntries(100) entries := listenToLogEntries(t, stream, defaultNumberOfEntries) assert.Equal(t, defaultNumberOfEntries, len(entries)) for entry := range entries { @@ -239,12 +228,12 @@ func TestShouldStreamAndFilterByContainerId(t *testing.T) { } } -func TestShouldStreamAndFilterByNodeId(t *testing.T) { - stream, err := client.GetStream(ctx, &logs.GetRequest{NodeId: testNodeId}) +func TestShouldStreamAndFilterByNode(t *testing.T) { + stream, err := client.GetStream(ctx, &logs.GetRequest{Node: testNodeId}) if err != nil { t.Error(err) } - go produceLogEntries(t, 100) + go produceLogEntries(100) entries := listenToLogEntries(t, stream, defaultNumberOfEntries) assert.Equal(t, defaultNumberOfEntries, len(entries)) for entry := range entries { @@ -252,38 +241,38 @@ func TestShouldStreamAndFilterByNodeId(t *testing.T) { } } -func TestShouldStreamAndFilterByServiceId(t *testing.T) { - stream, err := client.GetStream(ctx, &logs.GetRequest{ServiceId: testServiceId}) +func TestShouldStreamAndFilterByService(t *testing.T) { + stream, err := client.GetStream(ctx, &logs.GetRequest{Service: "testService"}) if err != nil { t.Error(err) } - go produceLogEntries(t, 100) + go produceLogEntries(100) entries := listenToLogEntries(t, stream, defaultNumberOfEntries) assert.Equal(t, defaultNumberOfEntries, len(entries)) for entry := range entries { - assert.Equal(t, testServiceId, entry.ServiceId) + assert.True(t, strings.HasPrefix(entry.ServiceName, "testService") || strings.HasPrefix(entry.ServiceId, "testService")) } } -func TestShouldStreamAndFilterByServiceName(t *testing.T) { - stream, err := client.GetStream(ctx, &logs.GetRequest{ServiceName: testServiceName}) +func TestShouldStreamAndFilterByMessage(t *testing.T) { + stream, err := client.GetStream(ctx, &logs.GetRequest{Message: testMessage}) if err != nil { t.Error(err) } - go produceLogEntries(t, 100) + go produceLogEntries(100) entries := listenToLogEntries(t, stream, defaultNumberOfEntries) assert.Equal(t, defaultNumberOfEntries, len(entries)) for entry := range entries { - assert.Equal(t, testServiceName, entry.ServiceName) + assert.Contains(t, strings.ToLower(entry.Message), testMessage) } } -func TestShouldStreamAndFilterByMessage(t *testing.T) { - stream, err := client.GetStream(ctx, &logs.GetRequest{Message: testMessage}) +func TestShouldStreamAndFilterCaseInsensitivelyByMessage(t *testing.T) { + stream, err := client.GetStream(ctx, &logs.GetRequest{Message: strings.ToUpper(testMessage)}) if err != nil { t.Error(err) } - go produceLogEntries(t, 100) + go produceLogEntries(100) entries := listenToLogEntries(t, stream, defaultNumberOfEntries) assert.Equal(t, defaultNumberOfEntries, len(entries)) for entry := range entries { @@ -291,15 +280,15 @@ func TestShouldStreamAndFilterByMessage(t *testing.T) { } } -func TestShouldStreamAndFilterCaseInsensitivelyByMessage(t *testing.T) { - stream, err := client.GetStream(ctx, &logs.GetRequest{Message: strings.ToUpper(testMessage)}) +func TestShouldStreamAndFilterByStack(t *testing.T) { + stream, err := client.GetStream(ctx, &logs.GetRequest{Stack: "testStack"}) if err != nil { t.Error(err) } - go produceLogEntries(t, 100) + go produceLogEntries(100) entries := listenToLogEntries(t, stream, defaultNumberOfEntries) assert.Equal(t, defaultNumberOfEntries, len(entries)) for entry := range entries { - assert.Contains(t, strings.ToLower(entry.Message), testMessage) + assert.True(t, strings.HasPrefix(entry.StackName, "testStack") || strings.HasPrefix(entry.StackId, "testStack")) } } diff --git a/cmd/amp/logs.go b/cmd/amp/logs.go index f1b788c1a..a2c5f2e3e 100644 --- a/cmd/amp/logs.go +++ b/cmd/amp/logs.go @@ -12,7 +12,7 @@ import ( ) var logsCmd = &cobra.Command{ - Use: "logs", + Use: "logs [OPTIONS] SERVICE", Short: "Fetch the logs", Long: `Search through all the logs of the system and fetch entries matching provided criteria.`, Run: func(cmd *cobra.Command, args []string) { @@ -25,14 +25,13 @@ var logsCmd = &cobra.Command{ func init() { // TODO logsCmd.Flags().String("timestamp", "", "filter by the given timestamp") - logsCmd.Flags().String("service-id", "", "Filter by the given service id") - logsCmd.Flags().String("service-name", "", "Filter by the given service name") + logsCmd.Flags().String("container", "", "Filter by the given container") + logsCmd.Flags().BoolP("follow", "f", false, "Follow log output") logsCmd.Flags().String("message", "", "Filter the message content by the given pattern") - logsCmd.Flags().String("container", "", "Filter by the given container id") - logsCmd.Flags().String("node", "", "Filter by the given node id") - logsCmd.Flags().StringP("number", "n", "100", "Number of results") logsCmd.Flags().BoolP("meta", "m", false, "Display entry metadata") - logsCmd.Flags().BoolP("follow", "f", false, "Follow log output") + logsCmd.Flags().String("node", "", "Filter by the given node") + logsCmd.Flags().StringP("number", "n", "100", "Number of results") + logsCmd.Flags().String("stack", "", "Filter by the given stack") RootCmd.AddCommand(logsCmd) } @@ -45,8 +44,7 @@ func Logs(amp *client.AMP, cmd *cobra.Command, args []string) error { } if amp.Verbose() { fmt.Println("Logs") - fmt.Printf("service-id: %v\n", cmd.Flag("service_id").Value) - fmt.Printf("service-name: %v\n", cmd.Flag("service_name").Value) + fmt.Printf("stack: %v\n", cmd.Flag("stack").Value) fmt.Printf("message: %v\n", cmd.Flag("message").Value) fmt.Printf("container: %v\n", cmd.Flag("container_id").Value) fmt.Printf("node: %v\n", cmd.Flag("node_id").Value) @@ -56,13 +54,12 @@ func Logs(amp *client.AMP, cmd *cobra.Command, args []string) error { request := logs.GetRequest{} if len(args) > 0 { - request.ServiceIsh = args[0] + request.Service = args[0] } - request.ServiceId = cmd.Flag("service-id").Value.String() - request.ServiceName = cmd.Flag("service-name").Value.String() + request.Container = cmd.Flag("container").Value.String() + request.Node = cmd.Flag("node").Value.String() request.Message = cmd.Flag("message").Value.String() - request.ContainerId = cmd.Flag("container").Value.String() - request.NodeId = cmd.Flag("node").Value.String() + request.Stack = cmd.Flag("stack").Value.String() if request.Size, err = strconv.ParseInt(cmd.Flag("number").Value.String(), 10, 64); err != nil { log.Fatalf("Unable to convert n parameter: %v\n", cmd.Flag("n").Value.String()) } diff --git a/swarm b/swarm index 62cab3d86..aa65164ae 100755 --- a/swarm +++ b/swarm @@ -180,7 +180,7 @@ removeservices() { stop=$(docker service ls -q --filter "label=io.amp.role=$ROLE") [[ ! -z ${stop[0]} ]] && echo "> Removing current infrastructure services..." && docker service rm $stop || true stop=$(docker service ls -q --filter "label=io.amp.role=user") - [[ ! -z ${stop[0]} ]] && echo "> Removing current user services..." && docker service rm $stop || true + [[ ! -z ${stop[0]} ]] && echo "> Removing current user services..." && docker service rm $stop || true } # start the services on the swarm