Skip to content

Commit

Permalink
use switch statement
Browse files Browse the repository at this point in the history
  • Loading branch information
khushijain21 committed Nov 8, 2024
1 parent 7946e54 commit 411c6fb
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
10 changes: 6 additions & 4 deletions connector/otlpjsonconnector/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ func (c *connectorLogs) ConsumeLogs(ctx context.Context, pl plog.Logs) error {

// Check if the "resourceLogs" key exists in the JSON data
value := token.AsString()
if logRegex.MatchString(value) {
switch {
case logRegex.MatchString(value):
var l plog.Logs
l, err := logsUnmarshaler.UnmarshalLogs([]byte(value))
if err != nil {
Expand All @@ -64,11 +65,12 @@ 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(value) || traceRegex.MatchString(value) {
case metricRegex.MatchString(value), traceRegex.MatchString(value):
// If it's a metric or trace payload, simply continue
continue
} else {
default:
// If no regex matches, log the invalid payload
c.logger.Error("Invalid otlp payload")

}

}
Expand Down
11 changes: 6 additions & 5 deletions connector/otlpjsonconnector/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ func (c *connectorMetrics) ConsumeLogs(ctx context.Context, pl plog.Logs) error
token := lRecord.Body()

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

} else if logRegex.MatchString(value) || traceRegex.MatchString(value) {
case logRegex.MatchString(value), traceRegex.MatchString(value):
// If it's a log or trace payload, simply continue
continue
} else {
default:
// If no regex matches, log the invalid payload
c.logger.Error("Invalid otlp payload")

}

}
Expand Down
10 changes: 6 additions & 4 deletions connector/otlpjsonconnector/traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ func (c *connectorTraces) ConsumeLogs(ctx context.Context, pl plog.Logs) error {
token := lRecord.Body()

value := token.AsString()
if traceRegex.MatchString(value) {
switch {
case traceRegex.MatchString(value):
var t ptrace.Traces
t, err := tracesUnmarshaler.UnmarshalTraces([]byte(value))
if err != nil {
Expand All @@ -64,11 +65,12 @@ 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(value) || logRegex.MatchString(value) {
case metricRegex.MatchString(value), logRegex.MatchString(value):
// If it's a metric or log payload, continue to the next iteration
continue
} else {
default:
// If no regex matches, log the invalid payload
c.logger.Error("Invalid otlp payload")

}
}
}
Expand Down

0 comments on commit 411c6fb

Please sign in to comment.