Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable resource attributes to metric label conversion option #1552

Merged
merged 1 commit into from
Nov 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions exporter/awsemfexporter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,29 @@ The following exporter configuration parameters are supported.
| `region` | Send Structured Logs to AWS CloudWatch in a specific region. If this field is not present in config, environment variable "AWS_REGION" can then be used to set region.| determined by metadata |
| `role_arn` | IAM role to upload segments to a different account. | |
| `max_retries` | Maximum number of retries before abandoning an attempt to post data. | 1 |
| `dimension_rollup_option`| DimensionRollupOption is the option for metrics dimension rollup. Three options are available. |"ZeroAndSingleDimensionRollup" (Enable both zero dimension rollup and single dimension rollup)|

| `dimension_rollup_option`| DimensionRollupOption is the option for metrics dimension rollup. Three options are available. |"ZeroAndSingleDimensionRollup" (Enable both zero dimension rollup and single dimension rollup)|
| `resource_to_telemetry_conversion` | "resource_to_telemetry_conversion" is the option for converting resource attributes to telemetry attributes. It has only one config onption- `enabled`. For metrics, if `enabled=true`, all the resource attributes will be converted to metric labels by default. See `Resource Attributes to Metric Labels` section below for examples. | `enabled=false` |


## AWS Credential Configuration

This exporter follows default credential resolution for the
[aws-sdk-go](https://docs.aws.amazon.com/sdk-for-go/api/index.html).

Follow the [guidelines](https://docs.aws.amazon.com/sdk-for-go/v1/developer-guide/configuring-sdk.html) for the
credential configuration.
credential configuration.


## Configuration Examples


### Resource Attributes to Metric Labels
`resource_to_telemetry_conversion` option can be enabled to convert all the resource attributes to metric labels. By default, this option is disabled. Users need to set `enabled=true` to opt-in. See the config example below.

```yaml
exporters:
awsemf:
region: 'us-west-2'
resource_to_telemetry_conversion:
enabled: true
```
6 changes: 6 additions & 0 deletions exporter/awsemfexporter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package awsemfexporter

import (
"go.opentelemetry.io/collector/config/configmodels"
"go.opentelemetry.io/collector/exporter/exporterhelper"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -55,6 +56,11 @@ type Config struct {
// "NoDimensionRollup" - No dimension rollup (only keep original metrics which contain all dimensions)
DimensionRollupOption string `mapstructure:"dimension_rollup_option"`

// ResourceToTelemetrySettings is the option for converting resource attrihutes to telemetry attributes.
// "Enabled" - A boolean field to enable/disable this option. Default is `false`.
// If enabled, all the resource attributes will be converted to metric labels by default.
exporterhelper.ResourceToTelemetrySettings `mapstructure:"resource_to_telemetry_conversion"`

// logger is the Logger used for writing error/warning logs
logger *zap.Logger
}
20 changes: 19 additions & 1 deletion exporter/awsemfexporter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/config/configmodels"
"go.opentelemetry.io/collector/config/configtest"
"go.opentelemetry.io/collector/exporter/exporterhelper"
)

func TestLoadConfig(t *testing.T) {
Expand All @@ -38,7 +39,7 @@ func TestLoadConfig(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, cfg)

assert.Equal(t, len(cfg.Exporters), 2)
assert.Equal(t, 3, len(cfg.Exporters))

r0 := cfg.Exporters["awsemf"]
assert.Equal(t, r0, factory.CreateDefaultConfig())
Expand All @@ -58,4 +59,21 @@ func TestLoadConfig(t *testing.T) {
RoleARN: "arn:aws:iam::123456789:role/monitoring-EKS-NodeInstanceRole",
DimensionRollupOption: "ZeroAndSingleDimensionRollup",
})

r2 := cfg.Exporters["awsemf/resource_attr_to_label"].(*Config)
assert.Equal(t, r2,
&Config{
ExporterSettings: configmodels.ExporterSettings{TypeVal: configmodels.Type(typeStr), NameVal: "awsemf/resource_attr_to_label"},
LogGroupName: "",
LogStreamName: "",
Endpoint: "",
RequestTimeoutSeconds: 30,
MaxRetries: 1,
NoVerifySSL: false,
ProxyAddress: "",
Region: "",
RoleARN: "",
DimensionRollupOption: "ZeroAndSingleDimensionRollup",
ResourceToTelemetrySettings: exporterhelper.ResourceToTelemetrySettings{Enabled: true},
})
}
21 changes: 21 additions & 0 deletions exporter/awsemfexporter/emf_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"go.opentelemetry.io/collector/config/configmodels"
"go.opentelemetry.io/collector/consumer/consumererror"
"go.opentelemetry.io/collector/consumer/pdata"
"go.opentelemetry.io/collector/exporter/exporterhelper"
"go.opentelemetry.io/collector/obsreport"
"go.uber.org/zap"
)
Expand Down Expand Up @@ -78,6 +79,26 @@ func New(
return emfExporter, nil
}

// NewEmfExporter creates a new exporter using exporterhelper
func NewEmfExporter(
config configmodels.Exporter,
params component.ExporterCreateParams,
) (component.MetricsExporter, error) {

exp, err := New(config, params)
if err != nil {
return nil, err
}

return exporterhelper.NewMetricsExporter(
config,
params.Logger,
exp.(*emfExporter).pushMetricsData,
exporterhelper.WithResourceToTelemetryConversion(config.(*Config).ResourceToTelemetrySettings),
exporterhelper.WithShutdown(exp.(*emfExporter).Shutdown),
)
}

func (emf *emfExporter) pushMetricsData(_ context.Context, md pdata.Metrics) (droppedTimeSeries int, err error) {
expConfig := emf.config.(*Config)
logGroup := "/metrics/default"
Expand Down
16 changes: 16 additions & 0 deletions exporter/awsemfexporter/emf_exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,3 +250,19 @@ func TestWrapErrorIfBadRequest(t *testing.T) {
err = wrapErrorIfBadRequest(&awsErr)
assert.False(t, consumererror.IsPermanent(err))
}

// This test verifies that if func New() returns an error then NewEmfExporter()
// will do so.
func TestNewEmfExporterWithoutConfig(t *testing.T) {
factory := NewFactory()
expCfg := factory.CreateDefaultConfig().(*Config)
env := stashEnv()
defer popEnv(env)
os.Setenv("AWS_STS_REGIONAL_ENDPOINTS", "fake")

assert.Nil(t, expCfg.logger)
exp, err := NewEmfExporter(expCfg, component.ExporterCreateParams{Logger: zap.NewNop()})
assert.NotNil(t, err)
assert.Nil(t, exp)
assert.NotNil(t, expCfg.logger)
}
2 changes: 1 addition & 1 deletion exporter/awsemfexporter/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,5 @@ func createMetricsExporter(_ context.Context,

expCfg := config.(*Config)

return New(expCfg, params)
return NewEmfExporter(expCfg, params)
}
3 changes: 3 additions & 0 deletions exporter/awsemfexporter/testdata/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ exporters:
awsemf/1:
region: 'us-west-2'
role_arn: "arn:aws:iam::123456789:role/monitoring-EKS-NodeInstanceRole"
awsemf/resource_attr_to_label:
resource_to_telemetry_conversion:
enabled: true

service:
pipelines:
Expand Down