Skip to content
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 11 commits into from
Sep 27, 2024
Merged

Conversation

julienrbrt
Copy link
Member

@julienrbrt julienrbrt commented Sep 16, 2024

Description

Closes: #21732

It will use another http server than grpc gateway (on v1) because we don't want to share the same http server between (unexisting) grpc gateway server components and metrics.
It will give a better UX to not have to pass http servers around.

Additionally, it makes all server consistent and does some preliminary boilerplate wiring for #20798


Author Checklist

All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.

I have...

  • included the correct type prefix in the PR title, you can find examples of the prefixes below:
  • confirmed ! in the type prefix if API or client breaking change
  • targeted the correct branch (see PR Targeting)
  • provided a link to the relevant issue or specification
  • reviewed "Files changed" and left comments if necessary
  • included the necessary unit and integration tests
  • added a changelog entry to CHANGELOG.md
  • updated the relevant documentation or specification, including comments for documenting Go code
  • confirmed all CI checks have passed

Reviewers Checklist

All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.

Please see Pull Request Reviewer section in the contributing guide for more information on how to review a pull request.

I have...

  • confirmed the correct type prefix in the PR title
  • confirmed all author checklist items have been addressed
  • reviewed state machine logic, API design and naming, documentation is accurate, tests and test coverage

Summary by CodeRabbit

Summary by CodeRabbit

  • New Features

    • Introduced a new telemetry server with enhanced configuration options.
    • Added functionality for metrics tracking within the command initialization process.
    • Enhanced configurability of the gRPC-gateway with a specified binding address.
  • Bug Fixes

    • Improved error handling in the metrics creation process.
    • Enhanced accuracy in configuration checks for the Server component.
  • Documentation

    • Updated comments and descriptions for clarity in configuration fields.
  • Refactor

    • Consolidated type assertions for the GRPCGatewayServer.
    • Simplified function signatures and structures for better maintainability, including changes to the DefaultConfig function and metrics handling.
    • Renamed and reorganized server components for improved clarity and functionality.

Copy link
Contributor

coderabbitai bot commented Sep 16, 2024

📝 Walkthrough
📝 Walkthrough

Walkthrough

The changes involve multiple updates across various files in the server's API, primarily focusing on the telemetry functionality. Key modifications include the introduction of new configuration options, restructuring of existing functions, and enhancements to type assertions. The telemetry server is now integrated into the command initialization process, reflecting a more cohesive setup for monitoring and metrics.

Changes

File Path Change Summary
server/v2/api/grpc/config.go Removed comments describing default values in DefaultConfig function; functionality remains unchanged.
server/v2/api/grpcgateway/config.go Added Address field to Config struct and updated DefaultConfig function to initialize it.
server/v2/api/grpcgateway/server.go Renamed GRPCGatewayServer to Server, updated associated methods, and simplified Config method logic.
server/v2/api/telemetry/config.go Introduced DefaultConfig() function; renamed Enabled to Enable and added Address field in Config struct.
server/v2/api/telemetry/server.go Added Server struct with methods for initializing, starting, and stopping the telemetry server; removed RegisterMetrics function.
server/v2/store/commands.go Changed PrunesCmd method receiver from StoreComponent[T] to Server[T].
server/v2/store/server.go Renamed StoreComponent[T] to Server[T] and updated method signatures accordingly.
server/v2/store/snapshot.go Changed method receivers from StoreComponent[T] to Server[T] for snapshot management functions.
server/v2/go.mod Removed dependency on github.com/gorilla/mux package.
server/v2/api/grpc/server.go Updated Config method logic and simplified Start method by removing goroutine and error channel.

Assessment against linked issues

Objective Addressed Explanation
Wire telemetry in server/v2

Possibly related PRs

Suggested labels

Type: Build, Type: CI, backport/v0.52.x

Suggested reviewers

  • kocubinski
  • facundomedica
  • sontrinh16
  • tac0turtle
  • hieuvubk

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@github-actions github-actions bot added C:server/v2 Issues related to server/v2 C:server/v2 api labels Sep 16, 2024
@julienrbrt julienrbrt marked this pull request as ready for review September 27, 2024 09:51
Copy link
Contributor

@julienrbrt your pull request is missing a changelog!

@julienrbrt
Copy link
Member Author

We just need to update confix for the config migration.

func DefaultConfig() *Config {
return &Config{
Enable: true,
Address: "localhost:1338",
Copy link
Member Author

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.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🧹 Outside diff range and nitpick comments (3)
server/v2/api/grpc/config.go (1)

7-8: Consider reinstating field comments for better documentation.

While the functionality remains unchanged, the removal of comments describing the default values for Address, MaxRecvMsgSize, and MaxSendMsgSize might impact code readability and maintainability. Consider adding inline comments to provide context for these configuration values, especially for MaxRecvMsgSize and MaxSendMsgSize, as their default values are not immediately obvious.

Example:

return &Config{
    Enable:         true,
    Address:        "localhost:9090", // Default gRPC server address
    MaxRecvMsgSize: 1024 * 1024 * 10, // 10 MB
    MaxSendMsgSize: math.MaxInt32,    // Maximum possible value
}
server/v2/api/telemetry/config.go (1)

Line range hint 19-72: Config struct is well-defined, but has some inconsistencies

The Config struct is well-structured and follows Go naming conventions. The field comments are clear and informative, providing good documentation for users of this configuration.

However, there are a few points to address:

  1. Breaking change: The Enabled field has been renamed to Enable. This is a breaking change that should be documented and possibly reconsidered if backward compatibility is important.

  2. Inconsistent tag values:

    • The MetricsSink field has a type mapstructure tag, which doesn't match its field name.
    • The StatsdAddr field has a stats-addr toml tag, which doesn't match its field name.

Consider the following changes:

  1. If backward compatibility is required, revert the field name change from Enable to Enabled.
  2. Update the inconsistent tags:
-	MetricsSink string `mapstructure:"type" toml:"metrics-sink" comment:"MetricsSink defines the type of metrics backend to use. Default is in memory"`
+	MetricsSink string `mapstructure:"metrics-sink" toml:"metrics-sink" comment:"MetricsSink defines the type of metrics backend to use. Default is in memory"`

-	StatsdAddr string `mapstructure:"statsd-addr" toml:"stats-addr" comment:"StatsdAddr defines the address of a statsd server to send metrics to. Only utilized if MetricsSink is set to \"statsd\" or \"dogstatsd\"."`
+	StatsdAddr string `mapstructure:"statsd-addr" toml:"statsd-addr" comment:"StatsdAddr defines the address of a statsd server to send metrics to. Only utilized if MetricsSink is set to \"statsd\" or \"dogstatsd\"."`

These changes will ensure consistency between field names and their corresponding tags.

simapp/v2/simdv2/cmd/commands.go (1)

85-85: LGTM: Telemetry initialization added correctly.

The addition of telemetry.New[T]() to the serverv2.AddCommands function call correctly integrates telemetry functionality into the server setup. This change aligns with the PR objectives and addresses the requirements outlined in issue #21732.

Consider wrapping the entire serverv2.AddCommands call in a separate error check for improved error handling and clarity. For example:

if err := serverv2.AddCommands(
    rootCmd,
    newApp,
    logger,
    initServerConfig(),
    cometbft.New(
        &genericTxDecoder[T]{txConfig},
        initCometOptions[T](),
        initCometConfig(),
    ),
    grpc.New[T](),
    store.New[T](newApp),
    telemetry.New[T](),
); err != nil {
    logger.Error("failed to add commands", "error", err)
    os.Exit(1)
}

This approach provides more context in case of an error and allows for proper logging before exiting.

📜 Review details

Configuration used: .coderabbit.yml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between dcf00cf and 0b69482.

📒 Files selected for processing (6)
  • server/v2/api/grpc/config.go (1 hunks)
  • server/v2/api/grpcgateway/server.go (1 hunks)
  • server/v2/api/telemetry/config.go (1 hunks)
  • server/v2/api/telemetry/metrics.go (2 hunks)
  • server/v2/api/telemetry/server.go (1 hunks)
  • simapp/v2/simdv2/cmd/commands.go (2 hunks)
🧰 Additional context used
📓 Path-based instructions (6)
server/v2/api/grpc/config.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

server/v2/api/grpcgateway/server.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

server/v2/api/telemetry/config.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

server/v2/api/telemetry/metrics.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

server/v2/api/telemetry/server.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

simapp/v2/simdv2/cmd/commands.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

🔇 Additional comments (7)
server/v2/api/grpc/config.go (1)

Line range hint 1-53: Overall structure and documentation look good.

The file maintains a clear structure with well-documented types and functions. The Config struct and its fields have descriptive comments, which is excellent for maintainability. The helper functions OverwriteDefaultConfig and Disable provide useful configuration options. The code adheres to the Uber Go Style Guide, with consistent naming conventions and structure.

server/v2/api/telemetry/config.go (1)

3-17: LGTM: DefaultConfig function is well-structured

The DefaultConfig() function is clear, concise, and follows Go naming conventions. It provides a good set of default values for the Config struct.

Could you please verify if "localhost:1338" is the intended default address for the metrics server? This seems to be a non-standard port, so we should ensure it's the correct choice.

server/v2/api/grpcgateway/server.go (1)

20-23: LGTM! Good improvement in type assertions and formatting.

The changes in the variable declaration section are well-implemented:

  1. The use of a variable block with parentheses for grouping related declarations is consistent with the Uber Golang style guide.
  2. Adding the type assertion for serverv2.HasConfig enhances type safety by ensuring GRPCGatewayServer implements this interface.
  3. The formatting and alignment of the type assertions improve code readability.

These changes contribute to better code organization and type checking without introducing any issues.

server/v2/api/telemetry/metrics.go (3)

61-61: Improved function signature and error handling.

The changes to the function signature and error handling are good improvements:

  1. The function name NewMetrics is more descriptive and avoids stuttering (good alignment with Uber Go Style Guide).
  2. Using a pointer for the config parameter (*Config) can be more efficient, especially for larger structs.
  3. The return signature is simplified by removing the blank identifier.
  4. Renaming rerr to err is more idiomatic in Go.

88-91: Consistent error handling in deferred function.

The deferred function now correctly checks for err instead of rerr, maintaining consistency with the error variable renaming.


61-61: Verify impact of function signature changes.

The changes to the function signature (name and parameter type) are improvements, but they may affect other parts of the codebase:

  1. The function name change from New to NewMetrics will require updates in all places where this function is called.
  2. Changing the parameter type to a pointer (*Config) may require updates in the calling code to pass a pointer instead of a value.

Please ensure that all usages of this function have been updated accordingly throughout the codebase.

Run the following script to check for any remaining usages of the old function signature:

simapp/v2/simdv2/cmd/commands.go (1)

18-18: LGTM: Import statement for telemetry package added correctly.

The new import statement for the telemetry package is properly formatted and consistent with the existing import style. This addition is necessary for the telemetry functionality being introduced.

server/v2/api/telemetry/server.go Outdated Show resolved Hide resolved
server/v2/api/telemetry/server.go Outdated Show resolved Hide resolved
server/v2/api/telemetry/server.go Outdated Show resolved Hide resolved
Copy link
Contributor

@hieuvubk hieuvubk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@tac0turtle
Copy link
Member

Metrics were previously sent to the comet metric server. This is a good change but we should default to sending to comet if an endpoint is not shared.

@julienrbrt
Copy link
Member Author

Metrics were previously sent to the comet metric server. This is a good change but we should default to sending to comet if an endpoint is not shared.

How so? It looks like it was on grpc gateway:

apiSrv.SetTelemetry(metrics)

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Outside diff range and nitpick comments (1)
server/v2/api/grpc/server.go (1)

151-151: Unnecessary change detected.

The modification on this line appears to be unnecessary as it doesn't alter the existing logic. The condition s.config == nil || *s.config == (Config{}) remains the same as before.

Consider reverting this change to maintain code clarity and avoid confusion. If there was a specific reason for this modification, please provide context in a comment.

📜 Review details

Configuration used: .coderabbit.yml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 0b69482 and 5c7a6b6.

📒 Files selected for processing (4)
  • server/v2/api/grpc/server.go (1 hunks)
  • server/v2/api/grpcgateway/server.go (2 hunks)
  • server/v2/api/telemetry/server.go (1 hunks)
  • server/v2/store/server.go (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • server/v2/api/telemetry/server.go
🧰 Additional context used
📓 Path-based instructions (3)
server/v2/api/grpc/server.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

server/v2/api/grpcgateway/server.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

server/v2/store/server.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

🔇 Additional comments (3)
server/v2/store/server.go (1)

Line range hint 71-76: Verify the impact of the Config() method signature change

The AI-generated summary mentions that the Config() method signature has been updated to func (g *StoreComponent[T]) Config() any. This change is not visible in the provided code snippet, but it's important to consider its implications.

Please verify the following:

  1. Confirm that the method signature has indeed been changed to return any.
  2. If confirmed, ensure that all call sites of this method are updated to handle the any return type safely, potentially requiring type assertions.
  3. Consider if this change aligns with the project's design principles regarding type safety.

You can use the following script to check for usage of the Config() method:

This will help identify areas that might need updates due to the signature change.

✅ Verification successful

Verification Successful: The Config() Method Signature Change is Properly Implemented

The Config() method in server/v2/store/server.go has been successfully updated to return any. A thorough search revealed no usages of this method outside its definition, ensuring that there are no places in the codebase requiring adjustments to handle the new return type.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for usage of the Config() method
rg --type go 'Config\(\)' -C 3

Length of output: 110977


Script:

#!/bin/bash
# Verify the signature of the Config() method in server/v2/store/server.go
rg --type go '^func\s+\(.*\)\s+Config\(\)\s+any' server/v2/store/server.go

Length of output: 119


Script:

#!/bin/bash
# Search for all usages of the Config() method excluding its definition
rg --type go 'Config\(\)' --invert-match 'func\s+\(.*\)\s+Config\(\)\s+any' -l | while read -r file; do
  echo "Checking file: $file"
  rg -n 'Config\(\)' "$file" | while read -r line; do
    line_number=$(echo "$line" | cut -d':' -f2)
    usage=$(echo "$line" | cut -d':' -f3-)
    # Check if the usage includes a type assertion or is assigned to an interface{}
    if ! echo "$usage" | grep -q '(\w+),' && ! echo "$usage" | grep -q 'any'; then
      echo "Potential type handling issue at $file:$line_number"
      echo "  Usage: $usage"
    fi
  done
done

Length of output: 168


Script:

#!/bin/bash
# Search for all usages of the Config() method excluding its definition
# and check for proper type handling.

# Find all files that call Config() but are not defining it
files=$(rg --type go 'Config\(\)' --invert-match 'func\s+\(.*\)\s+Config\(\)\s+any' -l)

if [ -z "$files" ]; then
  echo "No usages of Config() method found outside its definition."
  exit 0
fi

# Iterate over each file and check each usage
for file in $files; do
  echo "Checking file: $file"
  # Find lines where Config() is called
  rg -n 'Config\(\)' "$file" | while read -r line; do
    line_number=$(echo "$line" | cut -d':' -f2)
    usage=$(echo "$line" | cut -d':' -f3-)
    # Check if the usage includes a type assertion or is assigned to 'any'
    if ! echo "$usage" | grep -q 'config\s*:=\s*g\.Config()\s*\.(\w+)' && \
       ! echo "$usage" | grep -q '.*Config\(\)\s*\.\w\+'; then
      echo "Potential type handling issue at $file:$line_number"
      echo "  Usage: $usage"
    fi
  done
done

Length of output: 315

server/v2/api/grpcgateway/server.go (2)

20-23: LGTM: Improved type assertions for GRPCGatewayServer

The grouping of type assertions into a var block enhances readability and aligns with the Uber Golang style guide. The addition of the serverv2.HasConfig assertion correctly indicates that GRPCGatewayServer now implements this interface, which is consistent with the changes in the Config method.


75-75: LGTM: Improved nil check in Config method

The modification to dereference the config pointer before comparison with an empty Config struct is correct and more accurate. This change ensures that the method properly handles both nil pointers and non-nil pointers to empty structs, aligning with Go best practices for pointer comparisons.

server/v2/store/server.go Outdated Show resolved Hide resolved
Copy link
Contributor

@alpe alpe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice start!
I added some comments but I have only a limited picture of the ServerComponent

server/v2/api/telemetry/server.go Outdated Show resolved Hide resolved
server/v2/api/telemetry/server.go Outdated Show resolved Hide resolved
server/v2/api/telemetry/server.go Show resolved Hide resolved
server/v2/api/telemetry/server.go Outdated Show resolved Hide resolved
server/v2/api/telemetry/server.go Show resolved Hide resolved
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (1)
server/v2/cometbft/server.go (1)

274-274: LGTM with a suggestion for improvement.

The change improves the condition by making it more specific and avoiding comparison to a zero struct, which aligns with the Uber Go Style Guide. However, consider if other fields of AppTomlConfig are also important for determining whether a new default configuration should be created. If so, you might want to add checks for those fields as well.

If other fields are relevant, consider creating a method on AppTomlConfig to check if it's effectively empty:

func (c *AppTomlConfig) IsEffectivelyEmpty() bool {
    return c == nil || (c.Address == "" /* && check other relevant fields */)
}

Then use it in the Config() method:

if s.config.AppTomlConfig.IsEffectivelyEmpty() {
    // ... create new default configuration
}

This would encapsulate the logic for determining if a new configuration is needed and make it easier to maintain if the criteria change in the future.

📜 Review details

Configuration used: .coderabbit.yml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 97e19ff and e422f5d.

📒 Files selected for processing (5)
  • server/v2/api/grpc/server.go (1 hunks)
  • server/v2/api/grpcgateway/server.go (2 hunks)
  • server/v2/api/telemetry/server.go (1 hunks)
  • server/v2/cometbft/server.go (1 hunks)
  • server/v2/store/server.go (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (4)
  • server/v2/api/grpc/server.go
  • server/v2/api/grpcgateway/server.go
  • server/v2/api/telemetry/server.go
  • server/v2/store/server.go
🧰 Additional context used
📓 Path-based instructions (1)
server/v2/cometbft/server.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

@julienrbrt julienrbrt changed the title feat(server/v2): wire telemetry feat(server/v2): wire telemetry + server refactors Sep 27, 2024
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 4

🧹 Outside diff range and nitpick comments (5)
server/v2/api/grpcgateway/config.go (1)

5-6: LGTM with a minor suggestion

The addition of the Address field with a default value of "localhost:1317" is appropriate. This provides a sensible default for local development.

Consider extracting the default address as a package-level constant for better maintainability:

+const DefaultAddress = "localhost:1317"

func DefaultConfig() *Config {
	return &Config{
		Enable:  true,
-		Address: "localhost:1317",
+		Address: DefaultAddress,
	}
}
server/v2/store/server.go (1)

22-23: Update comment to reflect new struct name

The struct has been renamed from StoreComponent to Server, but the comment above it still mentions StoreComponent. Please update the comment to maintain consistency with the new struct name.

Consider updating the comment as follows:

-// StoreComponent manages store config and contains prune & snapshot commands
+// Server manages store config and contains prune & snapshot commands
server/v2/api/grpcgateway/server.go (3)

26-33: LGTM: Server struct refactored with improved naming.

The renaming of GRPCGatewayServer to Server simplifies the struct name, enhancing readability. The addition of the server *http.Server field suggests an improvement in server management.

Consider adding a comment for the server field to explain its purpose, similar to the other fields:

server *http.Server // HTTP server for serving gRPC-Gateway endpoints

Line range hint 65-82: LGTM: Name and Config methods updated appropriately.

The Name and Config methods have been correctly updated to reflect the struct renaming. The simplification of the Config method by removing the unnecessary check for an empty Config struct is a good improvement.

Consider adding a comment to explain the purpose of the cfgOptions slice and how it's used to override the default configuration:

// Apply configuration options to override defaults
for _, opt := range s.cfgOptions {
    opt(cfg)
}

99-119: LGTM: Start method significantly improved.

The Start method has been well-refactored to create and start an HTTP server, aligning with the addition of the http.Server field in the struct. The error handling is more robust, correctly distinguishing between server startup errors and normal shutdown.

Consider adding a context timeout for the ListenAndServe call to ensure the server doesn't hang indefinitely on shutdown:

go func() {
    if err := s.server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
        s.logger.Error("failed to start gRPC-Gateway server", "error", err)
    }
}()

select {
case <-ctx.Done():
    return s.Stop(context.Background())
case <-time.After(time.Second): // Allow a short time for server to start
    return nil
}
📜 Review details

Configuration used: .coderabbit.yml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between e422f5d and c80099b.

⛔ Files ignored due to path filters (1)
  • server/v2/go.sum is excluded by !**/*.sum
📒 Files selected for processing (9)
  • server/v2/api/grpc/server.go (4 hunks)
  • server/v2/api/grpcgateway/config.go (1 hunks)
  • server/v2/api/grpcgateway/server.go (5 hunks)
  • server/v2/api/telemetry/config.go (1 hunks)
  • server/v2/api/telemetry/server.go (1 hunks)
  • server/v2/go.mod (0 hunks)
  • server/v2/store/commands.go (1 hunks)
  • server/v2/store/server.go (2 hunks)
  • server/v2/store/snapshot.go (6 hunks)
💤 Files not reviewed due to no reviewable changes (1)
  • server/v2/go.mod
🚧 Files skipped from review as they are similar to previous changes (1)
  • server/v2/api/telemetry/config.go
🧰 Additional context used
📓 Path-based instructions (7)
server/v2/api/grpc/server.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

server/v2/api/grpcgateway/config.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

server/v2/api/grpcgateway/server.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

server/v2/api/telemetry/server.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

server/v2/store/commands.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

server/v2/store/server.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

server/v2/store/snapshot.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

🔇 Additional comments (31)
server/v2/api/grpcgateway/config.go (1)

13-15: LGTM: Well-documented addition to the Config struct

The new Address field is properly integrated into the Config struct with appropriate struct tags and a clear, descriptive comment. This addition enhances the configurability of the gRPC-gateway server.

server/v2/store/server.go (6)

15-17: LGTM: Interface implementations updated correctly

The interface implementations have been properly updated to use Server instead of StoreComponent, maintaining consistency with the type renaming. The use of blank identifier _ ensures compile-time verification of interface implementation.


29-30: LGTM: Constructor function updated correctly

The constructor function New has been properly updated to return *Server[T] instead of *StoreComponent[T], maintaining consistency with the struct renaming. The implementation is correct and follows the Golang style guide.


33-34: LGTM: Init method updated correctly

The Init method has been properly updated with the new receiver type *Server[T] and parameter name s. These changes are consistent with the struct renaming and improve code readability.


45-57: LGTM: Name, Start, and Stop methods updated correctly

The Name, Start, and Stop methods have been properly updated with the new receiver type *Server[T] and parameter name s. These changes are consistent with the struct renaming and improve code readability. The method implementations remain unchanged, which is correct.


Line range hint 57-70: LGTM: CLICommands method updated correctly

The CLICommands method has been properly updated with the new receiver type *Server[T] and parameter name s. These changes are consistent with the struct renaming and improve code readability. The method implementation remains unchanged, which is correct.


71-76: LGTM: Config method updated and improved

The Config method has been properly updated with the new receiver type *Server[T] and parameter name s. These changes are consistent with the struct renaming and improve code readability.

Additionally, the condition for returning the default config has been improved to address the potential nil pointer dereference issue mentioned in the past review comments. The new condition s.config == nil || s.config.AppDBBackend == "" is more robust as it checks both for a nil config and an empty AppDBBackend.

Great job on addressing the previous feedback and improving the code safety!

server/v2/api/telemetry/server.go (10)

1-13: LGTM: Import statements are well-organized and necessary.

The import statements are correctly ordered and include all necessary packages for the telemetry server implementation.


15-18: LGTM: Interface checks are correctly implemented.

The interface checks ensure that the Server type correctly implements the serverv2.ServerComponent and serverv2.HasConfig interfaces, providing compile-time verification.


20-20: LGTM: ServerName constant is well-defined.

The ServerName constant follows Go naming conventions and provides a centralized definition for the server name.


22-27: LGTM: Server struct is well-defined with appropriate fields.

The Server struct is correctly defined using generics, allowing flexibility for different transaction types. The struct fields cover all necessary components for the telemetry server.


29-32: LGTM: New function is correctly implemented.

The New function correctly creates and returns a new Server instance, properly utilizing the generic type T.


34-37: LGTM: Name method is correctly implemented.

The Name method correctly returns the ServerName constant, fulfilling the serverv2.ServerComponent interface requirement.


39-45: LGTM: Config method is correctly implemented with improved checks.

The Config method correctly implements the serverv2.HasConfig interface. The check for an empty Address field is an improvement over the previous version, addressing the issue raised in a past review comment.


47-65: LGTM: Init method is comprehensive and well-implemented.

The Init method correctly initializes the server with the provided configuration and logger. It properly unmarshals the configuration and sets up the metrics. The error handling is appropriate, and the inclusion of metrics initialization in this method improves the overall structure.


93-100: LGTM: Stop method is correctly implemented.

The Stop method appropriately shuts down the HTTP server. It correctly checks if the server is enabled and initialized before attempting to shut down. The use of the provided context for shutdown allows for proper timeout control.


1-126: Overall implementation is solid and meets the PR objectives.

The telemetry server implementation in this file is well-structured and follows Go best practices. It successfully integrates telemetry configuration and setup within the server/v2 API server, addressing the requirements outlined in the PR objectives. The use of generics, proper interface implementations, and comprehensive error handling contribute to a robust and flexible solution.

While some minor improvements have been suggested for error handling and server startup, the overall implementation is commendable and achieves the goals set out in the linked issue #21732.

server/v2/api/grpcgateway/server.go (4)

19-21: LGTM: Type assertions are correct and useful.

The type assertions for Server[transaction.Tx] implementing ServerComponent and HasConfig interfaces are well-placed. They provide compile-time checks, ensuring that the Server struct adheres to the expected interfaces.


Line range hint 37-64: LGTM: Server initialization updated consistently.

The New function has been correctly updated to return *Server[T], maintaining consistency with the struct renaming. The initialization logic remains largely unchanged, preserving existing behavior while incorporating the structural changes. The continued use of generics with [T transaction.Tx] maintains type safety.


121-127: LGTM: Stop method correctly updated for graceful shutdown.

The Stop method has been well-updated to use the Shutdown method on the HTTP server, ensuring a graceful stoppage. The logging of the server stop enhances observability. These changes align well with the addition of the HTTP server and the overall struct renaming.


136-138: LGTM: Constant added for GRPC block height header.

The addition of the GRPCBlockHeightHeader constant improves code readability and maintainability. The constant name follows Go naming conventions, which is good for consistency.

server/v2/api/grpc/server.go (2)

151-151: Improved configuration check

The modification to check for both nil config and empty Address provides a more precise condition for determining if the configuration is effectively uninitialized. This change aligns with the Uber Go Style Guide by using more specific checks.


175-177: Improved server start logic and logging

The changes in the Start method offer several improvements:

  1. Added an informational log statement, enhancing observability.
  2. Simplified error handling by directly returning errors from s.grpcSrv.Serve(listener).
  3. Removed the unnecessary goroutine and error channel, reducing complexity.

These modifications align with the Uber Go Style Guide by simplifying error handling and improving logging practices.

server/v2/store/commands.go (1)

20-20: Method receiver type changed from StoreComponent[T] to Server[T]

The method signature for PrunesCmd has been updated to use Server[T] as the receiver type instead of StoreComponent[T]. This change suggests a shift in the architectural design, potentially moving the pruning functionality to a higher-level component.

While the change itself adheres to the Golang style guide, it's important to consider the following:

  1. Ensure that all calls to PrunesCmd have been updated to use the Server[T] type.
  2. Verify that the Server[T] type has access to all necessary data and methods previously available in StoreComponent[T].
  3. Update any documentation or comments that reference this method to reflect the new receiver type.

To ensure this change doesn't introduce any issues, please run the following command to check for any remaining references to StoreComponent[T].PrunesCmd:

If this command returns any results, those locations may need to be updated to use the new Server[T] type.

server/v2/store/snapshot.go (7)

79-79: LGTM. Verify usage in other parts of the codebase.

The change from StoreComponent[T] to Server[T] as the receiver type is consistent with the refactoring. Ensure that all calls to this method are updated accordingly in other parts of the codebase.

#!/bin/bash
# Search for any remaining usage of StoreComponent.RestoreSnapshotCmd
rg "StoreComponent\[.\]\.RestoreSnapshotCmd" --type go

142-142: LGTM. Verify usage in other parts of the codebase.

The change from StoreComponent[T] to Server[T] as the receiver type is consistent with the refactoring. Ensure that all calls to this method are updated accordingly in other parts of the codebase.

#!/bin/bash
# Search for any remaining usage of StoreComponent.DeleteSnapshotCmd
rg "StoreComponent\[.\]\.DeleteSnapshotCmd" --type go

116-116: LGTM. Verify usage in other parts of the codebase.

The change from StoreComponent[T] to Server[T] as the receiver type is consistent with the refactoring. Ensure that all calls to this method are updated accordingly in other parts of the codebase.

#!/bin/bash
# Search for any remaining usage of StoreComponent.ListSnapshotsCmd
rg "StoreComponent\[.\]\.ListSnapshotsCmd" --type go

Line range hint 28-263: Overall LGTM. Comprehensive codebase check recommended.

The changes in this file consistently update the receiver type from StoreComponent[T] to Server[T] for all snapshot-related functions. This refactoring appears to be part of a larger architectural change. While the individual function implementations remain unchanged, it's crucial to ensure that all references to these methods throughout the codebase are updated to use the new Server[T] type.

#!/bin/bash
# Search for any remaining usage of StoreComponent in the entire codebase
rg "StoreComponent\[.\]" --type go

# List all files that still contain StoreComponent
rg -l "StoreComponent\[.\]" --type go

# Check for any TODO or FIXME comments related to this refactoring
rg "(TODO|FIXME).*StoreComponent" --type go

Consider running these checks and updating any remaining references to StoreComponent in the codebase to maintain consistency with this refactoring.


263-263: LGTM. Verify usage in other parts of the codebase.

The change from StoreComponent[T] to Server[T] as the receiver type is consistent with the refactoring. Ensure that all calls to this method are updated accordingly in other parts of the codebase.

#!/bin/bash
# Search for any remaining usage of StoreComponent.LoadArchiveCmd
rg "StoreComponent\[.\]\.LoadArchiveCmd" --type go

170-170: LGTM. Verify usage in other parts of the codebase.

The change from StoreComponent[T] to Server[T] as the receiver type is consistent with the refactoring. Ensure that all calls to this method are updated accordingly in other parts of the codebase.

#!/bin/bash
# Search for any remaining usage of StoreComponent.DumpArchiveCmd
rg "StoreComponent\[.\]\.DumpArchiveCmd" --type go

28-28: LGTM. Verify usage in other parts of the codebase.

The change from StoreComponent[T] to Server[T] as the receiver type is consistent with the refactoring. Ensure that all calls to this method are updated accordingly in other parts of the codebase.

✅ Verification successful

Verification Successful: All usages of StoreComponent.ExportSnapshotCmd have been updated to use Server[T]. No issues found.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for any remaining usage of StoreComponent.ExportSnapshotCmd
rg "StoreComponent\[.\]\.ExportSnapshotCmd" --type go

Length of output: 122


Script:

#!/bin/bash
# Broad search for any usage of ExportSnapshotCmd regardless of receiver type
rg "\.ExportSnapshotCmd\(" --type go

#!/bin/bash
# Use ast-grep to find all method calls to ExportSnapshotCmd
ast-grep --lang go --pattern 'CallExpr (selector: "ExportSnapshotCmd")' .

Length of output: 166

server/v2/api/telemetry/server.go Show resolved Hide resolved
server/v2/api/telemetry/server.go Show resolved Hide resolved
server/v2/api/grpcgateway/server.go Show resolved Hide resolved
server/v2/api/grpc/server.go Show resolved Hide resolved
@julienrbrt julienrbrt added this pull request to the merge queue Sep 27, 2024
@julienrbrt julienrbrt added the backport/v0.52.x PR scheduled for inclusion in the v0.52's next stable release label Sep 27, 2024
Merged via the queue into main with commit 7fe95fc Sep 27, 2024
74 of 75 checks passed
@julienrbrt julienrbrt deleted the julien/wire-telemetry branch September 27, 2024 16:58
mergify bot pushed a commit that referenced this pull request Sep 27, 2024
(cherry picked from commit 7fe95fc)

# Conflicts:
#	server/v2/api/grpc/config.go
#	server/v2/api/grpc/server.go
#	server/v2/api/grpcgateway/config.go
#	server/v2/api/grpcgateway/server.go
#	server/v2/api/telemetry/config.go
#	server/v2/api/telemetry/metrics.go
#	server/v2/api/telemetry/server.go
#	server/v2/go.mod
#	server/v2/go.sum
#	server/v2/store/commands.go
#	server/v2/store/server.go
#	server/v2/store/snapshot.go
julienrbrt added a commit that referenced this pull request Sep 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport/v0.52.x PR scheduled for inclusion in the v0.52's next stable release C:server/v2 api C:server/v2 cometbft C:server/v2 Issues related to server/v2
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Wire telemetry in server/v2
7 participants