-
Notifications
You must be signed in to change notification settings - Fork 47
feat: add stencil sink #368
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
Merged
Merged
Changes from 11 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
6b8e48c
feat: add stencil sink
scortier f07bc20
chore: update sink testing
scortier 41180c0
chore: update json schema format
scortier 5241390
chore: update avro schema format
scortier f8a305f
chore: add proto schema format
scortier 559d88c
chore: select schema format
scortier cfab1a0
refactor: update json schema format
scortier 0794dc3
refactor: update column datatype
scortier 2d8c080
refactor: update json schema type
scortier 6b968c9
chore: update postgres type
scortier 38232c9
test: update stencil sink test
scortier 6747bf0
chore: update stencil sink
scortier fdaa54f
refactor: update columns length check
scortier b4b5f3b
feat: add avro schema format
scortier 6fd95f6
test: increase test coverage
scortier fe0617b
chore: update approach to change schema format
scortier 2aa9a62
test: increase test coverage
scortier ec22330
docs: update stencil sink docs
scortier 746aed9
test: increase coverage
scortier 1898968
chore: update config
scortier fbf4c96
feat(stencil): use urn as schemaid
StewartJingga File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,18 @@ | ||
| # Stencil | ||
|
|
||
| Stencil is a schema registry that provides schema management and validation dynamically, efficiently, and reliably to ensure data compatibility across applications. | ||
|
|
||
| ## Usage | ||
|
|
||
| ```yaml | ||
| sinks: | ||
| name: stencil | ||
| config: | ||
| URL: https://stencil.com | ||
| namespaceId: test-namespace | ||
| schemaId: example | ||
| ``` | ||
|
|
||
| ## Contributing | ||
|
|
||
| Refer to the contribution guidelines for information on contributing to this module. |
This file contains hidden or 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,234 @@ | ||
| package stencil | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| _ "embed" | ||
| "encoding/json" | ||
| "fmt" | ||
| "github.com/odpf/meteor/models" | ||
| facetsv1beta1 "github.com/odpf/meteor/models/odpf/assets/facets/v1beta1" | ||
| assetsv1beta1 "github.com/odpf/meteor/models/odpf/assets/v1beta1" | ||
| "github.com/odpf/meteor/plugins" | ||
| "github.com/odpf/meteor/registry" | ||
| "github.com/odpf/meteor/utils" | ||
| "github.com/odpf/salt/log" | ||
| "github.com/pkg/errors" | ||
| "io/ioutil" | ||
| "net/http" | ||
| "strings" | ||
| ) | ||
|
|
||
| //go:embed README.md | ||
| var summary string | ||
|
|
||
| type Config struct { | ||
| Host string `mapstructure:"host" validate:"required"` | ||
| NamespaceID string `mapstructure:"namespaceId" validate:"required"` | ||
| SchemaID string `mapstructure:"schemaId" validate:"required"` | ||
| Headers map[string]string `mapstructure:"headers"` | ||
| } | ||
|
|
||
| var sampleConfig = `` | ||
|
|
||
| type httpClient interface { | ||
| Do(*http.Request) (*http.Response, error) | ||
| } | ||
|
|
||
| type Sink struct { | ||
| client httpClient | ||
| config Config | ||
| logger log.Logger | ||
| } | ||
|
|
||
| func New(c httpClient, logger log.Logger) plugins.Syncer { | ||
| sink := &Sink{client: c, logger: logger} | ||
| return sink | ||
| } | ||
|
|
||
| func (s *Sink) Info() plugins.Info { | ||
| return plugins.Info{ | ||
| Description: "Send metadata to stencil http service", | ||
| SampleConfig: sampleConfig, | ||
| Summary: summary, | ||
| Tags: []string{"http", "sink"}, | ||
| } | ||
| } | ||
|
|
||
| func (s *Sink) Validate(configMap map[string]interface{}) (err error) { | ||
| return utils.BuildConfig(configMap, &Config{}) | ||
| } | ||
|
|
||
| func (s *Sink) Init(ctx context.Context, configMap map[string]interface{}) (err error) { | ||
| if err = utils.BuildConfig(configMap, &s.config); err != nil { | ||
| return plugins.InvalidConfigError{Type: plugins.PluginTypeSink} | ||
| } | ||
|
|
||
| return | ||
| } | ||
|
|
||
| func (s *Sink) Sink(ctx context.Context, batch []models.Record) (err error) { | ||
|
|
||
| for _, record := range batch { | ||
| metadata := record.Data() | ||
|
|
||
| table, ok := metadata.(*assetsv1beta1.Table) | ||
| if !ok { | ||
| continue | ||
| } | ||
| s.logger.Info("sinking record to stencil", "record", table.GetResource().Urn) | ||
|
|
||
| stencilPayload, err := s.buildJsonStencilPayload(table) | ||
| if err != nil { | ||
| return errors.Wrap(err, "failed to build stencil payload") | ||
| } | ||
| if err = s.send(stencilPayload); err != nil { | ||
| return errors.Wrap(err, "error sending data") | ||
| } | ||
|
|
||
| s.logger.Info("successfully sinked record to stencil", "record", table.GetResource().Urn) | ||
| } | ||
|
|
||
| return | ||
| } | ||
|
|
||
| func (s *Sink) Close() (err error) { return } | ||
|
|
||
| func (s *Sink) send(record JsonSchema) (err error) { | ||
|
|
||
| // for json schema format | ||
| payloadBytes, err := json.Marshal(record) | ||
| if err != nil { | ||
| return | ||
| } | ||
|
|
||
| // send request | ||
| url := fmt.Sprintf("%s/v1beta1/namespaces/%s/schemas/%s", s.config.Host, s.config.NamespaceID, s.config.SchemaID) | ||
| req, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(payloadBytes)) | ||
| if err != nil { | ||
| return | ||
| } | ||
|
|
||
| for hdrKey, hdrVal := range s.config.Headers { | ||
| hdrVals := strings.Split(hdrVal, ",") | ||
| for _, val := range hdrVals { | ||
| req.Header.Add(hdrKey, val) | ||
| } | ||
| } | ||
|
|
||
| res, err := s.client.Do(req) | ||
| if err != nil { | ||
| return | ||
| } | ||
| if res.StatusCode == 200 { | ||
| return | ||
| } | ||
|
|
||
| var bodyBytes []byte | ||
| bodyBytes, err = ioutil.ReadAll(res.Body) | ||
| if err != nil { | ||
| return | ||
| } | ||
| err = fmt.Errorf("stencil returns %d: %v", res.StatusCode, string(bodyBytes)) | ||
|
|
||
| switch code := res.StatusCode; { | ||
| case code >= 500: | ||
| return plugins.NewRetryError(err) | ||
| default: | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| func (s *Sink) buildJsonStencilPayload(table *assetsv1beta1.Table) (JsonSchema, error) { | ||
| resource := table.GetResource() | ||
| properties := s.buildJsonProperties(table) | ||
|
|
||
| record := JsonSchema{ | ||
| Id: fmt.Sprintf("%s/%s.%s.json", s.config.Host, s.config.NamespaceID, s.config.SchemaID), | ||
| Schema: "https://json-schema.org/draft/2020-12/schema", | ||
| Title: resource.GetName(), | ||
| Type: JsonType(resource.GetType()), | ||
| Properties: properties, | ||
| } | ||
|
|
||
| return record, nil | ||
| } | ||
|
|
||
| func (s *Sink) buildJsonProperties(table *assetsv1beta1.Table) map[string]Property { | ||
| fmt.Println("aya kya") | ||
| columns := table.GetSchema().GetColumns() | ||
| if columns == nil { | ||
| fmt.Println("idhr aya kya") | ||
| return nil | ||
| } | ||
| columnRecord := make(map[string]Property) | ||
|
|
||
| for _, column := range columns { | ||
| fmt.Println("column m aya kya") | ||
| dataType := s.typeToJsonSchemaType(table, column) | ||
| fmt.Println("datatype", dataType) | ||
| columnType := []JsonType{dataType} | ||
|
|
||
| if column.IsNullable { | ||
| columnType = []JsonType{dataType, JsonTypeNull} | ||
| } | ||
|
|
||
| columnRecord[column.Name] = Property{ | ||
| Type: columnType, | ||
| Description: column.GetDescription(), | ||
| } | ||
| fmt.Println("col record", columnRecord) | ||
|
|
||
| } | ||
| fmt.Println("col record", columnRecord) | ||
|
|
||
| return columnRecord | ||
| } | ||
|
|
||
| func (s *Sink) typeToJsonSchemaType(table *assetsv1beta1.Table, column *facetsv1beta1.Column) (dataType JsonType) { | ||
| fmt.Println("schema m aya kya") | ||
| service := table.GetResource().GetService() | ||
|
|
||
| if service == "bigquery" { | ||
| fmt.Println("bigquery m aya kya") | ||
| switch column.DataType { | ||
| case "STRING", "DATE", "DATETIME", "TIME", "TIMESTAMP", "GEOGRAPHY": | ||
| dataType = JsonTypeString | ||
| case "INT64", "NUMERIC", "FLOAT64", "INT", "FLOAT", "BIGNUMERIC": | ||
| dataType = JsonTypeNumber | ||
| case "BYTES": | ||
| dataType = JsonTypeArray | ||
| case "BOOLEAN": | ||
| dataType = JsonTypeBoolean | ||
| case "RECORD": | ||
| dataType = JsonTypeObject | ||
| default: | ||
| dataType = JsonTypeString | ||
| } | ||
| } | ||
| if service == "postgres" { | ||
| switch column.DataType { | ||
| case "uuid", "integer", "decimal", "smallint", "bigint", "bit", "bit varying", "numeric", "real", "double precision", "cidr", "inet", "macaddr", "serial", "bigserial", "money": | ||
| dataType = JsonTypeNumber | ||
| case "varchar", "text", "character", "character varying", "date", "time", "timestamp", "interval", "point", "line", "path": | ||
| dataType = JsonTypeString | ||
| case "boolean": | ||
| dataType = JsonTypeBoolean | ||
| case "bytea", "integer[]", "character[]", "text[]": | ||
| dataType = JsonTypeArray | ||
| default: | ||
| dataType = JsonTypeString | ||
| } | ||
| } | ||
| fmt.Println("schema se gya kya") | ||
|
|
||
| return | ||
| } | ||
|
|
||
| func init() { | ||
| if err := registry.Sinks.Register("stencil", func() plugins.Syncer { | ||
| return New(&http.Client{}, plugins.GetLog()) | ||
| }); err != nil { | ||
| panic(err) | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.