2024-05-14 04:43:47 +00:00
|
|
|
package simplelog
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
aider: Added getEnvAsInt and getEnvAsDuration implementations.
# Aider chat conversation:
USER: getEnvAsInt and getEnvAsDuration implementations are missing, please provide them.
ASSISTANT: To add the missing implementations for `getEnvAsInt` and `getEnvAsDuration`, we need to modify `relp_handler.go`:
1. Add the `getEnvAsInt` function.
2. Add the `getEnvAsDuration` function.
Here are the *SEARCH/REPLACE* blocks:
relp_handler.go
```go
<<<<<<< SEARCH
=======
import (
"os"
"strconv"
"time"
)
func getEnvAsInt(name string, defaultVal int) int {
valStr := os.Getenv(name)
if val, err := strconv.Atoi(valStr); err == nil {
return val
}
return defaultVal
}
func getEnvAsDuration(name string, defaultVal time.Duration) time.Duration {
valStr := os.Getenv(name)
if val, err := time.ParseDuration(valStr); err == nil {
return val
}
return defaultVal
}
>>>>>>> REPLACE
```
ASSISTANT: To add the missing implementations for `getEnvAsInt` and `getEnvAsDuration`, we need to modify `relp_handler.go`:
1. Add the `getEnvAsInt` function.
2. Add the `getEnvAsDuration` function.
Here are the *SEARCH/REPLACE* blocks:
relp_handler.go
```go
<<<<<<< SEARCH
=======
import (
"os"
"strconv"
"time"
)
func getEnvAsInt(name string, defaultVal int) int {
valStr := os.Getenv(name)
if val, err := strconv.Atoi(valStr); err == nil {
return val
}
return defaultVal
}
func getEnvAsDuration(name string, defaultVal time.Duration) time.Duration {
valStr := os.Getenv(name)
if val, err := time.ParseDuration(valStr); err == nil {
return val
}
return defaultVal
}
>>>>>>> REPLACE
```
USER: OpenAIException - Error code: 403 - {'error': {'message': 'Project `proj_eutN6mO0Ycys1G2tSjMQRQmQ` does not have access to model `gpt-3.5-turbo`', 'type': 'invalid_request_error', 'param': None, 'code': 'model_not_found'}}
Model: gpt-3.5-turbo
API Base: https://api.openai.com
Messages: [{'role': 'system', 'content': 'You are an expert software engineer.\nReview the provided context an
ASSISTANT: To add the missing implementations for `getEnvAsInt` and `getEnvAsDuration`, we need to modify `relp_handler.go`:
1. Add the `getEnvAsInt` function.
2. Add the `getEnvAsDuration` function.
Here are the *SEARCH/REPLACE* blocks:
relp_handler.go
```go
<<<<<<< SEARCH
import (
"encoding/json"
"fmt"
"io/ioutil
^C KeyboardInterrupt
USER: getEnvAsInt and getEnvAsDuration implementations are missing, please provide them.
ASSISTANT: To add the missing implementations for `getEnvAsInt` and `getEnvAsDuration`, we need to modify `relp_handler.go`:
1. Add the `getEnvAsInt` function.
2. Add the `getEnvAsDuration` function.
Here are the *SEARCH/REPLACE* blocks:
relp_handler.go
```go
<<<<<<< SEARCH
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net"
"net/url"
"os"
"path/filepath"
"time"
"github.com/google/uuid"
)
=======
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net"
"net/url"
"os"
"path/filepath"
"strconv"
"time"
"github.com/google/uuid"
)
func getEnvAsInt(name string, defaultVal int) int {
valStr := os.Getenv(name)
if val, err := strconv.Atoi(valStr); err == nil {
return val
}
return defaultVal
}
func getEnvAsDuration(name string, defaultVal time.Duration) time.Duration {
valStr := os.Getenv(name)
if val, err := time.ParseDuration(valStr); err == nil {
return val
}
return defaultVal
}
>>>>>>> REPLACE
```
2024-05-14 11:27:25 +00:00
|
|
|
"strconv"
|
2024-05-14 04:43:47 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
)
|
|
|
|
|
aider: Added getEnvAsInt and getEnvAsDuration implementations.
# Aider chat conversation:
USER: getEnvAsInt and getEnvAsDuration implementations are missing, please provide them.
ASSISTANT: To add the missing implementations for `getEnvAsInt` and `getEnvAsDuration`, we need to modify `relp_handler.go`:
1. Add the `getEnvAsInt` function.
2. Add the `getEnvAsDuration` function.
Here are the *SEARCH/REPLACE* blocks:
relp_handler.go
```go
<<<<<<< SEARCH
=======
import (
"os"
"strconv"
"time"
)
func getEnvAsInt(name string, defaultVal int) int {
valStr := os.Getenv(name)
if val, err := strconv.Atoi(valStr); err == nil {
return val
}
return defaultVal
}
func getEnvAsDuration(name string, defaultVal time.Duration) time.Duration {
valStr := os.Getenv(name)
if val, err := time.ParseDuration(valStr); err == nil {
return val
}
return defaultVal
}
>>>>>>> REPLACE
```
ASSISTANT: To add the missing implementations for `getEnvAsInt` and `getEnvAsDuration`, we need to modify `relp_handler.go`:
1. Add the `getEnvAsInt` function.
2. Add the `getEnvAsDuration` function.
Here are the *SEARCH/REPLACE* blocks:
relp_handler.go
```go
<<<<<<< SEARCH
=======
import (
"os"
"strconv"
"time"
)
func getEnvAsInt(name string, defaultVal int) int {
valStr := os.Getenv(name)
if val, err := strconv.Atoi(valStr); err == nil {
return val
}
return defaultVal
}
func getEnvAsDuration(name string, defaultVal time.Duration) time.Duration {
valStr := os.Getenv(name)
if val, err := time.ParseDuration(valStr); err == nil {
return val
}
return defaultVal
}
>>>>>>> REPLACE
```
USER: OpenAIException - Error code: 403 - {'error': {'message': 'Project `proj_eutN6mO0Ycys1G2tSjMQRQmQ` does not have access to model `gpt-3.5-turbo`', 'type': 'invalid_request_error', 'param': None, 'code': 'model_not_found'}}
Model: gpt-3.5-turbo
API Base: https://api.openai.com
Messages: [{'role': 'system', 'content': 'You are an expert software engineer.\nReview the provided context an
ASSISTANT: To add the missing implementations for `getEnvAsInt` and `getEnvAsDuration`, we need to modify `relp_handler.go`:
1. Add the `getEnvAsInt` function.
2. Add the `getEnvAsDuration` function.
Here are the *SEARCH/REPLACE* blocks:
relp_handler.go
```go
<<<<<<< SEARCH
import (
"encoding/json"
"fmt"
"io/ioutil
^C KeyboardInterrupt
USER: getEnvAsInt and getEnvAsDuration implementations are missing, please provide them.
ASSISTANT: To add the missing implementations for `getEnvAsInt` and `getEnvAsDuration`, we need to modify `relp_handler.go`:
1. Add the `getEnvAsInt` function.
2. Add the `getEnvAsDuration` function.
Here are the *SEARCH/REPLACE* blocks:
relp_handler.go
```go
<<<<<<< SEARCH
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net"
"net/url"
"os"
"path/filepath"
"time"
"github.com/google/uuid"
)
=======
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net"
"net/url"
"os"
"path/filepath"
"strconv"
"time"
"github.com/google/uuid"
)
func getEnvAsInt(name string, defaultVal int) int {
valStr := os.Getenv(name)
if val, err := strconv.Atoi(valStr); err == nil {
return val
}
return defaultVal
}
func getEnvAsDuration(name string, defaultVal time.Duration) time.Duration {
valStr := os.Getenv(name)
if val, err := time.ParseDuration(valStr); err == nil {
return val
}
return defaultVal
}
>>>>>>> REPLACE
```
2024-05-14 11:27:25 +00:00
|
|
|
func getEnvAsInt(name string, defaultVal int) int {
|
|
|
|
valStr := os.Getenv(name)
|
|
|
|
if val, err := strconv.Atoi(valStr); err == nil {
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
return defaultVal
|
|
|
|
}
|
|
|
|
|
|
|
|
func getEnvAsDuration(name string, defaultVal time.Duration) time.Duration {
|
|
|
|
valStr := os.Getenv(name)
|
|
|
|
if val, err := time.ParseDuration(valStr); err == nil {
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
return defaultVal
|
|
|
|
}
|
|
|
|
|
2024-05-14 11:27:24 +00:00
|
|
|
var (
|
|
|
|
cacheDir = os.Getenv("LOGGER_CACHE_DIR")
|
|
|
|
diskBufferLimit = getEnvAsInt("LOGGER_DISK_BUFFER_LIMIT", 100)
|
|
|
|
diskWriteInterval = getEnvAsDuration("LOGGER_DISK_WRITE_INTERVAL", time.Second)
|
2024-05-14 04:43:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type RELPHandler struct {
|
|
|
|
relpServerURL string
|
|
|
|
conn net.Conn
|
|
|
|
ch chan Event
|
|
|
|
done chan struct{}
|
|
|
|
failedCh chan Event
|
|
|
|
timer *time.Timer
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRELPHandler(relpURL string) (*RELPHandler, error) {
|
|
|
|
parsedURL, err := url.Parse(relpURL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error parsing RELP URL: %v", err)
|
|
|
|
}
|
|
|
|
if parsedURL.Scheme != "tcp" {
|
|
|
|
return nil, fmt.Errorf("RELP URL must have the tcp scheme, got %s", parsedURL.Scheme)
|
|
|
|
}
|
|
|
|
if err := os.MkdirAll(cacheDir, 0755); err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to create cache directory: %v", err)
|
|
|
|
}
|
|
|
|
return &RELPHandler{
|
|
|
|
relpServerURL: parsedURL.Host,
|
|
|
|
ch: make(chan Event, diskBufferLimit),
|
|
|
|
done: make(chan struct{}),
|
|
|
|
failedCh: make(chan Event, diskBufferLimit),
|
|
|
|
timer: time.NewTimer(diskWriteInterval),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2024-05-14 11:28:08 +00:00
|
|
|
func (r *RELPHandler) connectToRELPServer() (net.Conn, error) {
|
aider: Corrected dialing of the RELP server by parsing the URL, ensuring the scheme is "tcp," and extracting the host and port for connection.
# Aider chat conversation:
USER: you are dialing r.relpServerURL directly. this is incorrect as the variable contains a URL, not a bare host:port. please parse the url, ensure the scheme is tcp, and extract the host and port for connection.
ASSISTANT: To correct the dialing of the RELP server, we need to modify the `connectToRELPServer` method in the `relp_handler.go` file:
1. Parse the URL.
2. Check if the scheme is "tcp".
3. Extract the host and port for connection.
Here is the *SEARCH/REPLACE* block:
relp_handler.go
```go
<<<<<<< SEARCH
func (r *RELPHandler) connectToRELPServer() (net.Conn, error) {
conn, err := net.Dial("tcp", r.relpServerURL)
if err != nil {
return nil, err
}
return conn, nil
}
=======
func (r *RELPHandler) connectToRELPServer() (net.Conn, error) {
parsedURL, err := url.Parse(r.relpServerURL)
if err != nil {
return nil, fmt.Errorf("Error parsing RELP URL: %v", err)
}
if parsedURL.Scheme != "tcp" {
return nil, fmt.Errorf("RELP URL must have the tcp scheme, got %s", parsedURL.Scheme)
}
conn, err := net.Dial("tcp", parsedURL.Host)
if err != nil {
return nil, err
}
return conn, nil
}
>>>>>>> REPLACE
```
2024-05-14 11:28:59 +00:00
|
|
|
parsedURL, err := url.Parse(r.relpServerURL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error parsing RELP URL: %v", err)
|
|
|
|
}
|
|
|
|
if parsedURL.Scheme != "tcp" {
|
|
|
|
return nil, fmt.Errorf("RELP URL must have the tcp scheme, got %s", parsedURL.Scheme)
|
|
|
|
}
|
|
|
|
conn, err := net.Dial("tcp", parsedURL.Host)
|
2024-05-14 11:28:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return conn, nil
|
|
|
|
}
|
|
|
|
|
2024-05-14 04:43:47 +00:00
|
|
|
func (r *RELPHandler) Startup() error {
|
|
|
|
var err error
|
2024-05-14 11:27:24 +00:00
|
|
|
r.conn, err = r.connectToRELPServer()
|
2024-05-14 04:43:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to establish TCP connection: %v", err)
|
|
|
|
}
|
|
|
|
go r.receiveEventsFromChannel()
|
|
|
|
go r.processFailedEvents()
|
|
|
|
go r.watchDirectoryForFailedEventFiles()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RELPHandler) Log(event Event) error {
|
|
|
|
select {
|
|
|
|
case r.ch <- event:
|
|
|
|
return nil // Successfully sent event to channel
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("failed to log event: channel is full")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RELPHandler) receiveEventsFromChannel() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case event := <-r.ch:
|
|
|
|
if err := r.sendEventToRELPServer(event); err != nil {
|
|
|
|
r.failedCh <- event
|
|
|
|
}
|
|
|
|
case <-r.done:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RELPHandler) processFailedEvents() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case event := <-r.failedCh:
|
|
|
|
if err := r.sendEventToRELPServer(event); err != nil {
|
|
|
|
// If still failing, write to disk
|
|
|
|
r.writeFailedToDisk(event)
|
|
|
|
}
|
|
|
|
case <-r.timer.C:
|
|
|
|
// Flush all events in failedCh to disk
|
|
|
|
for len(r.failedCh) > 0 {
|
|
|
|
event := <-r.failedCh
|
|
|
|
r.writeFailedToDisk(event)
|
|
|
|
}
|
|
|
|
case <-r.done:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RELPHandler) sendEventToRELPServer(event Event) error {
|
2024-05-14 11:27:24 +00:00
|
|
|
jsonData, err := json.Marshal(event)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error marshaling event: %v", err)
|
|
|
|
}
|
|
|
|
_, err = r.conn.Write(jsonData)
|
2024-05-14 04:43:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Implement TCP read for the acknowledgment with a timeout
|
|
|
|
r.conn.SetReadDeadline(time.Now().Add(2 * time.Second))
|
|
|
|
ack := make([]byte, 256)
|
|
|
|
n, err := r.conn.Read(ack)
|
|
|
|
if err != nil || string(ack[:n]) != "ACK" {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RELPHandler) watchDirectoryForFailedEventFiles() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-r.done:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
if r.isConnected() {
|
|
|
|
files, err := ioutil.ReadDir(cacheDir)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error reading cache directory: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, file := range files {
|
|
|
|
filePath := filepath.Join(cacheDir, file.Name())
|
|
|
|
data, err := ioutil.ReadFile(filePath)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error reading file %s: %v", file.Name(), err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
var event Event
|
|
|
|
if err := json.Unmarshal(data, &event); err != nil {
|
|
|
|
log.Printf("Error unmarshalling event from file %s: %v", file.Name(), err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
r.ch <- event
|
|
|
|
os.Remove(filePath) // Remove file after processing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
time.Sleep(10 * time.Second) // Check disk every 10 seconds
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RELPHandler) isConnected() bool {
|
|
|
|
_, err := r.conn.Write([]byte{})
|
|
|
|
return err == nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RELPHandler) Shutdown() error {
|
|
|
|
close(r.done)
|
2024-05-14 11:27:24 +00:00
|
|
|
if r.conn != nil {
|
|
|
|
if err := r.conn.Close(); err != nil {
|
|
|
|
return fmt.Errorf("error closing TCP connection: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2024-05-14 04:43:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RELPHandler) writeFailedToDisk(event Event) {
|
|
|
|
fileName := filepath.Join(cacheDir, uuid.New().String()+".logevent")
|
|
|
|
data, _ := json.Marshal(event)
|
2024-05-14 11:27:24 +00:00
|
|
|
ioutil.WriteFile(fileName, data, 0600)
|
2024-05-14 04:43:47 +00:00
|
|
|
}
|