Skip to content

Commit

Permalink
optimze regex
Browse files Browse the repository at this point in the history
  • Loading branch information
khushijain21 committed Nov 8, 2024
1 parent 00dca57 commit 7946e54
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 10 deletions.
2 changes: 1 addition & 1 deletion connector/otlpjsonconnector/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func createDefaultConfig() component.Config {

// createLogsConnector returns a connector which consume logs and export logs
func createLogsConnector(
c context.Context,
_ context.Context,
set connector.Settings,
cfg component.Config,
nextConsumer consumer.Logs,
Expand Down
7 changes: 4 additions & 3 deletions connector/otlpjsonconnector/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,10 @@ func (c *connectorLogs) ConsumeLogs(ctx context.Context, pl plog.Logs) error {
token := lRecord.Body()

// Check if the "resourceLogs" key exists in the JSON data
if logRegex.MatchString(token.AsString()) {
value := token.AsString()
if logRegex.MatchString(value) {
var l plog.Logs
l, err := logsUnmarshaler.UnmarshalLogs([]byte(token.AsString()))
l, err := logsUnmarshaler.UnmarshalLogs([]byte(value))
if err != nil {
c.logger.Error("could not extract logs from otlp json", zap.Error(err))
continue
Expand All @@ -63,7 +64,7 @@ func (c *connectorLogs) ConsumeLogs(ctx context.Context, pl plog.Logs) error {
if err != nil {
c.logger.Error("could not consume logs from otlp json", zap.Error(err))
}
} else if metricRegex.MatchString(token.AsString()) || traceRegex.MatchString(token.AsString()) {
} else if metricRegex.MatchString(value) || traceRegex.MatchString(value) {
continue
} else {
c.logger.Error("Invalid otlp payload")
Expand Down
8 changes: 5 additions & 3 deletions connector/otlpjsonconnector/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,11 @@ func (c *connectorMetrics) ConsumeLogs(ctx context.Context, pl plog.Logs) error
for k := 0; k < logRecord.LogRecords().Len(); k++ {
lRecord := logRecord.LogRecords().At(k)
token := lRecord.Body()
if metricRegex.MatchString(token.AsString()) {

value := token.AsString()
if metricRegex.MatchString(value) {
var m pmetric.Metrics
m, err := metricsUnmarshaler.UnmarshalMetrics([]byte(token.AsString()))
m, err := metricsUnmarshaler.UnmarshalMetrics([]byte(value))
if err != nil {
c.logger.Error("could not extract metrics from otlp json", zap.Error(err))
continue
Expand All @@ -63,7 +65,7 @@ func (c *connectorMetrics) ConsumeLogs(ctx context.Context, pl plog.Logs) error
c.logger.Error("could not consume metrics from otlp json", zap.Error(err))
}

} else if logRegex.MatchString(token.AsString()) || traceRegex.MatchString(token.AsString()) {
} else if logRegex.MatchString(value) || traceRegex.MatchString(value) {
continue
} else {
c.logger.Error("Invalid otlp payload")
Expand Down
7 changes: 4 additions & 3 deletions connector/otlpjsonconnector/traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,10 @@ func (c *connectorTraces) ConsumeLogs(ctx context.Context, pl plog.Logs) error {
lRecord := logRecord.LogRecords().At(k)
token := lRecord.Body()

if traceRegex.MatchString(token.AsString()) {
value := token.AsString()
if traceRegex.MatchString(value) {
var t ptrace.Traces
t, err := tracesUnmarshaler.UnmarshalTraces([]byte(token.AsString()))
t, err := tracesUnmarshaler.UnmarshalTraces([]byte(value))
if err != nil {
c.logger.Error("could not extract traces from otlp json", zap.Error(err))
continue
Expand All @@ -63,7 +64,7 @@ func (c *connectorTraces) ConsumeLogs(ctx context.Context, pl plog.Logs) error {
if err != nil {
c.logger.Error("could not consume traces from otlp json", zap.Error(err))
}
} else if metricRegex.MatchString(token.AsString()) || logRegex.MatchString(token.AsString()) {
} else if metricRegex.MatchString(value) || logRegex.MatchString(value) {
continue
} else {
c.logger.Error("Invalid otlp payload")
Expand Down

0 comments on commit 7946e54

Please sign in to comment.