1- package loki
1+ package framework
22
33import (
44 "context"
88 "time"
99
1010 "github.com/go-resty/resty/v2"
11-
12- "github.com/smartcontractkit/chainlink-testing-framework/framework"
1311)
1412
1513// APIError is a custom error type for handling non-200 responses from the Loki API
@@ -45,8 +43,8 @@ type LogEntry struct {
4543 Log string
4644}
4745
48- // Client represents a client to interact with Loki for querying logs
49- type Client struct {
46+ // LokiClient represents a client to interact with Loki for querying logs
47+ type LokiClient struct {
5048 BaseURL string
5149 TenantID string
5250 BasicAuth BasicAuth
@@ -62,9 +60,9 @@ type QueryParams struct {
6260 Limit int
6361}
6462
65- // NewQueryClient creates a new Loki client with the given parameters, initializes a logger, and configures Resty with debug mode
66- func NewQueryClient (baseURL , tenantID string , auth BasicAuth , queryParams QueryParams ) * Client {
67- framework . L .Info ().
63+ // NewLokiQueryClient creates a new Loki client with the given parameters, initializes a logger, and configures Resty with debug mode
64+ func NewLokiQueryClient (baseURL , tenantID string , auth BasicAuth , queryParams QueryParams ) * LokiClient {
65+ L .Info ().
6866 Str ("BaseURL" , baseURL ).
6967 Str ("TenantID" , tenantID ).
7068 Msg ("Initializing Loki Client" )
@@ -75,7 +73,7 @@ func NewQueryClient(baseURL, tenantID string, auth BasicAuth, queryParams QueryP
7573 restyClient := resty .New ().
7674 SetDebug (isDebug )
7775
78- return & Client {
76+ return & LokiClient {
7977 BaseURL : baseURL ,
8078 TenantID : tenantID ,
8179 BasicAuth : auth ,
@@ -85,9 +83,9 @@ func NewQueryClient(baseURL, tenantID string, auth BasicAuth, queryParams QueryP
8583}
8684
8785// QueryRange queries Loki logs based on the query parameters and returns the raw log entries
88- func (lc * Client ) QueryRange (ctx context.Context ) ([]LogEntry , error ) {
86+ func (lc * LokiClient ) QueryRange (ctx context.Context ) ([]LogEntry , error ) {
8987 // Log request details
90- framework . L .Info ().
88+ L .Info ().
9189 Str ("Query" , lc .QueryParams .Query ).
9290 Str ("StartTime" , lc .QueryParams .StartTime .Format (time .RFC3339Nano )).
9391 Str ("EndTime" , lc .QueryParams .EndTime .Format (time .RFC3339Nano )).
@@ -117,7 +115,7 @@ func (lc *Client) QueryRange(ctx context.Context) ([]LogEntry, error) {
117115 duration := time .Since (start )
118116
119117 if err != nil {
120- framework . L .Error ().Err (err ).Dur ("duration" , duration ).Msg ("Error querying Loki" )
118+ L .Error ().Err (err ).Dur ("duration" , duration ).Msg ("Error querying Loki" )
121119 return nil , err
122120 }
123121
@@ -127,7 +125,7 @@ func (lc *Client) QueryRange(ctx context.Context) ([]LogEntry, error) {
127125 if len (bodySnippet ) > 200 {
128126 bodySnippet = bodySnippet [:200 ] + "..."
129127 }
130- framework . L .Error ().
128+ L .Error ().
131129 Int ("StatusCode" , resp .StatusCode ()).
132130 Dur ("duration" , duration ).
133131 Str ("ResponseBody" , bodySnippet ).
@@ -139,40 +137,40 @@ func (lc *Client) QueryRange(ctx context.Context) ([]LogEntry, error) {
139137 }
140138
141139 // Log successful response
142- framework . L .Info ().
140+ L .Info ().
143141 Int ("StatusCode" , resp .StatusCode ()).
144142 Dur ("duration" , duration ).
145143 Msg ("Successfully queried Loki API" )
146144
147145 // Parse the response into the Response struct
148146 var lokiResp Response
149147 if err := json .Unmarshal (resp .Body (), & lokiResp ); err != nil {
150- framework . L .Error ().Err (err ).Msg ("Error decoding response from Loki" )
148+ L .Error ().Err (err ).Msg ("Error decoding response from Loki" )
151149 return nil , err
152150 }
153151
154152 // Extract log entries from the response
155153 logEntries := lc .extractRawLogEntries (lokiResp )
156154
157155 // Log the number of entries retrieved
158- framework . L .Info ().Int ("LogEntries" , len (logEntries )).Msg ("Successfully retrieved logs from Loki" )
156+ L .Info ().Int ("LogEntries" , len (logEntries )).Msg ("Successfully retrieved logs from Loki" )
159157
160158 return logEntries , nil
161159}
162160
163161// extractRawLogEntries processes the Response and returns raw log entries
164- func (lc * Client ) extractRawLogEntries (lokiResp Response ) []LogEntry {
162+ func (lc * LokiClient ) extractRawLogEntries (lokiResp Response ) []LogEntry {
165163 var logEntries []LogEntry
166164
167165 for _ , result := range lokiResp .Data .Result {
168166 for _ , entry := range result .Values {
169167 if len (entry ) != 2 {
170- framework . L .Error ().Interface ("Log entry" , entry ).Msgf ("Error parsing log entry. Expected 2 elements, got %d" , len (entry ))
168+ L .Error ().Interface ("Log entry" , entry ).Msgf ("Error parsing log entry. Expected 2 elements, got %d" , len (entry ))
171169 continue
172170 }
173171 var timestamp string
174172 if entry [0 ] == nil {
175- framework . L .Error ().Msg ("Error parsing timestamp. Entry at index 0, that should be a timestamp, is nil" )
173+ L .Error ().Msg ("Error parsing timestamp. Entry at index 0, that should be a timestamp, is nil" )
176174 continue
177175 }
178176 if timestampString , ok := entry [0 ].(string ); ok {
@@ -182,7 +180,7 @@ func (lc *Client) extractRawLogEntries(lokiResp Response) []LogEntry {
182180 } else if timestampFloat , ok := entry [0 ].(float64 ); ok {
183181 timestamp = fmt .Sprintf ("%f" , timestampFloat )
184182 } else {
185- framework . L .Error ().Msgf ("Error parsing timestamp. Expected string, int, or float64, got %T" , entry [0 ])
183+ L .Error ().Msgf ("Error parsing timestamp. Expected string, int, or float64, got %T" , entry [0 ])
186184 continue
187185 }
188186 logLine := entry [1 ].(string )
0 commit comments