Skip to content

Commit

Permalink
[exporter/opensearch] Use ss4o as the default encoding schema
Browse files Browse the repository at this point in the history
Signed-off-by: João Henri <[email protected]>
  • Loading branch information
jaehnri committed Sep 6, 2023
1 parent 42c777d commit 2e579e0
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 23 deletions.
37 changes: 16 additions & 21 deletions exporter/opensearchexporter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package opensearchexporter // import "github.com/open-telemetry/opentelemetry-co

import (
"errors"
"fmt"
"strings"

"go.opentelemetry.io/collector/config/confighttp"
Expand All @@ -22,6 +21,9 @@ const (

// defaultBulkAction value is used when component.Config.BulkAction is not set.
defaultBulkAction = "create"

// defaultMappingMode value is used when component.Config.MappingSettings.Mode is not set.
defaultMappingMode = "ss4o"
)

// Config defines configuration for OpenSearch exporter.
Expand All @@ -34,23 +36,24 @@ type Config struct {
// The Observability indices would follow the recommended for immutable data stream ingestion pattern using
// the data_stream concepts. See https://opensearch.org/docs/latest/dashboards/im-dashboards/datastream/
// Index pattern will follow the next naming template ss4o_{type}-{dataset}-{namespace}
Namespace string `mapstructure:"namespace"`
Dataset string `mapstructure:"dataset"`
Namespace string `mapstructure:"namespace"`

// BulkAction configures the action for ingesting data. Only `create` and `index` are allowed here.
// If not specified, the default value `create` will be used.
BulkAction string `mapstructure:"bulk_action"`
}

var (
errConfigNoEndpoint = errors.New("endpoint must be specified")
errDatasetNoValue = errors.New("dataset must be specified")
errNamespaceNoValue = errors.New("namespace must be specified")
errBulkActionInvalid = errors.New("bulk_action can either be `create` or `index`")
errConfigNoEndpoint = errors.New("endpoint must be specified")
errDatasetNoValue = errors.New("dataset must be specified")
errNamespaceNoValue = errors.New("namespace must be specified")
errBulkActionInvalid = errors.New("bulk_action can either be `create` or `index`")
errMappingModeInvalid = errors.New("mapping.mode is invalid")
)

type MappingsSettings struct {
// Mode configures the field mappings.
// Mode configures the field mappings. Uses SS4O by default.
Mode string `mapstructure:"mode"`

// Additional field mappings.
Expand All @@ -75,38 +78,34 @@ type MappingMode int

// Enum values for MappingMode.
const (
MappingNone MappingMode = iota
MappingSS4O MappingMode = iota
MappingECS
MappingFlattenAttributes
)

func (m MappingMode) String() string {
switch m {
case MappingNone:
return ""
case MappingSS4O:
return "ss4o"
case MappingECS:
return "ecs"
case MappingFlattenAttributes:
return "flatten_attributes"
default:
return ""
return "ss4o"
}
}

var mappingModes = func() map[string]MappingMode {
table := map[string]MappingMode{}
for _, m := range []MappingMode{
MappingNone,
MappingECS,
MappingSS4O,
MappingFlattenAttributes,
} {
table[strings.ToLower(m.String())] = m
}

// config aliases
table["no"] = MappingNone
table["none"] = MappingNone

return table
}()

Expand All @@ -123,16 +122,12 @@ func (cfg *Config) Validate() error {
multiErr = append(multiErr, errNamespaceNoValue)
}

if len(cfg.BulkAction) == 0 {
cfg.BulkAction = "create"
}

if cfg.BulkAction != "create" && cfg.BulkAction != "index" {
return errBulkActionInvalid
}

if _, ok := mappingModes[cfg.MappingsSettings.Mode]; !ok {
return fmt.Errorf("unknown mapping mode %v", cfg.MappingsSettings.Mode)
multiErr = append(multiErr, errMappingModeInvalid)
}

return multierr.Combine(multiErr...)
Expand Down
3 changes: 3 additions & 0 deletions exporter/opensearchexporter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ func TestLoadConfig(t *testing.T) {
RandomizationFactor: 0.5,
},
BulkAction: defaultBulkAction,
MappingsSettings: MappingsSettings{
Mode: "ss4o",
},
},
configValidateAssert: assert.NoError,
},
Expand Down
2 changes: 1 addition & 1 deletion exporter/opensearchexporter/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (m *encodeModel) encodeLogSSO(
schemaURL string,
record plog.LogRecord,
) ([]byte, error) {
sso := ssoLog{}
sso := ssoRecord{}
sso.Attributes = record.Attributes().AsRaw()
sso.Body = record.Body().AsString()

Expand Down
1 change: 1 addition & 0 deletions exporter/opensearchexporter/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func newDefaultConfig() component.Config {
Dataset: defaultDataset,
BulkAction: defaultBulkAction,
RetrySettings: exporterhelper.NewDefaultRetrySettings(),
MappingsSettings: MappingsSettings{Mode: defaultMappingMode},
}
}

Expand Down
2 changes: 1 addition & 1 deletion exporter/opensearchexporter/sso_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ type ssoSpan struct {
TraceState string `json:"traceState"`
}

type ssoLog struct {
type ssoRecord struct {
Attributes map[string]any `json:"attributes,omitempty"`
Body string `json:"body"`
InstrumentationScope struct {
Expand Down

0 comments on commit 2e579e0

Please sign in to comment.