github.com/mweagle/Sparta@v1.15.0/aws/cloudwatch/structured_metric_test.go (about)

     1  package cloudwatch
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/xeipuuv/gojsonschema"
    10  )
    11  
    12  func ensureValidMetric(t *testing.T, emMetric *EmbeddedMetric) {
    13  	sink := &bytes.Buffer{}
    14  	emMetric.PublishToSink(nil, sink)
    15  	// Verify...
    16  	schemaLoader := gojsonschema.NewReferenceLoader("file://./emf.schema.json")
    17  	documentLoader := gojsonschema.NewBytesLoader(sink.Bytes())
    18  
    19  	_, err := gojsonschema.Validate(schemaLoader, documentLoader)
    20  	if err != nil {
    21  		t.Fatalf("Failed to produce valid structured metric: %v", err)
    22  	}
    23  }
    24  
    25  func TestStructuredMetric(t *testing.T) {
    26  	// Initialize with high cardinality property
    27  	emMetric, _ := NewEmbeddedMetricWithProperties(map[string]interface{}{
    28  		"testMetric": "42",
    29  	})
    30  	// Add a directive with a namespace that defines a metric. Metric values
    31  	// may also be array
    32  	metricDirective := emMetric.NewMetricDirective("SpecialNamespace",
    33  		map[string]string{"functionVersion": "23"})
    34  
    35  	metricDirective.Metrics["invocations"] = MetricValue{
    36  		Unit:  UnitCount,
    37  		Value: 1,
    38  	}
    39  	emMetric.Publish(map[string]interface{}{
    40  		"additional": fmt.Sprintf("high cardinality prop: %d", time.Now().Unix()),
    41  	})
    42  	ensureValidMetric(t, emMetric)
    43  }