Skip to content

Commit

Permalink
Merge pull request #81 from logfire-ai/repo-sync/cli-private/main
Browse files Browse the repository at this point in the history
🔄 synced file(s) with logfire-ai/cli-private
  • Loading branch information
rahulprakash11 authored Jun 12, 2024
2 parents 7833191 + a4369b8 commit b21c8c1
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 46 deletions.
4 changes: 2 additions & 2 deletions internal/prompter/tea_onboarding_form.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,8 @@ func waitForLog(m *model) {
go grpcutil.GetLog(m.config, m.config.Get().Token, m.config.Get().EndPoint, m.config.Get().TeamId, m.config.Get().AccountId, m.sourceId, m.sourceToken, stop)
err := <-stop
if err != nil {
m.err = err
// m.err = errors.New("we apologize for the inconvenience. There seems to be an error on our end or with our server.\nPlease try again later or contact our support team for assistance")
// m.err = err
m.err = errors.New("we apologize for the inconvenience. There seems to be an error on our end or with our server.\nPlease try again later or contact our support team for assistance")
m.nextInput()
}
subStep = "awesome"
Expand Down
93 changes: 93 additions & 0 deletions pkg/cmdutil/APICalls/log_ingest_api_calls.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package APICalls

import (
"bytes"
"context"
"encoding/json"
"fmt"
"log"
"os"
"strings"

"io"
"net/http"
"time"
)

type LogMessage struct {
Dt string `json:"dt"`
Message string `json:"message"`
}

func LogIngestFlow(endpoint, sourceToken string) (string, error) {
istLocation, err := time.LoadLocation("UTC")
if err != nil {
return "", fmt.Errorf("Error loading IST location: %v", err)
}

currentTime := time.Now().In(istLocation)
formattedTime := currentTime.Format("2006-01-02 15:04:05")

logMessage := []LogMessage{
{
Dt: formattedTime,
Message: "Hello from Logfire!",
},
}

reqBody, err := json.Marshal(logMessage)
if err != nil {
return "", fmt.Errorf("Failed to marshal request body: %v", err)
}

ctxCmd, cancelCmd := context.WithTimeout(context.Background(), 5*time.Second)
defer cancelCmd()

url := endpoint

transport := &http.Transport{
IdleConnTimeout: 30 * time.Second,
MaxIdleConns: 100,
MaxConnsPerHost: 0,
DisableKeepAlives: false,
}

client := &http.Client{
Transport: transport,
Timeout: 10 * time.Second,
}

req, err := http.NewRequestWithContext(ctxCmd, "POST", url, bytes.NewBuffer(reqBody))
if err != nil {
return "", fmt.Errorf("Failed to create request: %v", err)
}
req.Header.Set("User-Agent", "Logfire-cli")
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", sourceToken))

resp, err := client.Do(req)
if err != nil {
if strings.Contains(err.Error(), "no such host") {
fmt.Printf("\nError: Connection failed (Server down or no internet)\n")
os.Exit(1)
}
return "", fmt.Errorf("Failed to execute request: %v", err)
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
log.Printf("Failed to close response body: %v", err)
}
}(resp.Body)

body, err := io.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("Failed to read response body: %v", err)
}

if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("Request failed with status code %d: %s", resp.StatusCode, string(body))
}

return string(body), nil
}
50 changes: 6 additions & 44 deletions pkg/cmdutil/grpcutil/onboarding_wait_for_log.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package grpcutil

import (
"bytes"
"context"
"fmt"
"log"
"os/exec"
"time"

"github.com/logfire-sh/cli/internal/config"
Expand Down Expand Up @@ -37,15 +34,6 @@ func GetLog(config config.Config, token, endpoint, teamId, accountId, sourceId,
pbSources := CreateGrpcSource(sources)
request.Sources = pbSources

// send test log

istLocation, err := time.LoadLocation("UTC")
if err != nil {
log.Println("Error loading IST location:", err)
stop <- err
return
}

filterService := NewFilterService()
defer filterService.CloseConnection()

Expand All @@ -55,40 +43,14 @@ func GetLog(config config.Config, token, endpoint, teamId, accountId, sourceId,
stop <- nil
return
default:
currentTime := time.Now().In(istLocation)
formattedTime := currentTime.Format("2006-01-02 15:04:05")

ctxCmd, cancelCmd := context.WithTimeout(context.Background(), 5*time.Second)
defer cancelCmd()

cmd := exec.CommandContext(ctxCmd, "curl",
"--location",
config.Get().GrpcIngestion,
"--header", "Content-Type: application/json",
"--header", fmt.Sprintf("Authorization: Bearer %s", sourceToken),
"--data", fmt.Sprintf("[{\"dt\":\"%s\",\"message\":\"%s\"}]", formattedTime, "Hello from Logfire!"),
)

var out bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &out

// Start the curl command
if err := cmd.Start(); err != nil {
log.Println("Error starting curl command:", err)
stop <- err
return
_, err := APICalls.LogIngestFlow(config.Get().GrpcIngestion, sourceToken)
if err != nil {
log.Printf("Error: %s", err.Error())
continue
}

// Wait for the curl command to complete
if err := cmd.Wait(); err != nil && err.Error() != "signal: killed" {
log.Println("Error waiting for curl command:", err)
log.Println("Curl output:", out.String())
stop <- err
return
}
// Allow the command to run for a short period before cancelling it
time.Sleep(1100 * time.Millisecond)
// wait some time to process log messages
time.Sleep(1 * time.Second)

// Check response from filter service
response, err := filterService.Client.GetFilteredData(context.Background(), request)
Expand Down

0 comments on commit b21c8c1

Please sign in to comment.