-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[exporter/doris] Second PR of New component: Doris Exporter (#34980)
**Description:** Second PR of New component: Doris Exporter. Implementation of traces. **Link to tracking Issue:** #33479 **Testing:** **Documentation:**
- Loading branch information
1 parent
e3a44b8
commit 601a99d
Showing
12 changed files
with
693 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Use this changelog template to create an entry for release notes. | ||
|
||
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' | ||
change_type: new_component | ||
|
||
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) | ||
component: dorisexporter | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: traces implementation | ||
|
||
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
issues: [33479] | ||
|
||
# (Optional) One or more lines of additional information to render under the primary note. | ||
# These lines will be padded with 2 spaces and then inserted directly into the document. | ||
# Use pipe (|) for multiline entries. | ||
subtext: | ||
|
||
# If your change doesn't affect end users or the exported elements of any package, | ||
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. | ||
# Optional: The change log or logs in which this entry should be included. | ||
# e.g. '[user]' or '[user, api]' | ||
# Include 'user' if the change is relevant to end users. | ||
# Include 'api' if there is a change to a library API. | ||
# Default: '[user]' | ||
change_logs: [user] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package dorisexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/dorisexporter" | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"database/sql" | ||
"fmt" | ||
"net/http" | ||
"time" | ||
|
||
_ "github.com/go-sql-driver/mysql" // for register database driver | ||
"go.opentelemetry.io/collector/component" | ||
"go.uber.org/zap" | ||
) | ||
|
||
const timeFormat = "2006-01-02 15:04:05.999999" | ||
|
||
type commonExporter struct { | ||
component.TelemetrySettings | ||
|
||
client *http.Client | ||
|
||
logger *zap.Logger | ||
cfg *Config | ||
timeZone *time.Location | ||
} | ||
|
||
func newExporter(logger *zap.Logger, cfg *Config, set component.TelemetrySettings) *commonExporter { | ||
// There won't be an error because it's already been validated in the Config.Validate method. | ||
timeZone, _ := cfg.timeZone() | ||
|
||
return &commonExporter{ | ||
TelemetrySettings: set, | ||
logger: logger, | ||
cfg: cfg, | ||
timeZone: timeZone, | ||
} | ||
} | ||
|
||
func (e *commonExporter) formatTime(t time.Time) string { | ||
return t.In(e.timeZone).Format(timeFormat) | ||
} | ||
|
||
type streamLoadResponse struct { | ||
TxnID int64 | ||
Label string | ||
Status string | ||
ExistingJobStatus string | ||
Message string | ||
NumberTotalRows int64 | ||
NumberLoadedRows int64 | ||
NumberFilteredRows int64 | ||
NumberUnselectedRows int64 | ||
LoadBytes int64 | ||
LoadTimeMs int64 | ||
BeginTxnTimeMs int64 | ||
StreamLoadPutTimeMs int64 | ||
ReadDataTimeMs int64 | ||
WriteDataTimeMs int64 | ||
CommitAndPublishTimeMs int64 | ||
ErrorURL string | ||
} | ||
|
||
func (r *streamLoadResponse) success() bool { | ||
return r.Status == "Success" || r.Status == "Publish Timeout" | ||
} | ||
|
||
func streamLoadURL(address string, db string, table string) string { | ||
return address + "/api/" + db + "/" + table + "/_stream_load" | ||
} | ||
|
||
func streamLoadRequest(ctx context.Context, cfg *Config, table string, data []byte) (*http.Request, error) { | ||
url := streamLoadURL(cfg.Endpoint, cfg.Database, table) | ||
req, err := http.NewRequestWithContext(ctx, http.MethodPut, url, bytes.NewBuffer(data)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
req.Header.Set("format", "json") | ||
req.Header.Set("Expect", "100-continue") | ||
req.Header.Set("strip_outer_array", "true") | ||
req.SetBasicAuth(cfg.Username, string(cfg.Password)) | ||
|
||
return req, nil | ||
} | ||
|
||
func createDorisHTTPClient(ctx context.Context, cfg *Config, host component.Host, settings component.TelemetrySettings) (*http.Client, error) { | ||
client, err := cfg.ClientConfig.ToClient(ctx, host, settings) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
client.CheckRedirect = func(req *http.Request, _ []*http.Request) error { | ||
req.SetBasicAuth(cfg.Username, string(cfg.Password)) | ||
return nil | ||
} | ||
|
||
return client, nil | ||
} | ||
|
||
func createDorisMySQLClient(cfg *Config) (*sql.DB, error) { | ||
dsn := fmt.Sprintf("%s:%s@tcp(%s)/mysql", cfg.Username, string(cfg.Password), cfg.MySQLEndpoint) | ||
conn, err := sql.Open("mysql", dsn) | ||
return conn, err | ||
} | ||
|
||
func createAndUseDatabase(ctx context.Context, conn *sql.DB, cfg *Config) error { | ||
_, err := conn.ExecContext(ctx, "CREATE DATABASE IF NOT EXISTS "+cfg.Database) | ||
if err != nil { | ||
return err | ||
} | ||
_, err = conn.ExecContext(ctx, "USE "+cfg.Database) | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package dorisexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/dorisexporter" | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
"go.opentelemetry.io/collector/component" | ||
"go.opentelemetry.io/collector/config/configtelemetry" | ||
"go.opentelemetry.io/otel/metric" | ||
) | ||
|
||
var testTelemetrySettings = component.TelemetrySettings{ | ||
LeveledMeterProvider: func(_ configtelemetry.Level) metric.MeterProvider { | ||
return nil | ||
}, | ||
} | ||
|
||
func TestNewCommonExporter(t *testing.T) { | ||
cfg := createDefaultConfig().(*Config) | ||
exporter := newExporter(nil, cfg, testTelemetrySettings) | ||
require.NotNil(t, exporter) | ||
} | ||
|
||
func TestCommonExporter_FormatTime(t *testing.T) { | ||
cfg := createDefaultConfig().(*Config) | ||
exporter := newExporter(nil, cfg, testTelemetrySettings) | ||
require.NotNil(t, exporter) | ||
|
||
now := time.Date(2024, 1, 1, 0, 0, 0, 1000, time.Local) | ||
require.Equal(t, "2024-01-01 00:00:00.000001", exporter.formatTime(now)) | ||
} | ||
|
||
func TestStreamLoadResponse_Success(t *testing.T) { | ||
resp := &streamLoadResponse{ | ||
Status: "Success", | ||
} | ||
require.True(t, resp.success()) | ||
|
||
resp.Status = "Publish Timeout" | ||
require.True(t, resp.success()) | ||
|
||
resp.Status = "Fail" | ||
require.False(t, resp.success()) | ||
} | ||
|
||
func TestStreamLoadUrl(t *testing.T) { | ||
url := streamLoadURL("http://doris:8030", "otel", "otel_logs") | ||
require.Equal(t, "http://doris:8030/api/otel/otel_logs/_stream_load", url) | ||
} |
Oops, something went wrong.