-
Notifications
You must be signed in to change notification settings - Fork 5
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
Add tenant ID processor #12
Merged
Merged
Changes from 10 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
76cf494
Add tenantId processor
pavolloffay ba6bf34
More tests
pavolloffay 5f3799a
More tests and metrics
pavolloffay e7fa07a
Use random port
pavolloffay bfc504b
OTLP test and format code
pavolloffay 0f75df8
cleanup
pavolloffay 7905342
More comments
pavolloffay 15c39b2
metrics
pavolloffay 57e0db4
cleanup
pavolloffay ce1916a
rever 2
pavolloffay cf0594d
Error on multiple headers
pavolloffay c179ece
clean
pavolloffay 8313ea4
adapt to JC approach
pavolloffay 4a634a5
More errors
pavolloffay 1c00303
Merge branch 'main' into tenantid-processor2
jcchavezs 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 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
Large diffs are not rendered by default.
Oops, something went wrong.
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,13 @@ | ||
package tenantidprocessor | ||
|
||
import "go.opentelemetry.io/collector/config/configmodels" | ||
|
||
// Config defines config for tenantid processor | ||
type Config struct { | ||
configmodels.ProcessorSettings `mapstructure:",squash"` | ||
|
||
// TenantIDHeaderName defines tenant HTTP header name | ||
TenantIDHeaderName string `mapstructure:"tenantid_header_name"` | ||
// TenantIDAttributeKey defines span attribute key for tenant | ||
TenantIDAttributeKey string `mapstructure:"tenantid_attribute_key"` | ||
} |
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,60 @@ | ||
package tenantidprocessor | ||
|
||
import ( | ||
"context" | ||
"go.opencensus.io/stats/view" | ||
|
||
"go.opentelemetry.io/collector/component" | ||
"go.opentelemetry.io/collector/config/configmodels" | ||
"go.opentelemetry.io/collector/consumer" | ||
"go.opentelemetry.io/collector/processor/processorhelper" | ||
) | ||
|
||
const ( | ||
typeStr = "hypertrace_tenantid" | ||
defaultTenantIdHeaderName = "x-tenant-id" | ||
defaultTenantIdAttributeKey = "tenant-id" | ||
) | ||
|
||
// NewFactory creates a factory for the tenantid processor. | ||
// The processor adds tenant ID to every received span. | ||
// The processor returns an error when the tenant ID is missing. | ||
// The tenant ID header is obtained from the context object. | ||
// The batch processor cleans context, therefore this processor | ||
// has to be added before, ideally right after the receiver. | ||
func NewFactory() component.ProcessorFactory { | ||
return processorhelper.NewFactory( | ||
typeStr, | ||
createDefaultConfig, | ||
processorhelper.WithTraces(createTraceProcessor), | ||
) | ||
} | ||
|
||
func createDefaultConfig() configmodels.Processor { | ||
return &Config{ | ||
ProcessorSettings: configmodels.ProcessorSettings{ | ||
TypeVal: typeStr, | ||
NameVal: typeStr, | ||
}, | ||
TenantIDHeaderName: defaultTenantIdHeaderName, | ||
TenantIDAttributeKey: defaultTenantIdAttributeKey, | ||
} | ||
} | ||
|
||
func createTraceProcessor( | ||
_ context.Context, | ||
params component.ProcessorCreateParams, | ||
cfg configmodels.Processor, | ||
nextConsumer consumer.TracesConsumer, | ||
) (component.TracesProcessor, error) { | ||
pCfg := cfg.(*Config) | ||
return processorhelper.NewTraceProcessor( | ||
cfg, | ||
nextConsumer, | ||
&processor{ | ||
tenantIDAttributeKey: pCfg.TenantIDAttributeKey, | ||
tenantIDHeaderName: pCfg.TenantIDHeaderName, | ||
logger: params.Logger, | ||
tenantIDViews: make(map[string]*view.View), | ||
}) | ||
} |
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,30 @@ | ||
package tenantidprocessor | ||
|
||
import ( | ||
"go.opencensus.io/stats" | ||
"go.opencensus.io/stats/view" | ||
"go.opencensus.io/tag" | ||
) | ||
|
||
var ( | ||
tagTenantID = tag.MustNewKey("tenant-id") | ||
|
||
statSpanPerTenant = stats.Int64("tenant_id_span_count", "Number of spans received from a tenant", stats.UnitDimensionless) | ||
) | ||
|
||
// MetricViews returns the metrics views related to storage. | ||
func MetricViews() []*view.View { | ||
tags := []tag.Key{tagTenantID} | ||
|
||
viewSpanCount := &view.View{ | ||
Name: statSpanPerTenant.Name(), | ||
Description: statSpanPerTenant.Description(), | ||
Measure: statSpanPerTenant, | ||
Aggregation: view.Sum(), | ||
TagKeys: tags, | ||
} | ||
|
||
return []*view.View{ | ||
viewSpanCount, | ||
} | ||
} |
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,68 @@ | ||
package tenantidprocessor | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"go.opencensus.io/stats" | ||
"go.opencensus.io/tag" | ||
"strings" | ||
|
||
"go.opencensus.io/stats/view" | ||
"go.opentelemetry.io/collector/consumer/pdata" | ||
"go.opentelemetry.io/collector/processor/processorhelper" | ||
"go.uber.org/zap" | ||
"google.golang.org/grpc/metadata" | ||
) | ||
|
||
type processor struct { | ||
tenantIDHeaderName string | ||
tenantIDAttributeKey string | ||
logger *zap.Logger | ||
tenantIDViews map[string]*view.View | ||
} | ||
|
||
var _ processorhelper.TProcessor = (*processor)(nil) | ||
|
||
// ProcessTraces implements processorhelper.TProcessor | ||
func (p *processor) ProcessTraces(ctx context.Context, traces pdata.Traces) (pdata.Traces, error) { | ||
md, ok := metadata.FromIncomingContext(ctx) | ||
if !ok { | ||
p.logger.Error("Could not extract headers from context", zap.Int("num-spans", traces.SpanCount())) | ||
return traces, fmt.Errorf("missing header %s", p.tenantIDHeaderName) | ||
} | ||
|
||
tenantIDHeaders := md.Get(p.tenantIDHeaderName) | ||
if len(tenantIDHeaders) == 0 { | ||
return traces, nil | ||
} else if len(tenantIDHeaders) > 1 { | ||
p.logger.Warn("Multiple tenant IDs provided, only the first one will be used", | ||
zap.String("header-name", p.tenantIDHeaderName), zap.String("header-value", strings.Join(tenantIDHeaders, ","))) | ||
} | ||
|
||
tenantID := tenantIDHeaders[0] | ||
p.addTenantIdToSpans(traces, tenantID) | ||
|
||
ctx, _ = tag.New(ctx, | ||
tag.Insert(tagTenantID, tenantID)) | ||
stats.Record(ctx, statSpanPerTenant.M(int64(traces.SpanCount()))) | ||
|
||
return traces, nil | ||
} | ||
|
||
func (p *processor) addTenantIdToSpans(traces pdata.Traces, tenantIDHeaderValue string) { | ||
rss := traces.ResourceSpans() | ||
for i := 0; i < rss.Len(); i++ { | ||
rs := rss.At(i) | ||
|
||
ilss := rs.InstrumentationLibrarySpans() | ||
for j := 0; j < ilss.Len(); j++ { | ||
ils := ilss.At(j) | ||
|
||
spans := ils.Spans() | ||
for k := 0; k < spans.Len(); k++ { | ||
span := spans.At(k) | ||
span.Attributes().Insert(p.tenantIDAttributeKey, pdata.NewAttributeValueString(tenantIDHeaderValue)) | ||
} | ||
} | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is there ever a case where this would be valid? I'm inclined to err on the side of caution and
just drop spans which have multiple tenant header values.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TBH I don't know, it's probably safer to return an error in this case. Every user should have assigned a single tenant.