Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/workflows/ci-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ jobs:

# create a dev tag for every branch except master
tag_version:
needs: test
if: ${{ github.ref_type == 'branch' && github.ref_name != 'master' }}
runs-on: ubuntu-latest
outputs:
Expand Down
2 changes: 1 addition & 1 deletion agent/cmd_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"syscall"
)

func setSysProcAttributes(cmd *exec.Cmd) {
func SetSysProcAttributes(cmd *exec.Cmd) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like these should go into a new package in internal sys or something

cmd.SysProcAttr = &syscall.SysProcAttr{
Setpgid: true,
Pgid: 0,
Expand Down
2 changes: 1 addition & 1 deletion agent/cmd_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"golang.org/x/sys/windows"
)

func setSysProcAttributes(cmd *exec.Cmd) {
func SetSysProcAttributes(cmd *exec.Cmd) {
cmd.SysProcAttr = &syscall.SysProcAttr{
HideWindow: true,
CreationFlags: windows.DETACHED_PROCESS | windows.CREATE_NEW_PROCESS_GROUP,
Expand Down
2 changes: 1 addition & 1 deletion agent/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func StartDaemon(ctx context.Context) (*Client, error) {
env = append(env, fmt.Sprintf("FLY_API_TOKEN=%s", config.Tokens(ctx).GraphQL()))
cmd.Env = env

setSysProcAttributes(cmd)
SetSysProcAttributes(cmd)

if err := cmd.Start(); err != nil {
err = forkError{err}
Expand Down
2 changes: 1 addition & 1 deletion internal/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func newRunE(fn Runner, preparers ...preparers.Preparer) func(*cobra.Command, []
io := iostreams.FromContext(ctx)

if !metrics.IsFlushMetricsDisabled(ctx) {
err := metrics.FlushMetrics()
err := metrics.FlushMetrics(ctx)
if err != nil {
fmt.Fprintln(io.ErrOut, "Error spawning metrics process: ", err)
}
Expand Down
35 changes: 1 addition & 34 deletions internal/command/metrics/metrics.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
package metrics

import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"time"

"github.com/PuerkitoBio/rehttp"
"github.com/spf13/cobra"
"github.com/superfly/flyctl/internal/buildinfo"
"github.com/superfly/flyctl/internal/command"
"github.com/superfly/flyctl/internal/config"
"github.com/superfly/flyctl/internal/metrics"
"github.com/superfly/flyctl/iostreams"
)
Expand Down Expand Up @@ -60,31 +53,5 @@ func run(ctx context.Context) error {

stdin_value := string(stdin_bytes)

authToken, err := metrics.GetMetricsToken(ctx)
if err != nil {
return err
}

cfg := config.FromContext(ctx)
request, err := http.NewRequest("POST", cfg.MetricsBaseURL+"/metrics_post", bytes.NewBuffer([]byte(stdin_value)))
if err != nil {
return err
}

request.Header.Set("Authorization", authToken)
request.Header.Set("User-Agent", fmt.Sprintf("flyctl/%s", buildinfo.Info().Version))

retryTransport := rehttp.NewTransport(http.DefaultTransport, rehttp.RetryAll(rehttp.RetryMaxRetries(3), rehttp.RetryTimeoutErr()), rehttp.ConstDelay(time.Second))

client := http.Client{
Transport: retryTransport,
Timeout: time.Second * 30,
}

resp, err := client.Do(request)
if err != nil {
return err
}

return resp.Body.Close()
return metrics.SendMetrics(ctx, stdin_value)
}
4 changes: 3 additions & 1 deletion internal/ctrlc/ctrlc.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ctrlc

import (
"context"
"fmt"
"os"
"os/signal"
Expand Down Expand Up @@ -45,7 +46,8 @@ func Hook(event func()) Handle {
// most terminals print ^C, this makes things easier to read.
fmt.Fprintf(os.Stderr, "\n")
}
metrics.FlushMetrics()
ctx := context.Background()
metrics.FlushMetrics(ctx)
event()
}()
return Handle{&boundSignal{sig: signalCh}}
Expand Down
80 changes: 68 additions & 12 deletions internal/metrics/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,20 @@ package metrics

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"time"

"github.com/PuerkitoBio/rehttp"
"github.com/superfly/flyctl/agent"
"github.com/superfly/flyctl/internal/buildinfo"
"github.com/superfly/flyctl/internal/config"
"github.com/superfly/flyctl/iostreams"
)

var metrics []metricsMessage = make([]metricsMessage, 0)
Expand All @@ -14,35 +25,80 @@ func queueMetric(metric metricsMessage) {
}

// Spawns a forked `flyctl metrics send` process that sends metrics to the flyctl-metrics server
func FlushMetrics() error {
func FlushMetrics(ctx context.Context) error {
json, err := json.Marshal(metrics)
if err != nil {
return err

}

flyctl, err := os.Executable()
iostream := iostreams.FromContext(ctx)

// On CI, always block on metrics send. This sucks, but the alternative is not getting metrics from CI at all. There are timeouts in place to prevent this from taking more than 15 seconds

if iostream.IsInteractive() {
flyctl, err := os.Executable()
if err != nil {
return err
}

cmd := exec.Command(flyctl, "metrics", "send")
stdin, err := cmd.StdinPipe()
if err != nil {
return err
}

go func() {
io.WriteString(stdin, string(json))
stdin.Close()
}()

cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, "FLY_NO_UPDATE_CHECK=1")

agent.SetSysProcAttributes(cmd)

if err := cmd.Start(); err != nil {
return err
}

if err := cmd.Process.Release(); err != nil {
return err
}
} else {
SendMetrics(ctx, string(json))
}

return nil
}

// / Spens up to 15 seconds sending all metrics collected so far to flyctl-metrics post endpoint
func SendMetrics(ctx context.Context, json string) error {
authToken, err := GetMetricsToken(ctx)
if err != nil {
return err
}

cmd := exec.Command(flyctl, "metrics", "send")
cfg := config.FromContext(ctx)
request, err := http.NewRequest("POST", cfg.MetricsBaseURL+"/metrics_post", bytes.NewBuffer([]byte(json)))
if err != nil {
return err
}

buffer := bytes.Buffer{}
buffer.Write(json)
request.Header.Set("Authorization", authToken)
request.Header.Set("User-Agent", fmt.Sprintf("flyctl/%s", buildinfo.Info().Version))

cmd.Stdin = &buffer
cmd.Env = os.Environ()
retryTransport := rehttp.NewTransport(http.DefaultTransport, rehttp.RetryAll(rehttp.RetryMaxRetries(3), rehttp.RetryTimeoutErr()), rehttp.ConstDelay(0))

err = cmd.Start()
if err != nil {
return err
client := http.Client{
Transport: retryTransport,
Timeout: time.Second * 5,
}

err = cmd.Process.Release()
resp, err := client.Do(request)
if err != nil {
return err
}

return nil
return resp.Body.Close()
}