-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
feat(server/v2): wire telemetry + server refactors #21746
Merged
Merged
Changes from 5 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d8b1c86
feat(server/v2): wire telemetry
julienrbrt a936803
Merge branch 'main' into julien/wire-telemetry
julienrbrt 9203f8f
updates
julienrbrt 0b69482
Merge branch 'main' into julien/wire-telemetry
julienrbrt 11b33bb
fix typo
julienrbrt 97e19ff
Merge branch 'main' into julien/wire-telemetry
julienrbrt e422f5d
proper empy config check
julienrbrt 64068e2
log if servers are disabled via config
julienrbrt 9117d85
no go rountine
julienrbrt 907123e
no double log
julienrbrt c80099b
updates
julienrbrt 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
There are no files selected for viewing
This file contains 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 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 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 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 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 |
---|---|---|
@@ -1,47 +1,127 @@ | ||
package telemetry | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/gorilla/mux" | ||
"cosmossdk.io/core/transaction" | ||
"cosmossdk.io/log" | ||
serverv2 "cosmossdk.io/server/v2" | ||
) | ||
|
||
func RegisterMetrics(r mux.Router, cfg Config) (*Metrics, error) { | ||
m, err := New(cfg) | ||
var ( | ||
_ serverv2.ServerComponent[transaction.Tx] = (*TelemetryServer[transaction.Tx])(nil) | ||
_ serverv2.HasConfig = (*TelemetryServer[transaction.Tx])(nil) | ||
julienrbrt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
|
||
const ServerName = "telemetry" | ||
|
||
type TelemetryServer[T transaction.Tx] struct { | ||
config *Config | ||
logger log.Logger | ||
server *http.Server | ||
metrics *Metrics | ||
} | ||
|
||
// New creates a new telemetry server. | ||
func New[T transaction.Tx]() *TelemetryServer[T] { | ||
return &TelemetryServer[T]{} | ||
} | ||
|
||
// Name returns the server name. | ||
func (s *TelemetryServer[T]) Name() string { | ||
return ServerName | ||
} | ||
|
||
func (s *TelemetryServer[T]) Config() any { | ||
if s.config == nil || s.config == (&Config{}) { | ||
julienrbrt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return DefaultConfig() | ||
} | ||
julienrbrt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return s.config | ||
} | ||
|
||
julienrbrt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Init implements serverv2.ServerComponent. | ||
func (s *TelemetryServer[T]) Init(appI serverv2.AppI[T], cfg map[string]any, logger log.Logger) error { | ||
serverCfg := s.Config().(*Config) | ||
if len(cfg) > 0 { | ||
if err := serverv2.UnmarshalSubConfig(cfg, s.Name(), &serverCfg); err != nil { | ||
return fmt.Errorf("failed to unmarshal config: %w", err) | ||
} | ||
} | ||
s.config = serverCfg | ||
s.logger = logger | ||
|
||
metrics, err := NewMetrics(s.config) | ||
if err != nil { | ||
return nil, err | ||
return fmt.Errorf("failed to initialize metrics: %w", err) | ||
} | ||
s.metrics = metrics | ||
|
||
metricsHandler := func(w http.ResponseWriter, r *http.Request) { | ||
format := strings.TrimSpace(r.FormValue("format")) | ||
return nil | ||
} | ||
|
||
gr, err := m.Gather(format) | ||
if err != nil { | ||
w.Header().Set("Content-Type", "application/json") | ||
w.WriteHeader(http.StatusBadRequest) | ||
bz, err := json.Marshal(errorResponse{Code: 400, Error: fmt.Sprintf("failed to gather metrics: %s", err)}) | ||
if err != nil { | ||
return | ||
} | ||
_, _ = w.Write(bz) | ||
func (s *TelemetryServer[T]) Start(ctx context.Context) error { | ||
if !s.config.Enable { | ||
return nil | ||
julienrbrt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
return | ||
} | ||
mux := http.NewServeMux() | ||
mux.HandleFunc("/", s.metricsHandler) | ||
// keeping /metrics for backwards compatibility | ||
mux.HandleFunc("/metrics", func(w http.ResponseWriter, r *http.Request) { | ||
http.Redirect(w, r, "/", http.StatusMovedPermanently) | ||
}) | ||
|
||
w.Header().Set("Content-Type", gr.ContentType) | ||
_, _ = w.Write(gr.Metrics) | ||
s.server = &http.Server{ | ||
Addr: s.config.Address, | ||
Handler: mux, | ||
} | ||
|
||
r.HandleFunc("/metrics", metricsHandler).Methods("GET") | ||
go func() { | ||
s.logger.Info("Starting telemetry server", "address", s.config.Address) | ||
if err := s.server.ListenAndServe(); err != nil && err != http.ErrServerClosed { | ||
julienrbrt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
s.logger.Error("Failed to start telemetry server", "error", err) | ||
} | ||
}() | ||
|
||
|
||
return m, nil | ||
return nil | ||
julienrbrt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
// errorResponse defines the attributes of a JSON error response. | ||
type errorResponse struct { | ||
Code int `json:"code,omitempty"` | ||
Error string `json:"error"` | ||
func (s *TelemetryServer[T]) Stop(ctx context.Context) error { | ||
if !s.config.Enable || s.server == nil { | ||
return nil | ||
} | ||
|
||
s.logger.Info("Stopping telemetry server") | ||
return s.server.Shutdown(ctx) | ||
} | ||
|
||
func (s *TelemetryServer[T]) metricsHandler(w http.ResponseWriter, r *http.Request) { | ||
format := strings.TrimSpace(r.FormValue("format")) | ||
|
||
// errorResponse defines the attributes of a JSON error response. | ||
type errorResponse struct { | ||
Code int `json:"code,omitempty"` | ||
Error string `json:"error"` | ||
} | ||
|
||
gr, err := s.metrics.Gather(format) | ||
if err != nil { | ||
w.Header().Set("Content-Type", "application/json") | ||
w.WriteHeader(http.StatusBadRequest) | ||
bz, err := json.Marshal(errorResponse{Code: 400, Error: fmt.Sprintf("failed to gather metrics: %s", err)}) | ||
if err != nil { | ||
return | ||
} | ||
_, _ = w.Write(bz) | ||
|
||
return | ||
} | ||
|
||
w.Header().Set("Content-Type", gr.ContentType) | ||
_, _ = w.Write(gr.Metrics) | ||
} |
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In v2, as discussed yesterday, the telemetry endpoint will be in another port than grpc gateway.