forked from harness/harness
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: [CDE-127]: Adding changes to include logging in container orche…
…stration. (harness#2178) * feat: [CDE-127]: Linting. * Merge branch 'refs/heads/main' into CDE-127 # Conflicts: # app/gitspace/orchestrator/container/embedded_docker.go # cmd/gitness/wire.go * feat: [CDE-127]: Addressing review comments. * feat: [CDE-127]: Removing streams map from StatefulLogger. Flushing the stream every time the invoking function is closed. * feat: [CDE-127]: Removing streams map from StatefulLogger. Flushing the stream every time the invoking function is closed. * feat: [CDE-127]: Removing streams map from StatefulLogger. Flushing the stream every time the invoking function is closed. * feat: [CDE-127]: Removing streams map from StatefulLogger. Flushing the stream every time the invoking function is closed. * feat: [CDE-127]: Adding flush stream logic to delete gitspace in orchestrator. Linting. * feat: [CDE-127]: Adding flush stream logic to delete gitspace in orchestrator. Linting. * feat: [CDE-127]: Addressing review comments. * feat: [CDE-127]: Adding changes to make logutil.StatefulLogger a wrapper on livelog.LogStream and adding initialisation and flush functions. * feat: [CDE-127]: Adding changes to make logutil.StatefulLogger a wrapper on livelog.LogStream and adding initialisation and flush functions. * feat: [CDE-127]: Adding changes to include logging in container orchestration's stop flow. * feat: [CDE-127]: Adding changes to include logging in container orchestration's start flow.
- Loading branch information
1 parent
bf0890b
commit d577102
Showing
7 changed files
with
492 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright 2023 Harness, 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 logutil | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
type reusableScanner struct { | ||
scanner *bufio.Scanner | ||
reader *strings.Reader | ||
} | ||
|
||
func newReusableScanner() *reusableScanner { | ||
reader := strings.NewReader("") | ||
scanner := bufio.NewScanner(reader) | ||
return &reusableScanner{ | ||
scanner: scanner, | ||
reader: reader, | ||
} | ||
} | ||
|
||
func (r *reusableScanner) scan(input string) ([]string, error) { | ||
r.reader.Reset(input) | ||
var lines []string | ||
|
||
for r.scanner.Scan() { | ||
lines = append(lines, r.scanner.Text()) | ||
} | ||
|
||
if err := r.scanner.Err(); err != nil { | ||
return nil, fmt.Errorf("error reading string %s: %w", input, err) | ||
} | ||
|
||
return lines, 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,106 @@ | ||
// Copyright 2023 Harness, 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 logutil | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/harness/gitness/livelog" | ||
) | ||
|
||
const offset int64 = 1000000000 | ||
|
||
// StatefulLogger is a wrapper on livelog.Logstream. It is used to create stateful instances of LogStreamInstance. | ||
type StatefulLogger struct { | ||
logz livelog.LogStream | ||
} | ||
|
||
// LogStreamInstance is a stateful instance of the livelog.LogStream. It keeps track of the position & log key (id). | ||
type LogStreamInstance struct { | ||
ctx context.Context | ||
id int64 | ||
offsetID int64 | ||
position int | ||
scanner *reusableScanner | ||
logz livelog.LogStream | ||
} | ||
|
||
func NewStatefulLogger(logz livelog.LogStream) *StatefulLogger { | ||
return &StatefulLogger{ | ||
logz: logz, | ||
} | ||
} | ||
|
||
// GetLogStream returns an instance of LogStreamInstance tied to the given id. | ||
func (s *StatefulLogger) CreateLogStream(ctx context.Context, id int64) (*LogStreamInstance, error) { | ||
// TODO: As livelog.LogStreamInstance uses only a single id as key, conflicts are likely if pipelines and gitspaces | ||
// are used in the same instance of Gitness. We need to update the underlying implementation to use another unique | ||
// key. To avoid that, we offset the ID by offset (1000000000). | ||
offsetID := offset + id | ||
|
||
// Create new logstream | ||
err := s.logz.Create(ctx, offsetID) | ||
if err != nil { | ||
return nil, fmt.Errorf("error creating log stream for ID %d: %w", id, err) | ||
} | ||
|
||
newStream := &LogStreamInstance{ | ||
id: id, | ||
offsetID: offsetID, | ||
ctx: ctx, | ||
scanner: newReusableScanner(), | ||
logz: s.logz, | ||
} | ||
|
||
return newStream, nil | ||
} | ||
|
||
// Write writes the msg into the underlying log stream. | ||
func (l *LogStreamInstance) Write(msg string) error { | ||
lines, err := l.scanner.scan(msg) | ||
if err != nil { | ||
return fmt.Errorf("error parsing log lines %s: %w", msg, err) | ||
} | ||
|
||
for _, line := range lines { | ||
err = l.logz.Write( | ||
l.ctx, | ||
l.offsetID, | ||
&livelog.Line{ | ||
Number: l.position, | ||
Message: line, | ||
Timestamp: time.Now().UnixMilli(), | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("could not write log %s for ID %d at pos %d: %w", line, l.id, l.position, err) | ||
} | ||
|
||
l.position++ | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// Flush deletes the underlying stream. | ||
func (l *LogStreamInstance) Flush() error { | ||
err := l.logz.Delete(l.ctx, l.offsetID) | ||
if err != nil { | ||
return fmt.Errorf("failed to delete old log stream for ID %d: %w", l.id, err) | ||
} | ||
|
||
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,29 @@ | ||
// Copyright 2023 Harness, 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 logutil | ||
|
||
import ( | ||
"github.com/harness/gitness/livelog" | ||
|
||
"github.com/google/wire" | ||
) | ||
|
||
var WireSet = wire.NewSet( | ||
ProvideStatefulLogger, | ||
) | ||
|
||
func ProvideStatefulLogger(logz livelog.LogStream) *StatefulLogger { | ||
return NewStatefulLogger(logz) | ||
} |
Oops, something went wrong.