github.com/yankunsam/loki/v2@v2.6.3-0.20220817130409-389df5235c27/pkg/logql/log/pattern/pattern_test.go (about)

     1  package pattern
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/require"
     8  )
     9  
    10  var fixtures = []struct {
    11  	expr     string
    12  	in       string
    13  	expected []string
    14  }{
    15  	{
    16  		"foo <foo> bar",
    17  		"foo buzz bar",
    18  		[]string{"buzz"},
    19  	},
    20  	{
    21  		"foo <foo> bar<fuzz>",
    22  		"foo buzz bar",
    23  		[]string{"buzz", ""},
    24  	},
    25  	{
    26  		"<foo> bar<fuzz>",
    27  		" bar",
    28  		[]string{"", ""},
    29  	},
    30  	{
    31  		"<path>?<_>",
    32  		`/api/plugins/versioncheck?slugIn=snuids-trafficlights-panel,input,gel&grafanaVersion=7.0.0-beta1`,
    33  		[]string{"/api/plugins/versioncheck"},
    34  	},
    35  	{
    36  		"<path>?<_>",
    37  		`/api/plugins/status`,
    38  		[]string{"/api/plugins/status"},
    39  	},
    40  	{
    41  		// Common Log Format
    42  		`<ip> <userid> <user> [<_>] "<method> <path> <_>" <status> <size>`,
    43  		`127.0.0.1 user-identifier frank [10/Oct/2000:13:55:36 -0700] "GET /apache_pb.gif HTTP/1.0" 200 2326`,
    44  		[]string{"127.0.0.1", "user-identifier", "frank", "GET", "/apache_pb.gif", "200", "2326"},
    45  	},
    46  	{
    47  		// Combined Log Format
    48  		`<ip> - - [<_>] "<method> <path> <_>" <status> <size> `,
    49  		`35.191.8.106 - - [19/May/2021:07:21:49 +0000] "GET /api/plugins/versioncheck?slugIn=snuids-trafficlights-panel,input,gel&grafanaVersion=7.0.0-beta1 HTTP/1.1" 200 107 "-" "Go-http-client/2.0" "80.153.74.144, 34.120.177.193" "TLSv1.3" "DE" "DEBW"`,
    50  		[]string{"35.191.8.106", "GET", "/api/plugins/versioncheck?slugIn=snuids-trafficlights-panel,input,gel&grafanaVersion=7.0.0-beta1", "200", "107"},
    51  	},
    52  	{
    53  		// MySQL
    54  		`<_> <id> [<level>] [<no>] [<component>] `,
    55  		`2020-08-06T14:25:02.835618Z 0 [Note] [MY-012487] [InnoDB] DDL log recovery : begin`,
    56  		[]string{"0", "Note", "MY-012487", "InnoDB"},
    57  	},
    58  	{
    59  		// MySQL
    60  		`<_> <id> [<level>] `,
    61  		`2021-05-19T07:40:12.215792Z 42761518 [Note] Aborted connection 42761518 to db: 'hosted_grafana' user: 'hosted_grafana' host: '10.36.4.122' (Got an error reading communication packets)`,
    62  		[]string{"42761518", "Note"},
    63  	},
    64  	{
    65  		// Kubernetes api-server
    66  		`<id> <_>       <_> <line>] `,
    67  		`W0519 07:46:47.647050       1 clientconn.go:1223] grpc: addrConn.createTransport failed to connect to {https://kubernetes-etcd-1.kubernetes-etcd:2379  <nil> 0 <nil>}. Err :connection error: desc = "transport: Error while dialing dial tcp 10.32.85.85:2379: connect: connection refused". Reconnecting...`,
    68  		[]string{"W0519", "clientconn.go:1223"},
    69  	},
    70  	{
    71  		// Cassandra
    72  		`<level>  [<component>]<_> in <duration>.<_>`,
    73  		`INFO  [Service Thread] 2021-05-19 07:40:12,130 GCInspector.java:284 - ParNew GC in 248ms.  CMS Old Gen: 5043436640 -> 5091062064; Par Eden Space: 671088640 -> 0; Par Survivor Space: 70188280 -> 60139760`,
    74  		[]string{"INFO", "Service Thread", "248ms"},
    75  	},
    76  	{
    77  		// Cortex & Loki distributor
    78  		`<_> msg="<method> <path> (<status>) <duration>"`,
    79  		`level=debug ts=2021-05-19T07:54:26.864644382Z caller=logging.go:66 traceID=7fbb92fd0eb9c65d msg="POST /loki/api/v1/push (204) 1.238734ms"`,
    80  		[]string{"POST", "/loki/api/v1/push", "204", "1.238734ms"},
    81  	},
    82  	{
    83  		// Etcd
    84  		`<_> <_> <level> | <component>: <_> peer <peer_id> <_> tcp <ip>:<_>`,
    85  		`2021-05-19 08:16:50.181436 W | rafthttp: health check for peer fd8275e521cfb532 could not connect: dial tcp 10.32.85.85:2380: connect: connection refused`,
    86  		[]string{"W", "rafthttp", "fd8275e521cfb532", "10.32.85.85"},
    87  	},
    88  	{
    89  		// Kafka
    90  		`<_>] <level> [Log partition=<part>, dir=<dir>] `,
    91  		`[2021-05-19 08:35:28,681] INFO [Log partition=p-636-L-fs-117, dir=/data/kafka-logs] Deleting segment 455976081 (kafka.log.Log)`,
    92  		[]string{"INFO", "p-636-L-fs-117", "/data/kafka-logs"},
    93  	},
    94  	{
    95  		// Elastic
    96  		`<_>][<level>][<component>] [<id>] [<index>]`,
    97  		`[2021-05-19T06:54:06,994][INFO ][o.e.c.m.MetaDataMappingService] [1f605d47-8454-4bfb-a67f-49f318bf837a] [usage-stats-2021.05.19/O2Je9IbmR8CqFyUvNpTttA] update_mapping [report]`,
    98  		[]string{"INFO ", "o.e.c.m.MetaDataMappingService", "1f605d47-8454-4bfb-a67f-49f318bf837a", "usage-stats-2021.05.19/O2Je9IbmR8CqFyUvNpTttA"},
    99  	},
   100  	{
   101  		// Envoy
   102  		`<_> "<method> <path> <_>" <status> <_> <received_bytes> <sent_bytes> <duration> <upstream_time> "<forward_for>" "<agent>" <_> <_> "<upstream>"`,
   103  		`[2016-04-15T20:17:00.310Z] "POST /api/v1/locations HTTP/2" 204 - 154 0 226 100 "10.0.35.28" "nsq2http" "cc21d9b0-cf5c-432b-8c7e-98aeb7988cd2" "locations" "tcp://10.0.2.1:80"`,
   104  		[]string{"POST", "/api/v1/locations", "204", "154", "0", "226", "100", "10.0.35.28", "nsq2http", "tcp://10.0.2.1:80"},
   105  	},
   106  }
   107  
   108  func Test_matcher_Matches(t *testing.T) {
   109  	for _, tt := range fixtures {
   110  		tt := tt
   111  		t.Run(tt.expr, func(t *testing.T) {
   112  			t.Parallel()
   113  			m, err := New(tt.expr)
   114  			require.NoError(t, err)
   115  			actual := m.Matches([]byte(tt.in))
   116  			var actualStrings []string
   117  			for _, a := range actual {
   118  				actualStrings = append(actualStrings, string(a))
   119  			}
   120  			require.Equal(t, tt.expected, actualStrings)
   121  		})
   122  	}
   123  }
   124  
   125  var res [][]byte
   126  
   127  func Benchmark_matcher_Matches(b *testing.B) {
   128  	for _, tt := range fixtures {
   129  		b.Run(tt.expr, func(b *testing.B) {
   130  			b.ReportAllocs()
   131  			m, err := New(tt.expr)
   132  			require.NoError(b, err)
   133  			b.ResetTimer()
   134  			l := []byte(tt.in)
   135  			for n := 0; n < b.N; n++ {
   136  				res = m.Matches(l)
   137  			}
   138  		})
   139  	}
   140  }
   141  
   142  func Test_Error(t *testing.T) {
   143  	for _, tt := range []struct {
   144  		name string
   145  		err  error
   146  	}{
   147  		{"<f>", nil},
   148  		{"<f> <a>", nil},
   149  		{"", newParseError("syntax error: unexpected $end, expecting IDENTIFIER or LITERAL", 1, 1)},
   150  		{"<_>", ErrNoCapture},
   151  		{"foo <_> bar <_>", ErrNoCapture},
   152  		{"foo bar buzz", ErrNoCapture},
   153  		{"<f><f>", fmt.Errorf("found consecutive capture '<f><f>': %w", ErrInvalidExpr)},
   154  		{"<f> f<d><b>", fmt.Errorf("found consecutive capture '<d><b>': %w", ErrInvalidExpr)},
   155  		{"<f> f<f>", fmt.Errorf("duplicate capture name (f): %w", ErrInvalidExpr)},
   156  		{`f<f><_>`, fmt.Errorf("found consecutive capture '<f><_>': %w", ErrInvalidExpr)},
   157  		{`<f>f<f><_>`, fmt.Errorf("found consecutive capture '<f><_>': %w", ErrInvalidExpr)},
   158  	} {
   159  		t.Run(tt.name, func(t *testing.T) {
   160  			_, err := New(tt.name)
   161  			require.Equal(t, tt.err, err)
   162  		})
   163  	}
   164  }