-
Notifications
You must be signed in to change notification settings - Fork 48
/
metadata.go
77 lines (66 loc) · 2.02 KB
/
metadata.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package messages
import (
"encoding/json"
"fmt"
"strings"
"github.com/signalfx/signalfx-go/idtool"
)
type MetadataMessage struct {
BaseJSONChannelMessage
TSID idtool.ID `json:"tsId"`
Properties MetadataProperties `json:"properties"`
}
type MetadataProperties struct {
Metric string `json:"sf_metric"`
OriginatingMetric string `json:"sf_originatingMetric"`
ResolutionMS int `json:"sf_resolutionMs"`
CreatedOnMS int `json:"sf_createdOnMs"`
// Additional SignalFx-generated properties about this time series. Many
// of these are exposed directly in fields on this struct.
InternalProperties map[string]interface{} `json:"-"`
// Custom properties applied to the timeseries through various means,
// including dimensions, properties on matching dimensions, etc.
CustomProperties map[string]string `json:"-"`
}
func (mp *MetadataProperties) UnmarshalJSON(b []byte) error {
// Deserialize it at first to get all the well-known fields put in place so
// we don't have to manually assign them below.
type Alias MetadataProperties
if err := json.Unmarshal(b, (*Alias)(mp)); err != nil {
return err
}
// Deserialize it again to a generic map so we can get at all the fields.
var propMap map[string]interface{}
if err := json.Unmarshal(b, &propMap); err != nil {
return err
}
mp.InternalProperties = make(map[string]interface{})
mp.CustomProperties = make(map[string]string)
for k, v := range propMap {
if strings.HasPrefix(k, "sf_") {
mp.InternalProperties[k] = v
} else {
mp.CustomProperties[k] = fmt.Sprintf("%v", v)
}
}
return nil
}
func (mp *MetadataProperties) MarshalJSON() ([]byte, error) {
type Alias MetadataProperties
intermediate, err := json.Marshal((*Alias)(mp))
if err != nil {
return nil, err
}
out := map[string]interface{}{}
err = json.Unmarshal(intermediate, &out)
if err != nil {
return nil, err
}
for k, v := range mp.InternalProperties {
out[k] = v
}
for k, v := range mp.CustomProperties {
out[k] = v
}
return json.Marshal(out)
}