-
Notifications
You must be signed in to change notification settings - Fork 31
Add runtime log level management API with automatic restoration #2897
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2b0f116
Add temporary log level change feature - core implementation
Copilot d23c42f
Remove hand-created token; force CLI to create one
bbockelm f3f18b6
Make sure runtime dir is not the global one for tests
bbockelm f7cc62f
Tuneup after invoking the set-logging-level from my terminal
bbockelm 16ec843
Fix sporadic TestBrokerApi failure by polling for broker readiness
bbockelm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,200 @@ | ||
| /*************************************************************** | ||
| * | ||
| * Copyright (C) 2025, Pelican Project, Morgridge Institute for Research | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); you | ||
| * may not use this file except in compliance with the License. You may | ||
| * obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| * | ||
| ***************************************************************/ | ||
|
|
||
| package main | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "net/http" | ||
| "net/url" | ||
| "path" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "github.com/pkg/errors" | ||
| log "github.com/sirupsen/logrus" | ||
| "github.com/spf13/cobra" | ||
|
|
||
| "github.com/pelicanplatform/pelican/config" | ||
| "github.com/pelicanplatform/pelican/param" | ||
| ) | ||
|
|
||
| var ( | ||
| loggingParameterName string | ||
|
|
||
| serverSetLoggingLevelCmd = &cobra.Command{ | ||
| Use: "set-logging-level <level> <duration>", | ||
| Short: "Temporarily change the server's log level", | ||
| Long: `Temporarily change the server's log level for a specified duration. | ||
| The log level will automatically revert to the configured level after the duration expires. | ||
|
|
||
| Valid log levels: debug, info, warn, error, fatal, panic | ||
| Duration should be specified as a Go duration string (e.g., 5m, 1h30m, 300s) | ||
|
|
||
| Examples: | ||
| pelican server set-logging-level debug 5m -s https://my-origin.com:8447 | ||
| pelican server set-logging-level info 30m -s https://my-cache.com:8447 -t /path/to/token | ||
| pelican server set-logging-level debug 2m -s https://my-origin.com:8447 --param Logging.Origin.Xrootd`, | ||
| Args: cobra.ExactArgs(2), | ||
| RunE: setLogLevel, | ||
| } | ||
| ) | ||
|
|
||
| func init() { | ||
| serverCmd.AddCommand(serverSetLoggingLevelCmd) | ||
| serverSetLoggingLevelCmd.Flags().StringVarP(&loggingParameterName, "param", "p", "Logging.Level", "Target parameter for the log level (e.g., Logging.Level, Logging.Origin.Xrootd, Logging.Cache.Xrootd)") | ||
| serverSetLoggingLevelCmd.Flags().StringVarP(&serverURLStr, "server", "s", "", "Web URL of the Pelican server (e.g. https://my-origin.com:8447)") | ||
| serverSetLoggingLevelCmd.Flags().StringVarP(&tokenLocation, "token", "t", "", "Path to the admin token file") | ||
| } | ||
|
|
||
| func setLogLevel(cmd *cobra.Command, args []string) error { | ||
| ctx := cmd.Context() | ||
| if ctx == nil { | ||
| ctx = context.Background() | ||
| } | ||
|
|
||
| // Initialize config to load configuration file | ||
| // InitClient will compute Server.ExternalWebUrl from Server.Hostname and Server.WebPort | ||
| if err := config.InitClient(); err != nil { | ||
| log.Errorln("Failed to initialize client:", err) | ||
| } | ||
|
|
||
| level := args[0] | ||
| durationStr := args[1] | ||
|
|
||
| // Parse duration as Go duration string (e.g., "5m", "1h30m", "300s") | ||
| duration, err := time.ParseDuration(durationStr) | ||
| if err != nil { | ||
| return errors.Wrap(err, "Duration must be a valid Go duration string (e.g., 5m, 1h30m, 300s)") | ||
| } | ||
| if duration <= 0 { | ||
| return errors.New("Duration must be positive") | ||
| } | ||
| durationSeconds := int(duration.Seconds()) | ||
|
|
||
| parameterName := strings.TrimSpace(loggingParameterName) | ||
| if parameterName == "" { | ||
| parameterName = "Logging.Level" | ||
| } | ||
|
|
||
| // Construct API URL - use config if server URL not provided | ||
| srvURL := serverURLStr | ||
| if srvURL == "" { | ||
| // Try to get Server.ExternalWebUrl from config (computed or explicit) | ||
| srvURL = param.Server_ExternalWebUrl.GetString() | ||
| if srvURL == "" { | ||
| return errors.New("Server URL must be provided via --server flag or Server.ExternalWebUrl config") | ||
| } | ||
| } | ||
|
|
||
| targetURL, err := constructLoggingApiURL(srvURL) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // Build request payload | ||
| payload := map[string]interface{}{ | ||
| "level": level, | ||
| "duration": durationSeconds, | ||
| "parameterName": parameterName, | ||
| } | ||
|
|
||
| payloadBytes, err := json.Marshal(payload) | ||
| if err != nil { | ||
| return errors.Wrap(err, "Failed to marshal request payload") | ||
| } | ||
|
|
||
| // Get admin token - use config for server URL if not provided | ||
| srvURL = serverURLStr | ||
| if srvURL == "" { | ||
| srvURL = param.Server_ExternalWebUrl.GetString() | ||
| } | ||
|
|
||
| tok, err := fetchOrGenerateWebAPIAdminToken(srvURL, tokenLocation) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // Prepare and send the HTTP request | ||
| req, err := http.NewRequestWithContext(ctx, "POST", targetURL.String(), bytes.NewBuffer(payloadBytes)) | ||
| if err != nil { | ||
| return errors.Wrap(err, "Failed to create HTTP request") | ||
| } | ||
| req.Header.Set("Content-Type", "application/json") | ||
| req.Header.Set("Authorization", "Bearer "+tok) | ||
| req.Header.Set("Accept", "application/json") | ||
| req.Header.Set("User-Agent", "pelican-client/"+config.GetVersion()) | ||
|
|
||
| httpClient := &http.Client{Transport: config.GetTransport()} | ||
| resp, err := httpClient.Do(req) | ||
| if err != nil { | ||
| return errors.Wrap(err, "HTTP request failed") | ||
| } | ||
| defer resp.Body.Close() | ||
|
|
||
| bodyBytes, err := handleAdminApiResponse(resp) | ||
| if err != nil { | ||
| return errors.Wrap(err, "Server request failed") | ||
| } | ||
|
|
||
| // Parse response | ||
| type LogLevelChangeResponse struct { | ||
| ChangeID string `json:"changeId"` | ||
| Level string `json:"level"` | ||
| ParameterName string `json:"parameterName"` | ||
| EndTime time.Time `json:"endTime"` | ||
| Remaining int `json:"remainingSeconds"` | ||
| } | ||
|
|
||
| var response LogLevelChangeResponse | ||
| if err := json.Unmarshal(bodyBytes, &response); err != nil { | ||
| return errors.Wrap(err, "Failed to parse server response") | ||
| } | ||
|
|
||
| fmt.Printf("Log level for %s successfully changed to '%s' for %d seconds\n", response.ParameterName, response.Level, response.Remaining) | ||
| fmt.Printf("Change ID: %s\n", response.ChangeID) | ||
| fmt.Printf("Will revert at: %s\n", response.EndTime.Format(time.RFC3339)) | ||
| return nil | ||
| } | ||
|
|
||
| func constructLoggingApiURL(serverURLStr string) (*url.URL, error) { | ||
| if serverURLStr == "" { | ||
| return nil, errors.New("The --server flag providing the server's web URL is required") | ||
| } | ||
| serverURLStr = strings.TrimSuffix(serverURLStr, "/") // Normalize URL | ||
| baseURL, err := url.Parse(serverURLStr) | ||
| if err != nil { | ||
| return nil, errors.Wrapf(err, "Invalid server URL format: %s", serverURLStr) | ||
| } | ||
| // A Pelican server must use HTTPS scheme | ||
| if baseURL.Scheme != "https" { | ||
| return nil, errors.Errorf("Server URL must have an https scheme: %s", serverURLStr) | ||
| } | ||
| if baseURL.Host == "" { | ||
| return nil, errors.Errorf("Server URL must include a hostname: %s", serverURLStr) | ||
| } | ||
| // Construct the full API endpoint URL | ||
| targetURL, err := baseURL.Parse(path.Join("/api/v1.0/logging/level")) | ||
| if err != nil { | ||
| return nil, errors.Wrap(err, "Failed to construct logging API URL") | ||
| } | ||
| return targetURL, nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| /*************************************************************** | ||
| * | ||
| * Copyright (C) 2025, Pelican Project, Morgridge Institute for Research | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); you | ||
| * may not use this file except in compliance with the License. You may | ||
| * obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| * | ||
| ***************************************************************/ | ||
|
|
||
| package main | ||
|
|
||
| import ( | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| var ( | ||
| serverCmd = &cobra.Command{ | ||
| Use: "server", | ||
| Short: "Manage server operations", | ||
| Long: `Provide commands to manage and interact with Pelican server operations. | ||
| These commands allow administrators to interact with server administrative APIs.`, | ||
| } | ||
| ) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.