-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New cache CLI: publish metrics (#280)
- Loading branch information
Showing
12 changed files
with
420 additions
and
29 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
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,30 @@ | ||
package files | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"testing" | ||
|
||
"github.com/semaphoreci/toolbox/cache-cli/pkg/metrics" | ||
assert "github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test__UnpackSendsMetricsOnFailure(t *testing.T) { | ||
os.Setenv("SEMAPHORE_TOOLBOX_METRICS_ENABLED", "true") | ||
metricsManager, err := metrics.InitMetricsManager(metrics.LocalBackend) | ||
assert.Nil(t, err) | ||
|
||
tempFile, _ := ioutil.TempFile("/tmp", "*") | ||
tempFile.WriteString("this is not a proper archive") | ||
|
||
_, err = Unpack(metricsManager, tempFile.Name()) | ||
assert.NotNil(t, err) | ||
|
||
bytes, err := ioutil.ReadFile("/tmp/toolbox_metrics") | ||
assert.Nil(t, err) | ||
assert.Contains(t, string(bytes), fmt.Sprintf("%s 1", metrics.CacheCorruptionRate)) | ||
|
||
os.Remove(tempFile.Name()) | ||
os.Remove("/tmp/toolbox_metrics") | ||
} |
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,67 @@ | ||
package metrics | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
) | ||
|
||
type LocalMetricsManager struct { | ||
ToolboxMetricsPath string | ||
CacheMetricsPath string | ||
} | ||
|
||
func NewLocalMetricsBackend() (*LocalMetricsManager, error) { | ||
return &LocalMetricsManager{ | ||
ToolboxMetricsPath: "/tmp/toolbox_metrics", | ||
CacheMetricsPath: "/tmp/cache_metrics", | ||
}, nil | ||
} | ||
|
||
func (b *LocalMetricsManager) Enabled() bool { | ||
return os.Getenv("SEMAPHORE_TOOLBOX_METRICS_ENABLED") == "true" | ||
} | ||
|
||
func (b *LocalMetricsManager) PublishBatch(metrics []Metric) error { | ||
if !b.Enabled() { | ||
return nil | ||
} | ||
|
||
for _, metric := range metrics { | ||
err := b.Publish(metric) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (b *LocalMetricsManager) Publish(metric Metric) error { | ||
if !b.Enabled() { | ||
return nil | ||
} | ||
|
||
switch metric.Name { | ||
case CacheDownloadSize, CacheDownloadTime, CacheUser, CacheServer: | ||
return publishMetricToFile(b.CacheMetricsPath, metric.Name, metric.Value) | ||
case CacheTotalRate, CacheCorruptionRate: | ||
return publishMetricToFile(b.ToolboxMetricsPath, metric.Name, metric.Value) | ||
} | ||
|
||
fmt.Printf("Ignoring metric %s\n", metric.Name) | ||
return nil | ||
} | ||
|
||
func publishMetricToFile(file, metricName, metricValue string) error { | ||
f, err := os.OpenFile(file, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
defer f.Close() | ||
|
||
line := fmt.Sprintf("%s %s\n", metricName, metricValue) | ||
|
||
_, err = f.WriteString(line) | ||
return err | ||
} |
Oops, something went wrong.