-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
2,032 additions
and
81 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"time" | ||
|
||
pixiecore "github.com/metal-stack/pixie/api" | ||
) | ||
|
||
func fetchMetalConfig(pixieURL string) (*pixiecore.MetalConfig, error) { | ||
certClient := http.Client{ | ||
Timeout: 5 * time.Second, | ||
} | ||
ctx, httpcancel := context.WithTimeout(context.Background(), 5*time.Second) | ||
defer httpcancel() | ||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, pixieURL, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
resp, err := certClient.Do(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
js, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
var metalConfig pixiecore.MetalConfig | ||
if err := json.Unmarshal(js, &metalConfig); err != nil { | ||
return nil, fmt.Errorf("unable to unmarshal grpcConfig:%w", err) | ||
} | ||
return &metalConfig, nil | ||
} |
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 |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"log/slog" | ||
|
||
"github.com/grafana/loki-client-go/loki" | ||
"github.com/metal-stack/pixie/api" | ||
promconfig "github.com/prometheus/common/config" | ||
slogloki "github.com/samber/slog-loki/v3" | ||
slogmulti "github.com/samber/slog-multi" | ||
) | ||
|
||
func AddRemoteHandler(spec *Specification, handler slog.Handler) (slog.Handler, error) { | ||
metalConfig := spec.MetalConfig | ||
if metalConfig.Logging == nil || metalConfig.Logging.Endpoint == "" { | ||
return handler, nil | ||
} | ||
if metalConfig.Logging.Type != api.LogTypeLoki { | ||
slog.New(handler).Error("unsupported remote logging type, ignoring", "type", metalConfig.Logging.Type) | ||
return handler, nil | ||
} | ||
httpClient := promconfig.DefaultHTTPClientConfig | ||
if metalConfig.Logging.BasicAuth != nil { | ||
httpClient.BasicAuth = &promconfig.BasicAuth{ | ||
Username: metalConfig.Logging.BasicAuth.User, | ||
Password: promconfig.Secret(metalConfig.Logging.BasicAuth.Password), | ||
} | ||
} | ||
if metalConfig.Logging.CertificateAuth != nil { | ||
httpClient.TLSConfig = promconfig.TLSConfig{ | ||
Cert: metalConfig.Logging.CertificateAuth.Cert, | ||
Key: promconfig.Secret(metalConfig.Logging.CertificateAuth.Key), | ||
InsecureSkipVerify: metalConfig.Logging.CertificateAuth.InsecureSkipVerify, | ||
} | ||
} | ||
|
||
config, err := loki.NewDefaultConfig(metalConfig.Logging.Endpoint) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to create loki default config %w", err) | ||
} | ||
// config.EncodeJson = true | ||
config.Client = httpClient | ||
client, err := loki.New(config) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to create loki client %w", err) | ||
} | ||
|
||
lokiHandler := slogloki.Option{ | ||
Level: slog.LevelDebug, | ||
Client: client}.NewLokiHandler().WithAttrs( | ||
[]slog.Attr{ | ||
{Key: "component", Value: slog.StringValue("metal-hammer")}, | ||
{Key: "machineID", Value: slog.StringValue(spec.MachineUUID)}, | ||
}, | ||
) | ||
mdw := slogmulti.NewHandleInlineMiddleware(jsonFormattingMiddleware) | ||
combinedHandler := slogmulti.Fanout(slogmulti.Pipe(mdw).Handler(lokiHandler), handler) | ||
return combinedHandler, nil | ||
} | ||
|
||
func jsonFormattingMiddleware(ctx context.Context, record slog.Record, next func(context.Context, slog.Record) error) error { | ||
attrs := map[string]string{"msg": record.Message, "level": record.Level.String(), "time": record.Time.String()} | ||
|
||
record.Attrs(func(attr slog.Attr) bool { | ||
attrs[attr.Key] = attr.Value.String() | ||
return true | ||
}) | ||
|
||
r, err := json.Marshal(attrs) | ||
if err != nil { | ||
return fmt.Errorf("unable to marshal log attributes %w", err) | ||
} | ||
record = slog.NewRecord(record.Time, record.Level, string(r), record.PC) | ||
return next(ctx, record) | ||
} |
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
Oops, something went wrong.