Add initiator field and parse url (#3558)

- Add initiator field to flow proto
- Parse URL
- Update a few trace logs
This commit is contained in:
Maycon Santos
2025-03-21 14:47:04 +01:00
committed by GitHub
parent cb318b7ef4
commit d9d051cb1e
4 changed files with 89 additions and 69 deletions

View File

@ -6,7 +6,7 @@ import (
"crypto/x509"
"errors"
"fmt"
"strings"
"net/url"
"sync"
"time"
@ -31,9 +31,12 @@ type GRPCClient struct {
}
func NewClient(addr, payload, signature string, interval time.Duration) (*GRPCClient, error) {
parsedURL, err := url.Parse(addr)
if err != nil {
return nil, fmt.Errorf("parsing url: %w", err)
}
var opts []grpc.DialOption
if strings.Contains(addr, "443") {
if parsedURL.Scheme == "https" {
certPool, err := x509.SystemCertPool()
if err != nil || certPool == nil {
log.Debugf("System cert pool not available; falling back to embedded cert, error: %v", err)
@ -58,7 +61,7 @@ func NewClient(addr, payload, signature string, interval time.Duration) (*GRPCCl
grpc.WithDefaultServiceConfig(`{"healthCheckConfig": {"serviceName": ""}}`),
)
conn, err := grpc.NewClient(addr, opts...)
conn, err := grpc.NewClient(fmt.Sprintf("%s:%s", parsedURL.Hostname(), parsedURL.Port()), opts...)
if err != nil {
return nil, fmt.Errorf("creating new grpc client: %w", err)
}
@ -100,6 +103,11 @@ func (c *GRPCClient) establishStreamAndReceive(ctx context.Context, msgHandler f
return fmt.Errorf("create event stream: %w", err)
}
err = stream.Send(&proto.FlowEvent{IsInitiator: true})
if err != nil {
log.Infof("failed to send initiator message to flow receiver but will attempt to continue. Error: %s", err)
}
if err = checkHeader(stream); err != nil {
return fmt.Errorf("check header: %w", err)
}