-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(flink): Add logs command (#807)
* feat(flink): Add logs command * chore: update docs * chore: update to upstream meroxa-go
- Loading branch information
Showing
65 changed files
with
367 additions
and
111 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
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,98 @@ | ||
/* | ||
Copyright © 2022 Meroxa Inc | ||
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 flink | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
"github.com/meroxa/cli/cmd/meroxa/builder" | ||
"github.com/meroxa/cli/log" | ||
"github.com/meroxa/cli/utils/display" | ||
"github.com/meroxa/meroxa-go/pkg/meroxa" | ||
) | ||
|
||
var ( | ||
_ builder.CommandWithAliases = (*Logs)(nil) | ||
_ builder.CommandWithDocs = (*Logs)(nil) | ||
_ builder.CommandWithArgs = (*Logs)(nil) | ||
_ builder.CommandWithClient = (*Logs)(nil) | ||
_ builder.CommandWithLogger = (*Logs)(nil) | ||
_ builder.CommandWithExecute = (*Logs)(nil) | ||
) | ||
|
||
type Logs struct { | ||
client flinkLogsClient | ||
logger log.Logger | ||
|
||
args struct { | ||
NameOrUUID string | ||
} | ||
} | ||
|
||
type flinkLogsClient interface { | ||
GetFlinkLogsV2(ctx context.Context, nameOrUUID string) (*meroxa.Logs, error) | ||
} | ||
|
||
func (*Logs) Aliases() []string { | ||
return []string{"log"} | ||
} | ||
|
||
func (l *Logs) Usage() string { | ||
return `logs [NameOrUUID] [--path pwd]` | ||
} | ||
|
||
func (l *Logs) Docs() builder.Docs { | ||
return builder.Docs{ | ||
Short: "View relevant logs to the state of the given Flink Job", | ||
Example: `meroxa jobs logs my-flink-job-name | ||
meroxa jobs logs my-flink-job-uuid`, | ||
} | ||
} | ||
|
||
func (l *Logs) Execute(ctx context.Context) error { | ||
nameOrUUID := l.args.NameOrUUID | ||
|
||
appLogs, getErr := l.client.GetFlinkLogsV2(ctx, nameOrUUID) | ||
if getErr != nil { | ||
return getErr | ||
} | ||
|
||
output := display.LogsTable(appLogs) | ||
|
||
l.logger.Info(ctx, output) | ||
l.logger.JSON(ctx, appLogs) | ||
|
||
return nil | ||
} | ||
|
||
func (l *Logs) Client(client meroxa.Client) { | ||
l.client = client | ||
} | ||
|
||
func (l *Logs) Logger(logger log.Logger) { | ||
l.logger = logger | ||
} | ||
|
||
func (l *Logs) ParseArgs(args []string) error { | ||
if len(args) < 1 { | ||
return errors.New("requires Flink Job name or UUID") | ||
} | ||
|
||
l.args.NameOrUUID = args[0] | ||
return 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,104 @@ | ||
package flink | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"reflect" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"github.com/meroxa/cli/utils" | ||
|
||
"github.com/golang/mock/gomock" | ||
"github.com/google/go-cmp/cmp" | ||
"github.com/meroxa/cli/log" | ||
"github.com/meroxa/cli/utils/display" | ||
"github.com/meroxa/meroxa-go/pkg/meroxa" | ||
"github.com/meroxa/meroxa-go/pkg/mock" | ||
) | ||
|
||
func TestLogsFlinkJobArgs(t *testing.T) { | ||
tests := []struct { | ||
args []string | ||
err error | ||
name string | ||
}{ | ||
{args: nil, err: errors.New("requires Flink Job name or UUID"), name: ""}, | ||
{args: []string{"job-name"}, err: nil, name: "job-name"}, | ||
} | ||
|
||
for _, tt := range tests { | ||
l := &Logs{} | ||
err := l.ParseArgs(tt.args) | ||
|
||
if err != nil && tt.err.Error() != err.Error() { | ||
t.Fatalf("expected \"%s\" got \"%s\"", tt.err, err) | ||
} | ||
|
||
if tt.name != l.args.NameOrUUID { | ||
t.Fatalf("expected \"%s\" got \"%s\"", tt.name, l.args.NameOrUUID) | ||
} | ||
} | ||
} | ||
|
||
func TestFlinkLogsExecution(t *testing.T) { | ||
ctx := context.Background() | ||
ctrl := gomock.NewController(t) | ||
client := mock.NewMockClient(ctrl) | ||
logger := log.NewTestLogger() | ||
|
||
fj := utils.GenerateFlinkJob() | ||
|
||
flinkLogs := &meroxa.Logs{ | ||
Data: []meroxa.LogData{ | ||
{ | ||
Timestamp: time.Now().UTC(), | ||
Log: "log just logging", | ||
Source: "connector", | ||
}, | ||
{ | ||
Timestamp: time.Now().UTC(), | ||
Log: "another log", | ||
Source: "flink-job", | ||
}, | ||
}, | ||
Metadata: meroxa.Metadata{ | ||
End: time.Now().UTC(), | ||
Start: time.Now().UTC().Add(-12 * time.Hour), | ||
Limit: 10, | ||
}, | ||
} | ||
|
||
client.EXPECT().GetFlinkLogsV2(ctx, fj.Name).Return(flinkLogs, nil) | ||
|
||
l := &Logs{ | ||
client: client, | ||
logger: logger, | ||
} | ||
l.args.NameOrUUID = fj.Name | ||
|
||
err := l.Execute(ctx) | ||
if err != nil { | ||
t.Fatalf("not expected error, got %q", err.Error()) | ||
} | ||
|
||
gotLeveledOutput := logger.LeveledOutput() | ||
wantLeveledOutput := display.LogsTable(flinkLogs) | ||
|
||
if !strings.Contains(gotLeveledOutput, wantLeveledOutput) { | ||
t.Fatalf(cmp.Diff(wantLeveledOutput, gotLeveledOutput)) | ||
} | ||
|
||
gotJSONOutput := logger.JSONOutput() | ||
var gotAppLogs meroxa.Logs | ||
err = json.Unmarshal([]byte(gotJSONOutput), &gotAppLogs) | ||
if err != nil { | ||
t.Fatalf("not expected error, got %q", err.Error()) | ||
} | ||
|
||
if !reflect.DeepEqual(gotAppLogs, *flinkLogs) { | ||
t.Fatalf(cmp.Diff(*flinkLogs, gotAppLogs)) | ||
} | ||
} |
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
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
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
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
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.