diff --git a/go.mod b/go.mod index 049bb2a..bc648ea 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,6 @@ toolchain go1.24.0 require ( github.com/dynatrace-ace/dynatrace-go-api-client/api/v2/environment/dynatrace v0.0.0-20210816162345-de2eacc8ac9a github.com/go-logr/logr v1.4.2 - github.com/google/go-cmp v0.6.0 github.com/google/uuid v1.6.0 github.com/hashicorp/golang-lru v0.5.1 github.com/openmcp-project/controller-utils v0.4.2 @@ -44,6 +43,7 @@ require ( github.com/golang/protobuf v1.5.4 // indirect github.com/google/btree v1.1.3 // indirect github.com/google/gnostic-models v0.6.9 // indirect + github.com/google/go-cmp v0.6.0 // indirect github.com/google/gofuzz v1.2.0 // indirect github.com/grpc-ecosystem/grpc-gateway/v2 v2.22.0 // indirect github.com/josharian/intern v1.0.0 // indirect diff --git a/internal/client/client.go b/internal/client/client.go index d23cd0d..1b14a1c 100644 --- a/internal/client/client.go +++ b/internal/client/client.go @@ -8,24 +8,6 @@ import ( dyn "github.com/dynatrace-ace/dynatrace-go-api-client/api/v2/environment/dynatrace" ) -// AbstractDynatraceClient This interface is a wrapper for the dynatrace-go-client, this interface only implements functions regarding metrics. -// This wrapper also includes the ability to add metadata to your metric which uses the SettingsObjectAPI in the background. -type AbstractDynatraceClient interface { - NewClient(url *string, token string) *dyn.APIClient - - // Metrics: Speak directly to MetricsAPI - SendMetric(ctx context.Context, metric MetricMetadata) (*http.Response, error) - GetMetric(ctx context.Context, id string) (dyn.MetricDescriptor, *http.Response, error) - GetAllMetrics(ctx context.Context) (dyn.MetricDescriptorCollection, *http.Response, error) - DeleteMetric(ctx context.Context, id string) (*http.Response, error) - - // Metrics Metadata: Speaks to SettingsObjectAPI - SendMetricMetadata(ctx context.Context, metric MetricMetadata) ([]dyn.SettingsObjectResponse, *http.Response, error) - GetMetricMetadata(ctx context.Context, id string) (dyn.ObjectsList, *http.Response, error) - DeleteMetricMetadata(ctx context.Context, objectID string) (*http.Response, error) - UpdateMetricMetadata(ctx context.Context) (dyn.SettingsObjectResponse, *http.Response, error) -} - // DynatraceClient is a wrapper for the dynatrace-go-client, this interface only implements functions regarding metrics. type DynatraceClient struct { Client *dyn.APIClient @@ -77,61 +59,3 @@ func (d *DynatraceClient) SendMetric(ctx context.Context, metric MetricMetadata) return res, err } - -// GetAllMetrics returns all available metrics that Dynatrace has to over, this is not limited to anything. -func (d *DynatraceClient) GetAllMetrics(ctx context.Context) (dyn.MetricDescriptorCollection, *http.Response, error) { - coll, res, err := d.Client.MetricsApi.AllMetrics(ctx).Execute() - return coll, res, err -} - -// GetMetric get a specific metric -// id: id of the metric you want (not the display name) -func (d *DynatraceClient) GetMetric(ctx context.Context, id string) (dyn.MetricDescriptor, *http.Response, error) { - des, res, err := d.Client.MetricsApi.Metric(ctx, id).Execute() - return des, res, err -} - -// DeleteMetric deletes a metric -// id: id of the metric you want (not the display name) -func (d *DynatraceClient) DeleteMetric(ctx context.Context, id string) (*http.Response, error) { - res, err := d.Client.MetricsApi.Delete(ctx, id).Execute() - return res, err -} - -// SendMetricMetadata sends the metadata of the metric -// this is a big request, this could cause overhead if send with every single datapoint -func (d *DynatraceClient) SendMetricMetadata(ctx context.Context, metric MetricMetadata) ([]dyn.SettingsObjectResponse, *http.Response, error) { - settings, err := metric.GenerateSettingsObjects() - if err != nil { - return []dyn.SettingsObjectResponse{}, &http.Response{}, err - } - collPost, resPost, errPost := d.Client.SettingsObjectsApi.PostSettingsObjects(ctx).SettingsObjectCreate(settings).Execute() - return collPost, resPost, errPost -} - -// GetMetricMetadata gets the metadata of a specific metric -// id: id of the metric you want (not the display name) -func (d *DynatraceClient) GetMetricMetadata(ctx context.Context, id string) (dyn.ObjectsList, *http.Response, error) { - coll, res, err := d.Client.SettingsObjectsApi.GetSettingsObjects(ctx).SchemaIds("builtin:metric.metadata").Scopes("metric-" + id).Execute() - return coll, res, err -} - -// DeleteMetricMetadata deletes the metadata of a specific metric -// objectId: this id can be optained by making a get request (is not the normal id of an metric) -func (d *DynatraceClient) DeleteMetricMetadata(ctx context.Context, objectID string) (*http.Response, error) { - resDel, errDel := d.Client.SettingsObjectsApi.DeleteSettingsObjectByObjectId(ctx, objectID).Execute() - return resDel, errDel -} - -// UpdateMetricMetadata updates the metadata of a specific metric -// objectId: this id can be optained by making a get request (is not the normal id of an metric) -// metric: the updated complete metricMetadata object -// updateToken: can again be obtained by making a get request for the metric you want to update -func (d *DynatraceClient) UpdateMetricMetadata(ctx context.Context, objectID string, metric MetricMetadata, updateToken string) (dyn.SettingsObjectResponse, *http.Response, error) { - settings, err := metric.GenerateUpdateSettings(objectID, metric, updateToken) - if err != nil { - return dyn.SettingsObjectResponse{}, &http.Response{}, err - } - colUpd, resUpd, errUpd := d.Client.SettingsObjectsApi.PutSettingsObjectByObjectId(ctx, objectID).SettingsObjectUpdate(settings).Execute() - return colUpd, resUpd, errUpd -} diff --git a/internal/client/client_test.go b/internal/client/client_test.go deleted file mode 100644 index 02df83b..0000000 --- a/internal/client/client_test.go +++ /dev/null @@ -1,477 +0,0 @@ -package client - -import ( - "testing" - "time" - - "github.com/google/go-cmp/cmp" -) - -func Setup() MetricMetadata { - return NewMetricMetadata("id", "name", "description") -} - -func TestMetricAddDimension(t *testing.T) { - tests := map[string]struct { - input map[string]string - want map[string]string - }{ - "one value": {input: map[string]string{"Test": "0"}, want: map[string]string{"Test": "0"}}, - "two value": {input: map[string]string{"key-one": "0", "key-two": "1"}, want: map[string]string{"key-one": "0", "key-two": "1"}}, - "zero value": {input: map[string]string{}, want: map[string]string{}}, - } - - for name, tc := range tests { - t.Run(name, func(t *testing.T) { - metric := Setup() - - // add dimensions - for key, value := range tc.input { - err := metric.AddDimension(key, value) - if err != nil { - t.Fatalf("an error when adding a dimension: %s", err) - } - } - - diff := cmp.Diff(tc.want, metric.Metric.dimensions) - if diff != "" { - t.Fatalf("%s", diff) - } - }) - } -} - -func TestMetricAddDimensions(t *testing.T) { - tests := map[string]struct { - input map[string]string - want map[string]string - }{ - "one value": {input: map[string]string{"Test": "0"}, want: map[string]string{"Test": "0"}}, - "two value": {input: map[string]string{"key-one": "0", "key-two": "1"}, want: map[string]string{"key-one": "0", "key-two": "1"}}, - "zero value": {input: map[string]string{}, want: map[string]string{}}, - } - - for name, tc := range tests { - t.Run(name, func(t *testing.T) { - metric := Setup() - - // add dimensions - err := metric.AddDimensions(tc.input) - if err != nil { - t.Fatalf("an error when adding a dimension: %s", err) - } - - diff := cmp.Diff(tc.want, metric.Metric.dimensions) - if diff != "" { - t.Fatalf("%s", diff) - } - }) - } -} - -func TestMetricClearDimensions(t *testing.T) { - tests := map[string]struct { - input map[string]string - want map[string]string - }{ - "one value": {input: map[string]string{"Test": "0"}, want: map[string]string{}}, - "two value": {input: map[string]string{"key-one": "0", "key-two": "1"}, want: map[string]string{}}, - "zero value": {input: map[string]string{}, want: map[string]string{}}, - } - - for name, tc := range tests { - t.Run(name, func(t *testing.T) { - metric := Setup() - - // add dimensions - for key, value := range tc.input { - err := metric.AddDimension(key, value) - if err != nil { - t.Fatalf("an error when adding a dimension: %s", err) - } - } - - metric.ClearDimensions() - diff := cmp.Diff(tc.want, metric.Metric.dimensions) - if diff != "" { - t.Fatalf("%s", diff) - } - }) - } -} - -func TestMetricAddDatapoint(t *testing.T) { - tests := map[string]struct { - input []float64 - want []float64 - }{ - "one value": {input: []float64{0}, want: []float64{0}}, - "two value": {input: []float64{2.42, 6.69}, want: []float64{2.42, 6.69}}, - "zero value": {input: []float64{}, want: []float64{}}, - } - - for name, tc := range tests { - t.Run(name, func(t *testing.T) { - metric := Setup() - - // add datapoint - for _, value := range tc.input { - metric.AddDatapoint(value) - } - - diff := cmp.Diff(tc.want, metric.Metric.datapoints) - if diff != "" { - t.Fatalf("%s", diff) - } - }) - } -} - -func TestMetricAddDatapoints(t *testing.T) { - tests := map[string]struct { - input []float64 - want []float64 - }{ - "one value": {input: []float64{0}, want: []float64{0}}, - "two value": {input: []float64{2.42, 6.69}, want: []float64{2.42, 6.69}}, - "zero value": {input: []float64{}, want: []float64{}}, - } - - for name, tc := range tests { - t.Run(name, func(t *testing.T) { - metric := Setup() - - // add datapoints - metric.AddDatapoints(tc.input...) - - diff := cmp.Diff(tc.want, metric.Metric.datapoints) - if diff != "" { - t.Fatalf("%s", diff) - } - }) - } -} - -func TestMetricClearDatapoints(t *testing.T) { - tests := map[string]struct { - input []float64 - want []float64 - }{ - "one value": {input: []float64{0}, want: []float64{}}, - "two value": {input: []float64{2.42, 6.69}, want: []float64{}}, - "zero value": {input: []float64{}, want: []float64{}}, - } - - for name, tc := range tests { - t.Run(name, func(t *testing.T) { - metric := Setup() - - // add datapoints - metric.AddDatapoints(tc.input...) - - metric.ClearDatapoints() - diff := cmp.Diff(tc.want, metric.Metric.datapoints) - if diff != "" { - t.Fatalf("%s", diff) - } - }) - } -} - -func TestMetricSetTimestamp(t *testing.T) { - tests := map[string]struct { - input int64 - want int64 - }{ - "now": {input: time.Now().UnixMilli(), want: time.Now().UnixMilli()}, - "zero": {input: 0, want: 0}, - } - - for name, tc := range tests { - t.Run(name, func(t *testing.T) { - metric := Setup() - - // set type - err := metric.SetTimestamp(tc.input) - if err != nil { - t.Fatalf("timestamp was not set on the object: %s", err) - } - - diff := cmp.Diff(tc.want, metric.Metric.timestamp) - if diff != "" { - t.Fatalf("%s", diff) - } - }) - } -} - -func TestMetricSetTypeCount(t *testing.T) { - tests := map[string]struct { - input []float64 - want MetricType - }{ - "one": {input: []float64{2.42}, want: COUNT}, - "three": {input: []float64{2.42, 42.0, 6.90}, want: GAUGE}, - "zero": {input: []float64{}, want: GAUGE}, - } - - for name, tc := range tests { - t.Run(name, func(t *testing.T) { - metric := Setup() - - // set type - metric.AddDatapoints(tc.input...) - _ = metric.SetTypeCount(10) - - diff := cmp.Diff(tc.want, metric.Metric.valueType) - if diff != "" { - t.Fatalf("%s", diff) - } - }) - } -} - -func TestMetricSetUnit(t *testing.T) { - tests := map[string]struct { - input string - want string - }{ - "one": {input: "Percent", want: "Percent"}, - "three": {input: "kg", want: "kg"}, - "zero": {input: "", want: ""}, - } - - for name, tc := range tests { - t.Run(name, func(t *testing.T) { - metric := Setup() - - // set unit - metric.SetUnit(tc.input) - - diff := cmp.Diff(tc.want, metric.value.Unit) - if diff != "" { - t.Fatalf("%s", diff) - } - }) - } -} - -func TestMetricAddTags(t *testing.T) { - tests := map[string]struct { - input []string - want []string - }{ - "three": {input: []string{"super", "cool", "tags"}, want: []string{"super", "cool", "tags"}}, - "one": {input: []string{"tags"}, want: []string{"tags"}}, - "zero": {input: []string{}, want: []string{}}, - } - - for name, tc := range tests { - t.Run(name, func(t *testing.T) { - metric := Setup() - - // set tags - metric.AddTags(tc.input) - - diff := cmp.Diff(tc.want, metric.value.Tags) - if diff != "" { - t.Fatalf("%s", diff) - } - }) - } -} - -func TestMetricAddTag(t *testing.T) { - tests := map[string]struct { - input []string - want []string - }{ - "three": {input: []string{"super", "cool", "tags"}, want: []string{"super", "cool", "tags"}}, - "one": {input: []string{"tags"}, want: []string{"tags"}}, - "zero": {input: []string{}, want: []string{}}, - } - - for name, tc := range tests { - t.Run(name, func(t *testing.T) { - metric := Setup() - - // set tags - for _, tag := range tc.input { - metric.AddTag(tag) - } - - diff := cmp.Diff(tc.want, metric.value.Tags) - if diff != "" { - t.Fatalf("%s", diff) - } - }) - } -} - -func TestMetricSetMaxValue(t *testing.T) { - tests := map[string]struct { - input float64 - want float64 - }{ - "one": {input: 64.42, want: 64.42}, - "zero": {input: 0.0, want: 0.0}, - } - - for name, tc := range tests { - t.Run(name, func(t *testing.T) { - metric := Setup() - - // set tags - - metric.SetMaxValue(tc.input) - - diff := cmp.Diff(tc.want, metric.value.MetricProperties.MaxValue) - if diff != "" { - t.Fatalf("%s", diff) - } - }) - } -} - -func TestMetricSetMinValue(t *testing.T) { - tests := map[string]struct { - input float64 - want float64 - }{ - "one": {input: 64.42, want: 64.42}, - "zero": {input: 0.0, want: 0.0}, - } - - for name, tc := range tests { - t.Run(name, func(t *testing.T) { - metric := Setup() - - // set tags - - metric.SetMinValue(tc.input) - - diff := cmp.Diff(tc.want, metric.value.MetricProperties.MinValue) - if diff != "" { - t.Fatalf("%s", diff) - } - }) - } -} - -func TestMetricSetRootCauseRelevant(t *testing.T) { - tests := map[string]struct { - input bool - want bool - }{ - "true": {input: true, want: true}, - "false": {input: false, want: false}, - } - - for name, tc := range tests { - t.Run(name, func(t *testing.T) { - metric := Setup() - - metric.SetRootCauseRelevant(tc.input) - - diff := cmp.Diff(tc.want, metric.value.MetricProperties.RootCauseRelevant) - if diff != "" { - t.Fatalf("%s", diff) - } - }) - } -} - -func TestMetricSetImpactRelevant(t *testing.T) { - tests := map[string]struct { - input bool - want bool - }{ - "true": {input: true, want: true}, - "false": {input: false, want: false}, - } - - for name, tc := range tests { - t.Run(name, func(t *testing.T) { - metric := Setup() - - metric.SetImpactRelevant(tc.input) - - diff := cmp.Diff(tc.want, metric.value.MetricProperties.ImpactRelevant) - if diff != "" { - t.Fatalf("%s", diff) - } - }) - } -} - -func TestMetricSetValueType(t *testing.T) { - tests := map[string]struct { - input ValueType - want ValueType - }{ - "score": {input: SCORE, want: SCORE}, - "error": {input: ERROR, want: ERROR}, - } - - for name, tc := range tests { - t.Run(name, func(t *testing.T) { - metric := Setup() - - metric.SetValueType(tc.input) - - diff := cmp.Diff(tc.want, metric.value.MetricProperties.ValueType) - if diff != "" { - t.Fatalf("%s", diff) - } - }) - } -} - -func TestMetricSetLatency(t *testing.T) { - tests := map[string]struct { - input int - want int - }{ - "positive": {input: 20, want: 20}, - "zero": {input: 0, want: 0}, - "negative": {input: -13, want: 0}, - } - - for name, tc := range tests { - t.Run(name, func(t *testing.T) { - metric := Setup() - - metric.SetLatency(tc.input) - - diff := cmp.Diff(tc.want, metric.value.MetricProperties.Latency) - if diff != "" { - t.Fatalf("%s", diff) - } - }) - } -} -func TestMetricAddMetadataDimension(t *testing.T) { - tests := map[string]struct { - input []Dimension - want []Dimension - }{ - "one": {input: []Dimension{{Key: "dimension", DisplayName: "display name"}}, want: []Dimension{{Key: "dimension", DisplayName: "display name"}}}, - "zero": {input: []Dimension{}, want: []Dimension{}}, - "multiple": {input: []Dimension{{Key: "dimension", DisplayName: "display name"}, {Key: "dimension-two", DisplayName: "display name two"}}, want: []Dimension{{Key: "dimension", DisplayName: "display name"}, {Key: "dimension-two", DisplayName: "display name two"}}}, - } - - for name, tc := range tests { - t.Run(name, func(t *testing.T) { - metric := Setup() - - for _, dimensions := range tc.input { - metric.AddMetadataDimension(dimensions.Key, dimensions.DisplayName) - } - - diff := cmp.Diff(tc.want, metric.value.Dimensions) - if diff != "" { - t.Fatalf("%s", diff) - } - }) - } -} diff --git a/internal/client/metrics.go b/internal/client/metrics.go index 267ff64..7be75b8 100644 --- a/internal/client/metrics.go +++ b/internal/client/metrics.go @@ -1,13 +1,10 @@ package client import ( - "encoding/json" - "errors" "fmt" "slices" "strconv" "strings" - "time" dyn "github.com/dynatrace-ace/dynatrace-go-api-client/api/v2/environment/dynatrace" ) @@ -38,7 +35,6 @@ type Metric struct { id *string dimensions map[string]string datapoints []float64 - timestamp int64 // UTC Milliseconds Timestamp valueType MetricType Min float64 max float64 @@ -142,11 +138,6 @@ func (m *MetricMetadata) AddDimensions(dimension map[string]string) error { return nil } -// RemoveDimension remove dimension by key -func (m *MetricMetadata) RemoveDimension(key string) { - delete(m.Metric.dimensions, key) -} - // ClearDimensions empty all dimensions and replaces them with an empty map func (m *MetricMetadata) ClearDimensions() { m.Metric.dimensions = map[string]string{} @@ -173,174 +164,6 @@ func (m *MetricMetadata) AddDatapoint(point float64) { } } -// AddDatapoints Add multiple datapoints that will be the value displayed on the graph in dynatrace -// This function only appends to a list of datapoints. -// The payload if multiple datapoints exist consist of multiple statistics calculated from the list of points. -// Payload consists of: minimum, maximum, count and sum -// -// Type regarding the number of Datapoints: -// When Adding more than one datapoint the Type of the Metric will automatically be set to GAUGE -// -// If there is only one datapoint the type will still be GAUGE, but can be manually set to COUNT via "SetValueType()" -func (m *MetricMetadata) AddDatapoints(points ...float64) { - if len(points) == 0 { - return - } - m.Metric.datapoints = append(m.Metric.datapoints, points...) - m.Metric.Min = slices.Min(m.Metric.datapoints) - m.Metric.max = slices.Max(m.Metric.datapoints) - m.Metric.count = uint64(len(m.Metric.datapoints)) - m.Metric.valueType = GAUGE - for _, value := range m.Metric.datapoints { - m.Metric.sum += value - } -} - -// ClearDatapoints replaces list of datapoints with an empty array -func (m *MetricMetadata) ClearDatapoints() { - m.Metric.datapoints = []float64{} -} - -// SetTimestamp Set the timestamp of the metric manually -// if not set the timestamp when the metric is received will be used -// -// Rules: -// -// - Up to 10 minutes in the future -// -// - Up to 1 hour in the past -func (m *MetricMetadata) SetTimestamp(timestamp int64) error { - pastLimit := int64(time.Now().Add(time.Hour*-1).UTC().Nanosecond()) / int64(time.Millisecond) - futureLimit := int64(time.Now().Add(time.Minute*10).UTC().Nanosecond()) / int64(time.Millisecond) - if timestamp > pastLimit || timestamp < futureLimit { - m.Metric.timestamp = timestamp - return nil - } - return errors.New("timestamp can only be 10 minutes into the future or 1 hour into the past") -} - -// SetTypeCount Set type to COUNT -// -// Type count can only be set if one datapoint exists -// the reason being, that count will add a delta to the current datapoint and records this in dynatrace -func (m *MetricMetadata) SetTypeCount(delta float64) error { - if len(m.Metric.datapoints) == 1 { - m.Metric.valueType = COUNT - m.Metric.delta = delta - return nil - } - return fmt.Errorf("using type count is only possible with one datapoint: current count %v", len(m.Metric.datapoints)) -} - -// SetUnit Set the unit of measure for your metric -// -// This is also only send via metric metadata -func (m *MetricMetadata) SetUnit(unit string) { - m.value.Unit = unit -} - -// AddTags Add tags for your metric -// -// # This is also only send via metric metadata -// -// This can be used to filter for your metric in the dynatrace UI -func (m *MetricMetadata) AddTags(tags []string) { - m.value.Tags = append(m.value.Tags, tags...) - -} - -// AddTag Add a tag to your metric -// -// # This is also only send via metric metadata -// -// This can be used to filter for your metric in the dynatrace UI -func (m *MetricMetadata) AddTag(tag string) { - m.value.Tags = append(m.value.Tags, tag) - -} - -// SetMaxValue Add a maximum value to your metric -// -// # This is also only send via metric metadata -// -// This can be used to limit the value (useful if a metric is used that sums up over a long period of time) -func (m *MetricMetadata) SetMaxValue(value float64) { - m.value.MetricProperties.MaxValue = value - -} - -// SetMinValue Add a minimum value to your metric -// -// # This is also only send via metric metadata -// -// This can be used to limit the value (useful if a metric is used that sums up over a long period of time) -func (m *MetricMetadata) SetMinValue(value float64) { - m.value.MetricProperties.MinValue = value - -} - -// SetRootCauseRelevant Add a rootcause relevant flag to your metric -// -// # This is also only send via metric metadata -// -// This can be used to filter for your metric -func (m *MetricMetadata) SetRootCauseRelevant(value bool) { - m.value.MetricProperties.RootCauseRelevant = value - -} - -// SetImpactRelevant Add a impact relevant flag to your metric -// -// # This is also only send via metric metadata -// -// This can be used to filter for your metric -func (m *MetricMetadata) SetImpactRelevant(value bool) { - m.value.MetricProperties.ImpactRelevant = value - -} - -// SetValueType Set the Value type of your metric -// -// # This is also only send via metric metadata -// -// This can be used to filter for your metric -func (m *MetricMetadata) SetValueType(value ValueType) { - m.value.MetricProperties.ValueType = value - -} - -// SetLatency Set the latency of your metric -// -// This is also only send via metric metadata -func (m *MetricMetadata) SetLatency(latency int) { - if latency > 0 { - m.value.MetricProperties.Latency = latency - } - -} - -// AddMetadataDimension Add a dimension to your metric -// -// # This is also only send via metric metadata -// -// This can be used to predefine the available metrics without sending a datapoint -func (m *MetricMetadata) AddMetadataDimension(key string, displayName string) { - if key != "" && displayName != "" { - m.value.Dimensions = append(m.value.Dimensions, Dimension{Key: key, DisplayName: displayName}) - } - -} - -// AddMetadataDimensions Add dimensions to your metric -// -// # This is also only send via metric metadata -// -// This can be used to predefine the available metrics without sending a datapoint -func (m *MetricMetadata) AddMetadataDimensions(dimensions []Dimension) { - m.value.Dimensions = append(m.value.Dimensions, dimensions...) - -} - // GenerateMetricBody Generate the payload body that can be used to send a datapoint with dimensions to the backend // // This will generate a body to send a datapoint to the backend (please use the client-api if you want to send something) @@ -366,49 +189,3 @@ func (m *MetricMetadata) GenerateMetricBody() string { return body } - -// GenerateSettingsObjects Generate the payload body that can be used to send metric metadata to the backend -// -// This will generate a SettingsObject to send metadata for a sepcific metric to the backend (PLEASE use the client-api if you want to send something) -// This can be used to check if the body is correctly generated -func (m *MetricMetadata) GenerateSettingsObjects() ([]dyn.SettingsObjectCreate, error) { - val := m.value - js, err := json.Marshal(val) - if err != nil { - return []dyn.SettingsObjectCreate{}, err - } - var mapped map[string]interface{} - err = json.Unmarshal(js, &mapped) - if err != nil { - return []dyn.SettingsObjectCreate{}, err - } - m.Setting.Value = mapped - - return []dyn.SettingsObjectCreate{m.Setting}, nil -} - -// GenerateUpdateSettings Generate the payload body that can be used to send metric metadata to the backend -// -// This will generate a SettingsObjectUpdate to send updated metadata for a sepcific metric to the backend (PLEASE use the client-api if you want to send something) -// This can be used to check if the body is correctly generated -func (m *MetricMetadata) GenerateUpdateSettings(_ string, metric MetricMetadata, updateToken string) (dyn.SettingsObjectUpdate, error) { - val := metric.value - js, err := json.Marshal(val) - if err != nil { - return dyn.SettingsObjectUpdate{}, err - } - var mapped map[string]interface{} - err = json.Unmarshal(js, &mapped) - if err != nil { - return dyn.SettingsObjectUpdate{}, err - } - var setting dyn.SettingsObjectUpdate - - if updateToken != "" { - setting = dyn.SettingsObjectUpdate{UpdateToken: &updateToken, Value: mapped} - return setting, nil - } - setting = dyn.SettingsObjectUpdate{Value: mapped} - return setting, nil - -}