-
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.
Implement spans exporting for ClickHouse storage
Signed-off-by: haanhvu <[email protected]>
- Loading branch information
Showing
10 changed files
with
408 additions
and
8 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
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
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,83 @@ | ||
// Copyright (c) 2023 The Jaeger Authors. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package clickhouse | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"errors" | ||
"fmt" | ||
"net/url" | ||
|
||
"github.com/ClickHouse/clickhouse-go/v2" | ||
) | ||
|
||
type Config struct { | ||
Endpoint string `mapstructure:"endpoint"` | ||
Username string `mapstructure:"username"` | ||
Password string `mapstructure:"password"` | ||
Database string `mapstructure:"database"` | ||
SpansTableName string `mapstructure:"spans_table_name"` | ||
//materialized views' names? | ||
} | ||
|
||
const ( | ||
defaultDatabase = "default" | ||
defaultUsername = "default" | ||
defaultPassword = "" | ||
) | ||
|
||
func (cfg *Config) NewClient(ctx context.Context) (*sql.DB, error) { | ||
if cfg.Endpoint == "" { | ||
return nil, errors.New("no endpoints specified") | ||
} | ||
|
||
dsnURL, err := url.Parse(cfg.Endpoint) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
queryParams := dsnURL.Query() | ||
|
||
if dsnURL.Scheme == "https" { | ||
queryParams.Set("secure", "true") | ||
} | ||
|
||
if cfg.Database != "" { | ||
dsnURL.Path = cfg.Database | ||
} else { | ||
dsnURL.Path = defaultDatabase | ||
} | ||
|
||
if cfg.Username != "" { | ||
cfg.Username = defaultUsername | ||
} | ||
|
||
if cfg.Password != "" { | ||
cfg.Password = defaultPassword | ||
} | ||
|
||
dsnURL.User = url.UserPassword(cfg.Username, cfg.Password) | ||
|
||
dsnURL.RawQuery = queryParams.Encode() | ||
|
||
dsn := dsnURL.String() | ||
|
||
if _, err = clickhouse.ParseDSN(dsn); err != nil { | ||
return nil, err | ||
} | ||
|
||
db, err := sql.Open("clickhouse", dsn) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
createDBquery := fmt.Sprintf("CREATE DATABASE IF NOT EXISTS %s", cfg.Database) | ||
_, err = db.ExecContext(ctx, createDBquery) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return db, nil | ||
} |
Oops, something went wrong.