-
Notifications
You must be signed in to change notification settings - Fork 3.1k
(otelarrowexporter): Create exporter per unique value of configured metadataKeys #34827
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
mx-psi
merged 17 commits into
open-telemetry:main
from
moh-osman3:mohosman/arrow-exporter-by-metadata
Sep 19, 2024
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
828f63b
add metadata wrapper around exporter
kristinapathak 3061d33
Update exporter/otelarrowexporter/metadata.go
kristinapathak 7571c46
unit tests
moh-osman3 03b6d67
fix tests
moh-osman3 120f24e
remove MetadataConfig
moh-osman3 3079c8e
lint
moh-osman3 714af4c
changelog
moh-osman3 d4b068b
fix race and rm from interface
moh-osman3 178994a
more lint
moh-osman3 e1aa2a4
go generate?
moh-osman3 5f585be
make generate
moh-osman3 16c8657
review feedback
moh-osman3 00f640e
fix test
moh-osman3 3f13edf
partial review feedback to isolate race
moh-osman3 fa6b431
userAgent once
moh-osman3 38f3188
move netreporter and fix race?
moh-osman3 32b9cb6
gofmt
moh-osman3 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,27 @@ | ||
| # Use this changelog template to create an entry for release notes. | ||
|
|
||
| # One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' | ||
| change_type: enhancement | ||
|
|
||
| # The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) | ||
| component: otelarrowexporter | ||
|
|
||
| # A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
| note: Allow separate arrow exporter per unique value of configured metadataKeys. | ||
|
|
||
| # Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
| issues: [34178] | ||
|
|
||
| # (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: [] |
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
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
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,203 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package otelarrowexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/otelarrowexporter" | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "runtime" | ||
| "sort" | ||
| "strings" | ||
| "sync" | ||
|
|
||
| arrowPkg "github.com/apache/arrow/go/v16/arrow" | ||
| "go.opentelemetry.io/collector/client" | ||
| "go.opentelemetry.io/collector/component" | ||
| "go.opentelemetry.io/collector/consumer/consumererror" | ||
| "go.opentelemetry.io/collector/exporter" | ||
| "go.opentelemetry.io/collector/pdata/plog" | ||
| "go.opentelemetry.io/collector/pdata/pmetric" | ||
| "go.opentelemetry.io/collector/pdata/ptrace" | ||
| "go.opentelemetry.io/otel/attribute" | ||
| "go.uber.org/multierr" | ||
| "google.golang.org/grpc/metadata" | ||
|
|
||
| "github.com/open-telemetry/opentelemetry-collector-contrib/internal/otelarrow/compression/zstd" | ||
| "github.com/open-telemetry/opentelemetry-collector-contrib/internal/otelarrow/netstats" | ||
| ) | ||
|
|
||
| var ( | ||
| // errTooManyExporters is returned when the MetadataCardinalityLimit has been reached. | ||
| errTooManyExporters = consumererror.NewPermanent(errors.New("too many exporter metadata-value combinations")) | ||
| ) | ||
|
|
||
| type metadataExporter struct { | ||
| config *Config | ||
| settings exporter.Settings | ||
| scf streamClientFactory | ||
| host component.Host | ||
|
|
||
| metadataKeys []string | ||
| exporters sync.Map | ||
| netReporter *netstats.NetworkReporter | ||
|
|
||
| userAgent string | ||
|
|
||
| // Guards the size and the storing logic to ensure no more than limit items are stored. | ||
| // If we are willing to allow "some" extra items than the limit this can be removed and size can be made atomic. | ||
| lock sync.Mutex | ||
| size int | ||
| } | ||
|
|
||
| var _ exp = (*metadataExporter)(nil) | ||
|
|
||
| func newMetadataExporter(cfg component.Config, set exporter.Settings, streamClientFactory streamClientFactory) (exp, error) { | ||
| oCfg := cfg.(*Config) | ||
| netReporter, err := netstats.NewExporterNetworkReporter(set) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| userAgent := fmt.Sprintf("%s/%s (%s/%s)", | ||
| set.BuildInfo.Description, set.BuildInfo.Version, runtime.GOOS, runtime.GOARCH) | ||
|
|
||
| if !oCfg.Arrow.Disabled { | ||
| // Ignoring an error because Validate() was called. | ||
| _ = zstd.SetEncoderConfig(oCfg.Arrow.Zstd) | ||
|
|
||
| userAgent += fmt.Sprintf(" ApacheArrow/%s (NumStreams/%d)", arrowPkg.PkgVersion, oCfg.Arrow.NumStreams) | ||
| } | ||
| // use lower-case, to be consistent with http/2 headers. | ||
| mks := make([]string, len(oCfg.MetadataKeys)) | ||
| for i, k := range oCfg.MetadataKeys { | ||
| mks[i] = strings.ToLower(k) | ||
| } | ||
| sort.Strings(mks) | ||
| if len(mks) == 0 { | ||
| return newExporter(cfg, set, streamClientFactory, userAgent, netReporter) | ||
| } | ||
| return &metadataExporter{ | ||
| config: oCfg, | ||
| settings: set, | ||
| scf: streamClientFactory, | ||
| metadataKeys: mks, | ||
| userAgent: userAgent, | ||
| netReporter: netReporter, | ||
| }, nil | ||
| } | ||
|
|
||
| func (e *metadataExporter) getSettings() exporter.Settings { | ||
| return e.settings | ||
| } | ||
|
|
||
| func (e *metadataExporter) getConfig() component.Config { | ||
| return e.config | ||
| } | ||
|
|
||
| func (e *metadataExporter) start(_ context.Context, host component.Host) (err error) { | ||
| e.host = host | ||
| return nil | ||
| } | ||
|
|
||
| func (e *metadataExporter) shutdown(ctx context.Context) error { | ||
| var err error | ||
| e.exporters.Range(func(_ any, value any) bool { | ||
| be := value.(exp) | ||
| err = multierr.Append(err, be.shutdown(ctx)) | ||
| return true | ||
| }) | ||
| return err | ||
| } | ||
|
|
||
| func (e *metadataExporter) pushTraces(ctx context.Context, td ptrace.Traces) error { | ||
| s, mdata := e.getAttrSet(ctx, e.metadataKeys) | ||
|
|
||
| be, err := e.getOrCreateExporter(ctx, s, mdata) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| return be.pushTraces(ctx, td) | ||
| } | ||
|
|
||
| func (e *metadataExporter) pushMetrics(ctx context.Context, md pmetric.Metrics) error { | ||
| s, mdata := e.getAttrSet(ctx, e.metadataKeys) | ||
|
|
||
| be, err := e.getOrCreateExporter(ctx, s, mdata) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return be.pushMetrics(ctx, md) | ||
| } | ||
|
|
||
| func (e *metadataExporter) pushLogs(ctx context.Context, ld plog.Logs) error { | ||
| s, mdata := e.getAttrSet(ctx, e.metadataKeys) | ||
|
|
||
| be, err := e.getOrCreateExporter(ctx, s, mdata) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return be.pushLogs(ctx, ld) | ||
| } | ||
|
|
||
| func (e *metadataExporter) getOrCreateExporter(ctx context.Context, s attribute.Set, md metadata.MD) (exp, error) { | ||
| e.lock.Lock() | ||
| defer e.lock.Unlock() | ||
|
|
||
| if e.config.MetadataCardinalityLimit != 0 && e.size >= int(e.config.MetadataCardinalityLimit) { | ||
| return nil, errTooManyExporters | ||
| } | ||
|
|
||
| v, ok := e.exporters.Load(s) | ||
| if ok { | ||
| return v.(exp), nil | ||
| } | ||
|
|
||
| newExp, err := newExporter(e.config, e.settings, e.scf, e.userAgent, e.netReporter) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to create exporter: %w", err) | ||
| } | ||
|
|
||
| var loaded bool | ||
| v, loaded = e.exporters.LoadOrStore(s, newExp) | ||
| if !loaded { | ||
| // set metadata keys for base exporter to add them to the outgoing context. | ||
| newExp.(*baseExporter).setMetadata(md) | ||
|
|
||
| // Start the goroutine only if we added the object to the map, otherwise is already started. | ||
| err = newExp.start(ctx, e.host) | ||
| if err != nil { | ||
| e.exporters.Delete(s) | ||
| return nil, fmt.Errorf("failed to start exporter: %w", err) | ||
| } | ||
|
|
||
| e.size++ | ||
| } | ||
|
|
||
| return v.(exp), nil | ||
| } | ||
|
|
||
| // getAttrSet is code taken from the core collector's batchprocessor multibatch logic. | ||
| // https://github.com/open-telemetry/opentelemetry-collector/blob/v0.107.0/processor/batchprocessor/batch_processor.go#L298 | ||
| func (e *metadataExporter) getAttrSet(ctx context.Context, keys []string) (attribute.Set, metadata.MD) { | ||
| // Get each metadata key value, form the corresponding | ||
| // attribute set for use as a map lookup key. | ||
| info := client.FromContext(ctx) | ||
| md := map[string][]string{} | ||
| var attrs []attribute.KeyValue | ||
| for _, k := range keys { | ||
| // Lookup the value in the incoming metadata, copy it | ||
| // into the outgoing metadata, and create a unique | ||
| // value for the attributeSet. | ||
| vs := info.Metadata.Get(k) | ||
| md[k] = vs | ||
| if len(vs) == 1 { | ||
| attrs = append(attrs, attribute.String(k, vs[0])) | ||
| } else { | ||
| attrs = append(attrs, attribute.StringSlice(k, vs)) | ||
| } | ||
| } | ||
| return attribute.NewSet(attrs...), metadata.MD(md) | ||
| } | ||
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.