Skip to content

Commit

Permalink
Add support for configuring custom query params to Logs API
Browse files Browse the repository at this point in the history
These query params can be sort order or any other params.
  • Loading branch information
khrm authored and tekton-robot committed Sep 26, 2024
1 parent f352564 commit a98e863
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions config/base/env/config
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,4 @@ LOGGING_PLUGIN_CA_CERT=
LOGGING_PLUGIN_QUERY_LIMIT=1700
LOGGING_PLUGIN_TLS_VERIFICATION_DISABLE=
LOGGING_PLUGIN_FORWARDER_DELAY_DURATION=10
LOGGING_PLUGIN_QUERY_PARAMS='direction=forward'
1 change: 1 addition & 0 deletions docs/logging-support.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ These are the common configuration options for all third party logging APIs.
- `LOGGING_PLUGIN_TLS_VERIFICATION_DISABLE`: Set to `true` to disable TLS verification for the third party logging API. (optional)
- `LOGGING_PLUGIN_FORWARDER_DELAY_DURATION`: This is the max duration in minutes taken by third party logging system to forward and store the logs after completion of taskrun and pipelinerun. This is used to search between start time of runs and completion plus buffer duration.
- `LOGGING_PLUGIN_QUERY_LIMIT`: Sets the query limit for Third Party Logging API if logging backend has a limit on number of log lines returned.
- `LOGGING_PLUGIN_QUERY_PARAMS`: Sets the query params for Third Party Logging API, these can be direction/sort order.Specify them in this format: "foo=bar&direction=backward"
1 change: 1 addition & 0 deletions pkg/api/server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ type Config struct {
LOGGING_PLUGIN_QUERY_LIMIT uint `mapstructure:"LOGGING_PLUGIN_QUERY_LIMIT"`
LOGGING_PLUGIN_TLS_VERIFICATION_DISABLE bool `mapstructure:"LOGGING_PLUGIN_TLS_VERIFICATION_DISABLE"`
LOGGING_PLUGIN_FORWARDER_DELAY_DURATION uint `mapstructure:"LOGGING_PLUGIN_FORWARDER_DELAY_DURATION"`
LOGGING_PLUGIN_QUERY_PARAMS string `mapstructure:"LOGGING_PLUGIN_QUERY_PARAMS"`
}

func Get() *Config {
Expand Down
4 changes: 4 additions & 0 deletions pkg/api/server/v1alpha2/plugin_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,17 @@ func (s *LogPluginServer) getLokiLogs(writer *logs.BufferedLog, parent string, r
}

parameters := url.Values{}
for k, v := range s.queryParams {
parameters.Add(k, v)
}
parameters.Add("query", `{ `+s.staticLabels+s.config.LOGGING_PLUGIN_NAMESPACE_KEY+`="`+parent+`" }|json uid="`+uidKey+`", message="message" |uid="`+rec.Name+`"| line_format "{{.message}}"`)
parameters.Add("end", endTime)
parameters.Add("start", startTime)
parameters.Add("limit", strconv.Itoa(int(s.queryLimit)))

URL.RawQuery = parameters.Encode()
s.logger.Debugf("loki request url:%s", URL.String())

req, err := http.NewRequest("GET", URL.String(), nil)
if err != nil {
s.logger.Errorf("new request to loki failed, err: %s:", err.Error())
Expand Down
1 change: 1 addition & 0 deletions pkg/api/server/v1alpha2/plugin_logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func TestLogPluginServer_GetLog(t *testing.T) {
LOGGING_PLUGIN_STATIC_LABELS: "namespace=\"foo\"",
LOGGING_PLUGIN_NAMESPACE_KEY: "namespace",
LOGGING_PLUGIN_QUERY_LIMIT: 1500,
LOGGING_PLUGIN_QUERY_PARAMS: "direction=forward",
}, logger.Get("info"), test.NewDB(t))
if err != nil {
t.Fatalf("failed to create server: %v", err)
Expand Down
13 changes: 13 additions & 0 deletions pkg/api/server/v1alpha2/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ type LogPluginServer struct {

queryLimit uint

queryParams map[string]string

// TODO: In future add support for non Oauth support
tokenSource oauth2.TokenSource
}
Expand Down Expand Up @@ -212,5 +214,16 @@ func (s *Server) createLogPluginServer() error {
s.LogPluginServer.tokenSource = transport.NewCachedFileTokenSource(s.config.LOGGING_PLUGIN_TOKEN_PATH)
s.LogPluginServer.queryLimit = s.config.LOGGING_PLUGIN_QUERY_LIMIT

s.LogPluginServer.queryParams = map[string]string{}
if s.config.LOGGING_PLUGIN_QUERY_PARAMS != "" {
for _, v := range strings.Split(s.config.LOGGING_PLUGIN_QUERY_PARAMS, "&") {
queryParam := strings.Split(v, "=")
if len(queryParam) != 2 {
return fmt.Errorf("incorrect format for LOGGING_PLUGIN_QUERY_PARAMS: %s", v)
}
s.LogPluginServer.queryParams[queryParam[0]] = queryParam[1]
}
}

return nil
}

0 comments on commit a98e863

Please sign in to comment.