-
Notifications
You must be signed in to change notification settings - Fork 2k
Introduce model translation and encoding interfaces #3200
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 1 commit
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
985605f
[wip] Introduce model translation and encodings interfaces
jrcamp deaafb6
use encode/decode consistently
jrcamp 2cf2810
review feedback
jrcamp b647d08
decouple encoding from model
jrcamp c22633a
add high level interface
jrcamp 2cbc215
cleanup
jrcamp dd2068d
Apply suggestions from code review
jrcamp 2cdfcc4
renamings
jrcamp 78a9a4c
reword error
jrcamp a1f4736
cleanup
jrcamp b384588
return interface instead of out parameter
jrcamp 1aa9a13
review feedback
jrcamp 8cadcad
serialize -> marshal
jrcamp 3460c5d
put in internal
jrcamp a4345e0
lint
jrcamp 1675664
Merge remote-tracking branch 'upstream/main' into unmarshal-intf
jrcamp 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package encodings | ||
|
|
||
| import "go.opentelemetry.io/collector/consumer/pdata" | ||
|
|
||
| // MetricsDecoder converts another protocol to pdata. | ||
| type MetricsDecoder interface { | ||
| // UnmarshalMetrics converts data in another protocol to pdata. | ||
| UnmarshalMetrics(bytes []byte) (pdata.Metrics, error) | ||
| } | ||
|
|
||
| // TracesDecoder converts another type of data to pdata. | ||
| type TracesDecoder interface { | ||
| // UnmarshalTraces converts data in another protocol to pdata. | ||
| UnmarshalTraces(bytes []byte) (pdata.Traces, error) | ||
| } | ||
|
|
||
| // LogsDecoder converts another type of data to pdata. | ||
| type LogsDecoder interface { | ||
| // UnmarshalLogs converts a data model of another protocol into pdata. | ||
| UnmarshalLogs(bytes []byte) (pdata.Logs, error) | ||
| } | ||
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,32 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package encodings | ||
|
|
||
| import "go.opentelemetry.io/collector/consumer/pdata" | ||
|
|
||
| // MetricsEncoder converts pdata to another type. | ||
jrcamp marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| type MetricsEncoder interface { | ||
| MarshalMetrics(md pdata.Metrics) ([]byte, error) | ||
jrcamp marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| // TracesEncoder converts pdata to another type. | ||
| type TracesEncoder interface { | ||
| MarshalTraces(md pdata.Traces) ([]byte, error) | ||
| } | ||
|
|
||
| // LogsEncoder converts pdata to another type. | ||
| type LogsEncoder interface { | ||
| MarshalLogs(md pdata.Logs) ([]byte, error) | ||
| } | ||
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,38 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package encodings | ||
|
|
||
| import "fmt" | ||
|
|
||
| // Encoding is the format that a protocol is serialized to. | ||
| type Encoding string | ||
jrcamp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const ( | ||
| Protobuf Encoding = "protobuf" | ||
| JSON Encoding = "json" | ||
| Thrift Encoding = "thrift" | ||
| ) | ||
|
|
||
| func (e Encoding) String() string { | ||
| return string(e) | ||
jrcamp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| type ErrInvalidEncoding struct { | ||
| Encoding Encoding | ||
| } | ||
|
|
||
| func (e *ErrInvalidEncoding) Error() string { | ||
| return fmt.Sprintf("unsupported encoding %q", e.Encoding) | ||
| } | ||
jrcamp marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
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,51 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package models | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "go.opentelemetry.io/collector/consumer/pdata" | ||
| ) | ||
|
|
||
| type MetricsModelTranslator interface { | ||
jrcamp marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // MetricsFromModel converts a data model of another protocol into pdata. | ||
| MetricsFromModel(src interface{}) (pdata.Metrics, error) | ||
jrcamp marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // MetricsToModel converts pdata to data model. | ||
| MetricsToModel(md pdata.Metrics, out interface{}) error | ||
| } | ||
|
|
||
| type TracesModelTranslator interface { | ||
| // TracesFromModel converts a data model of another protocol into pdata. | ||
| TracesFromModel(src interface{}) (pdata.Traces, error) | ||
| // TracesToModel converts pdata to data model. | ||
| TracesToModel(md pdata.Traces, out interface{}) error | ||
| } | ||
|
|
||
| type LogsModelTranslator interface { | ||
| // LogsFromModel converts a data model of another protocol into pdata. | ||
| LogsFromModel(src interface{}) (pdata.Logs, error) | ||
| // LogsToModel converts pdata to data model. | ||
| LogsToModel(md pdata.Logs, out interface{}) error | ||
| } | ||
|
|
||
| // ErrIncompatibleType details a type conversion error during translation. | ||
| type ErrIncompatibleType struct { | ||
| Model interface{} | ||
jrcamp marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // TODO: maybe do expected vs. actual? | ||
| } | ||
|
|
||
| func (i *ErrIncompatibleType) Error() string { | ||
| return fmt.Sprintf("model type %T is incompatible", i.Model) | ||
| } | ||
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,71 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package zipkinv2 | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
|
|
||
| zipkinmodel "github.com/openzipkin/zipkin-go/model" | ||
| "github.com/openzipkin/zipkin-go/proto/zipkin_proto3" | ||
|
|
||
| "go.opentelemetry.io/collector/consumer/pdata" | ||
| "go.opentelemetry.io/collector/protocols/encodings" | ||
| "go.opentelemetry.io/collector/protocols/models" | ||
| ) | ||
|
|
||
| var ( | ||
| _ encodings.TracesEncoder = (*Encoder)(nil) | ||
| _ encodings.TracesDecoder = (*Encoder)(nil) | ||
| ) | ||
|
|
||
| type Encoder struct { | ||
| models.TracesModelTranslator | ||
| // Encoding is the format Zipkin is serialized to. | ||
| Encoding encodings.Encoding | ||
| } | ||
|
|
||
| func (z *Encoder) UnmarshalTraces(bytes []byte) (pdata.Traces, error) { | ||
| switch z.Encoding { | ||
| case encodings.Protobuf: | ||
| spans, err := zipkin_proto3.ParseSpans(bytes, false) | ||
| if err != nil { | ||
| return pdata.NewTraces(), err | ||
| } | ||
| return z.TracesFromModel(spans) | ||
| case encodings.JSON: | ||
| var spans []*zipkinmodel.SpanModel | ||
| if err := json.Unmarshal(bytes, &spans); err != nil { | ||
| return pdata.NewTraces(), err | ||
| } | ||
| return z.TracesFromModel(spans) | ||
| default: | ||
| return pdata.NewTraces(), &encodings.ErrInvalidEncoding{Encoding: z.Encoding} | ||
| } | ||
| } | ||
|
|
||
| func (z *Encoder) MarshalTraces(td pdata.Traces) ([]byte, error) { | ||
| switch z.Encoding { | ||
| // TODO | ||
| // case protocols.Protobuf: | ||
| case encodings.JSON: | ||
| var spans []*zipkinmodel.SpanModel | ||
| if err := z.TracesToModel(td, &spans); err != nil { | ||
| return nil, err | ||
| } | ||
| return json.Marshal(spans) | ||
| default: | ||
| return nil, &encodings.ErrInvalidEncoding{Encoding: z.Encoding} | ||
| } | ||
| } |
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,50 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package zipkinv2 | ||
|
|
||
| import ( | ||
| zipkinmodel "github.com/openzipkin/zipkin-go/model" | ||
|
|
||
| "go.opentelemetry.io/collector/consumer/pdata" | ||
| "go.opentelemetry.io/collector/protocols/models" | ||
| "go.opentelemetry.io/collector/translator/trace/zipkin" | ||
| ) | ||
|
|
||
| var ( | ||
| _ models.TracesModelTranslator = (*Model)(nil) | ||
| ) | ||
|
|
||
| type Model struct { | ||
| // ParseStringTags is true if string tags should be automatically converted to numbers. | ||
| ParseStringTags bool | ||
| } | ||
|
|
||
| func (z *Model) TracesFromModel(src interface{}) (pdata.Traces, error) { | ||
| if model, ok := src.([]*zipkinmodel.SpanModel); ok { | ||
| return zipkin.V2SpansToInternalTraces(model, z.ParseStringTags) | ||
| } | ||
| return pdata.NewTraces(), &models.ErrIncompatibleType{Model: src} | ||
| } | ||
|
|
||
| func (z *Model) TracesToModel(td pdata.Traces, out interface{}) error { | ||
| if model, ok := out.(*[]*zipkinmodel.SpanModel); ok { | ||
| sm, err := zipkin.InternalTracesToZipkinSpans(td) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| *model = sm | ||
| } | ||
| return &models.ErrIncompatibleType{Model: out} | ||
| } |
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.