forked from flant/shell-operator
-
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.
Merge pull request flant#165 from flant/feat_metrics_from_hooks
feat: allow to send metrics from hooks
- Loading branch information
Showing
6 changed files
with
147 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package metrics_storage | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
) | ||
|
||
type MetricOperation struct { | ||
Name string `json:"name"` | ||
Add *float64 `json:"add,omitempty"` | ||
Set *float64 `json:"set,omitempty"` | ||
Labels map[string]string `json:"labels"` | ||
} | ||
|
||
func MetricOperationsFromReader(r io.Reader) ([]MetricOperation, error) { | ||
var operations = make([]MetricOperation, 0) | ||
|
||
dec := json.NewDecoder(r) | ||
for { | ||
var metricOperation MetricOperation | ||
if err := dec.Decode(&metricOperation); err == io.EOF { | ||
break | ||
} else if err != nil { | ||
return nil, err | ||
} | ||
|
||
operations = append(operations, metricOperation) | ||
} | ||
|
||
return operations, nil | ||
} | ||
|
||
func MetricOperationsFromBytes(data []byte) ([]MetricOperation, error) { | ||
return MetricOperationsFromReader(bytes.NewReader(data)) | ||
} | ||
|
||
func MetricOperationsFromFile(filePath string) ([]MetricOperation, error) { | ||
data, err := ioutil.ReadFile(filePath) | ||
if err != nil { | ||
return nil, fmt.Errorf("cannot read %s: %s", filePath, err) | ||
} | ||
|
||
if len(data) == 0 { | ||
return nil, nil | ||
} | ||
return MetricOperationsFromBytes(data) | ||
} |
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