-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
498 additions
and
42 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
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,78 @@ | ||
package metric | ||
|
||
import ( | ||
"bytes" | ||
"encoding/gob" | ||
"errors" | ||
"fmt" | ||
"sync" | ||
|
||
"github.com/influxdata/telegraf" | ||
) | ||
|
||
// storage for tracking data that can't be serialized to disk | ||
var ( | ||
// todo need some way to empty this map out when done with a tracking ID. | ||
// grouped tracking metrics means that ID->Data association is not one to one, | ||
// many metrics could be associated with one tracking ID so we cannot just | ||
// clear this every time in FromBytes. | ||
trackingStore = make(map[telegraf.TrackingID]telegraf.TrackingData) | ||
mu = sync.Mutex{} | ||
|
||
// ErrSkipTracking indicates that tracking information could not be found after | ||
// deserializing a metric from bytes. In this case we should skip the metric | ||
// and continue as if it does not exist. | ||
ErrSkipTracking = errors.New("metric tracking data not found") | ||
) | ||
|
||
type serializedMetric struct { | ||
M telegraf.Metric | ||
TID telegraf.TrackingID | ||
} | ||
|
||
func ToBytes(m telegraf.Metric) ([]byte, error) { | ||
var sm serializedMetric | ||
if um, ok := m.(telegraf.UnwrappableMetric); ok { | ||
sm.M = um.Unwrap() | ||
} else { | ||
sm.M = m | ||
} | ||
|
||
if tm, ok := m.(telegraf.TrackingMetric); ok { | ||
sm.TID = tm.TrackingID() | ||
|
||
mu.Lock() | ||
trackingStore[sm.TID] = tm.TrackingData() | ||
mu.Unlock() | ||
} | ||
|
||
var buf bytes.Buffer | ||
encoder := gob.NewEncoder(&buf) | ||
if err := encoder.Encode(&sm); err != nil { | ||
return nil, fmt.Errorf("failed to encode metric to bytes: %w", err) | ||
} | ||
return buf.Bytes(), nil | ||
} | ||
|
||
func FromBytes(b []byte) (telegraf.Metric, error) { | ||
buf := bytes.NewBuffer(b) | ||
decoder := gob.NewDecoder(buf) | ||
|
||
var sm *serializedMetric | ||
if err := decoder.Decode(&sm); err != nil { | ||
return nil, fmt.Errorf("failed to decode metric from bytes: %w", err) | ||
} | ||
|
||
m := sm.M | ||
if sm.TID != 0 { | ||
mu.Lock() | ||
td := trackingStore[sm.TID] | ||
mu.Unlock() | ||
|
||
if td == nil { | ||
return nil, ErrSkipTracking | ||
} | ||
m = rebuildTrackingMetric(m, td) | ||
} | ||
return m, nil | ||
} |
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,7 @@ | ||
package metric | ||
|
||
import "encoding/gob" | ||
|
||
func Init() { | ||
gob.RegisterName("metric.metric", &metric{}) | ||
} |
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
Oops, something went wrong.