Skip to content

Commit

Permalink
Add prom type as metric attribute (#2689)
Browse files Browse the repository at this point in the history
  • Loading branch information
bjrara authored and pmatyjasek-sumo committed Apr 28, 2021
1 parent f5d3d5b commit c7d97bf
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 4 deletions.
44 changes: 40 additions & 4 deletions exporter/awsemfexporter/metric_translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,23 @@ const (
// DimensionRollupOptions
zeroAndSingleDimensionRollup = "ZeroAndSingleDimensionRollup"
singleDimensionRollupOnly = "SingleDimensionRollupOnly"

prometheusReceiver = "prometheus"
attributeReceiver = "receiver"
fieldPrometheusMetricType = "prom_metric_type"
)

var fieldPrometheusTypes = map[pdata.MetricDataType]string{
pdata.MetricDataTypeNone: "",
pdata.MetricDataTypeIntGauge: "gauge",
pdata.MetricDataTypeDoubleGauge: "gauge",
pdata.MetricDataTypeIntSum: "counter",
pdata.MetricDataTypeDoubleSum: "counter",
pdata.MetricDataTypeIntHistogram: "histogram",
pdata.MetricDataTypeDoubleHistogram: "histogram",
pdata.MetricDataTypeDoubleSummary: "summary",
}

// CWMetrics defines
type CWMetrics struct {
Measurements []CWMeasurement
Expand Down Expand Up @@ -63,6 +78,9 @@ type CWMetricMetadata struct {
LogGroup string
LogStream string
InstrumentationLibraryName string

receiver string
metricDataType pdata.MetricDataType
}

type metricTranslator struct {
Expand All @@ -87,6 +105,10 @@ func (mt metricTranslator) translateOTelToGroupedMetric(rm *pdata.ResourceMetric
logGroup, logStream := getLogInfo(rm, cWNamespace, config)

ilms := rm.InstrumentationLibraryMetrics()
var metricReceiver string
if receiver, ok := rm.Resource().Attributes().Get(attributeReceiver); ok {
metricReceiver = receiver.StringVal()
}
for j := 0; j < ilms.Len(); j++ {
ilm := ilms.At(j)
if ilm.InstrumentationLibrary().Name() == "" {
Expand All @@ -104,6 +126,8 @@ func (mt metricTranslator) translateOTelToGroupedMetric(rm *pdata.ResourceMetric
LogGroup: logGroup,
LogStream: logStream,
InstrumentationLibraryName: instrumentationLibName,
receiver: metricReceiver,
metricDataType: metric.DataType(),
}
addToGroupedMetric(&metric, groupedMetrics, metadata, config.logger, mt.metricDescriptor)
}
Expand All @@ -113,17 +137,25 @@ func (mt metricTranslator) translateOTelToGroupedMetric(rm *pdata.ResourceMetric
// translateGroupedMetricToCWMetric converts Grouped Metric format to CloudWatch Metric format.
func translateGroupedMetricToCWMetric(groupedMetric *GroupedMetric, config *Config) *CWMetrics {
labels := groupedMetric.Labels
fields := make(map[string]interface{}, len(labels)+len(groupedMetric.Metrics))
fieldsLength := len(labels) + len(groupedMetric.Metrics)

isPrometheusMetric := groupedMetric.Metadata.receiver == prometheusReceiver
if isPrometheusMetric {
fieldsLength++
}
fields := make(map[string]interface{}, fieldsLength)

// Add labels to fields
for k, v := range labels {
fields[k] = v
}

// Add metrics to fields
for metricName, metricInfo := range groupedMetric.Metrics {
fields[metricName] = metricInfo.Value
}
if isPrometheusMetric {
fields[fieldPrometheusMetricType] = fieldPrometheusTypes[groupedMetric.Metadata.metricDataType]
}

var cWMeasurements []CWMeasurement
if len(config.MetricDeclarations) == 0 {
Expand Down Expand Up @@ -181,7 +213,9 @@ func groupedMetricToCWMeasurement(groupedMetric *GroupedMetric, config *Config)
for metricName, metricInfo := range groupedMetric.Metrics {
metrics[idx] = map[string]string{
"Name": metricName,
"Unit": metricInfo.Unit,
}
if metricInfo.Unit != "" {
metrics[idx]["Unit"] = metricInfo.Unit
}
idx++
}
Expand Down Expand Up @@ -247,7 +281,9 @@ func groupedMetricToCWMeasurementsWithFilters(groupedMetric *GroupedMetric, conf

metric := map[string]string{
"Name": metricName,
"Unit": metricInfo.Unit,
}
if metricInfo.Unit != "" {
metric["Unit"] = metricInfo.Unit
}
metricDeclKey := fmt.Sprint(metricDeclIdx)
if group, ok := metricDeclGroups[metricDeclKey]; ok {
Expand Down
42 changes: 42 additions & 0 deletions exporter/awsemfexporter/metric_translator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func createMetricTestData() internaldata.MetricsData {
Labels: map[string]string{
conventions.AttributeServiceName: "myServiceName",
conventions.AttributeServiceNamespace: "myServiceNS",
attributeReceiver: prometheusReceiver,
},
},
Metrics: []*metricspb.Metric{
Expand Down Expand Up @@ -795,6 +796,47 @@ func TestTranslateGroupedMetricToCWMetric(t *testing.T) {
},
},
},
{
"prometheus metrics",
&GroupedMetric{
Labels: map[string]string{
"label1": "value1",
},
Metrics: map[string]*MetricInfo{
"metric1": {
Value: 1,
Unit: "Count",
},
},
Metadata: CWMetricMetadata{
Namespace: namespace,
TimestampMs: timestamp,
receiver: prometheusReceiver,
metricDataType: pdata.MetricDataTypeDoubleGauge,
},
},
nil,
&CWMetrics{
Measurements: []CWMeasurement{
{
Namespace: namespace,
Dimensions: [][]string{{"label1"}},
Metrics: []map[string]string{
{
"Name": "metric1",
"Unit": "Count",
},
},
},
},
TimestampMs: timestamp,
Fields: map[string]interface{}{
"label1": "value1",
"metric1": 1,
fieldPrometheusMetricType: "gauge",
},
},
},
}

logger := zap.NewNop()
Expand Down

0 comments on commit c7d97bf

Please sign in to comment.