Skip to content

Commit

Permalink
refactor: endpoint and mysql_endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
joker-star-l committed Aug 14, 2024
1 parent f23099b commit 0854933
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 29 deletions.
5 changes: 2 additions & 3 deletions exporter/dorisexporter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ This exporter supports sending traces, metrics, and logs data to [Apache Doris](

The following configuration options are supported:

* `endpoint`
* `http` The http stream load address.
* `tcp` The mysql protocol tcp address; ignored if create_schema is false.
* `endpoint` The http stream load address.
* `database` (default = otel) The database name.
* `username` The authentication username.
* `password` The authentication password.
Expand All @@ -28,6 +26,7 @@ The following configuration options are supported:
* `traces` (default = otel_traces) The table name for traces.
* `metrics` (default = otel_metrics) The table name for metrics.
* `create_schema` (default = true) Whether databases and tables are created automatically.
* `mysql_endpoint` The mysql protocol address to create the schema; ignored if create_schema is false.
* `history_days` (default = 0) Data older than these days will be deleted; ignored if create_schema is false. If set to 0, historical data will not be deleted.
* `timezone` (default = Asia/Shanghai) The time zone of doris.
* `timeout` (default = 5s) Time to wait per individual attempt to send data to a backend.
Expand Down
34 changes: 15 additions & 19 deletions exporter/dorisexporter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ type Config struct {
configretry.BackOffConfig `mapstructure:"retry_on_failure"`
exporterhelper.QueueSettings `mapstructure:"sending_queue"`

// Endpoint is the http stream load address and mysql protocol tcp address.
Endpoint `mapstructure:"endpoint"`
// TableNames is the table name for logs, traces and metrics.
Table `mapstructure:"table"`

// Endpoint is the http stream load address.
Endpoint string `mapstructure:"endpoint"`
// Database is the database name.
Database string `mapstructure:"database"`
// Username is the authentication username.
Expand All @@ -29,19 +29,14 @@ type Config struct {
Password string `mapstructure:"password"`
// CreateSchema is whether databases and tables are created automatically.
CreateSchema bool `mapstructure:"create_schema"`
// MySQLEndpoint is the mysql protocol address to create the schema; ignored if create_schema is false.
MySQLEndpoint string `mapstructure:"mysql_endpoint"`
// Data older than these days will be deleted; ignored if create_schema is false. If set to 0, historical data will not be deleted.
HistoryDays int32 `mapstructure:"history_days"`
// Timezone is the timezone of the doris.
TimeZone string `mapstructure:"timezone"`
}

type Endpoint struct {
// HTTP is the http stream load address.
HTTP string `mapstructure:"http"`
// TCP is the mysql protocol tcp address; ignored if create_schema is false.
TCP string `mapstructure:"tcp"`
}

type Table struct {
// Logs is the table name for logs.
Logs string `mapstructure:"logs"`
Expand All @@ -52,12 +47,12 @@ type Table struct {
}

func (cfg *Config) Validate() (err error) {
if cfg.Endpoint.HTTP == "" {
err = errors.Join(err, errors.New("endpoint.http must be specified"))
if cfg.Endpoint == "" {
err = errors.Join(err, errors.New("endpoint must be specified"))
}
if cfg.CreateSchema {
if cfg.Endpoint.TCP == "" {
err = errors.Join(err, errors.New("endpoint.tcp must be specified"))
if cfg.MySQLEndpoint == "" {
err = errors.Join(err, errors.New("mysql_endpoint must be specified"))
}

if cfg.HistoryDays < 0 {
Expand All @@ -68,20 +63,21 @@ func (cfg *Config) Validate() (err error) {
return err
}

const (
defaultStart = -2147483648 // IntMin
defaultHistoryDays = 498
)

func (cfg *Config) startAndHistoryDays() (int32, int32) {
if cfg.HistoryDays == 0 {
return -2147483648, 498
return defaultStart, defaultHistoryDays
}
if cfg.HistoryDays > 498 {
return -cfg.HistoryDays, 498
return -cfg.HistoryDays, defaultHistoryDays
}
return -cfg.HistoryDays, cfg.HistoryDays
}

func (cfg *Config) timeZone() (*time.Location, error) {
return time.LoadLocation(cfg.TimeZone)
}

func (cfg *Config) Test() (*time.Location, error) {
return time.LoadLocation(cfg.TimeZone)
}
8 changes: 4 additions & 4 deletions exporter/dorisexporter/exporter_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"go.uber.org/zap"
)

const TimeFormat = "2006-01-02 15:04:05.999999"
const timeFormat = "2006-01-02 15:04:05.999999"

type commonExporter struct {
client *http.Client
Expand Down Expand Up @@ -47,7 +47,7 @@ func newExporter(logger *zap.Logger, cfg *Config) (*commonExporter, error) {
}

func (e *commonExporter) formatTime(t time.Time) string {
return t.In(e.timeZone).Format(TimeFormat)
return t.In(e.timeZone).Format(timeFormat)
}

type StreamLoadResponse struct {
Expand Down Expand Up @@ -79,7 +79,7 @@ func streamLoadUrl(address string, db string, table string) string {
}

func streamLoadRequest(ctx context.Context, cfg *Config, table string, data []byte) (*http.Request, error) {
url := streamLoadUrl(cfg.Endpoint.HTTP, cfg.Database, table)
url := streamLoadUrl(cfg.Endpoint, cfg.Database, table)
req, err := http.NewRequestWithContext(ctx, http.MethodPut, url, bytes.NewBuffer(data))
if err != nil {
return nil, err
Expand All @@ -95,7 +95,7 @@ func streamLoadRequest(ctx context.Context, cfg *Config, table string, data []by
}

func createMySQLClient(cfg *Config) (*sql.DB, error) {
dsn := fmt.Sprintf("%s:%s@tcp(%s)/mysql", cfg.Username, cfg.Password, cfg.Endpoint.TCP[6:])
dsn := fmt.Sprintf("%s:%s@tcp(%s)/mysql", cfg.Username, cfg.Password, cfg.MySQLEndpoint)
conn, err := sql.Open("mysql", dsn)
return conn, err
}
Expand Down
5 changes: 2 additions & 3 deletions exporter/dorisexporter/testdata/config.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
doris:
endpoint:
http: http://localhost:8030
tcp: tcp://localhost:9030
endpoint: http://localhost:8030
database: otel
username: root
password:
Expand All @@ -10,6 +8,7 @@ doris:
traces: otel_traces
metrics: otel_metrics
create_schema: true
mysql_endpoint: localhost:8030
history_days: 0
timezone: Asia/Shanghai
timeout: 5s
Expand Down

0 comments on commit 0854933

Please sign in to comment.