-
Notifications
You must be signed in to change notification settings - Fork 714
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
kafka-streams: consolidate code (#1408)
Signed-off-by: Adrian Cole <adrian@tetrate.io>
- Loading branch information
1 parent
b62f266
commit ad678d2
Showing
4 changed files
with
102 additions
and
102 deletions.
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
instrumentation/kafka-streams/src/main/java/brave/kafka/streams/BaseTracingProcessor.java
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,64 @@ | ||
/* | ||
* Copyright 2013-2024 The OpenZipkin 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 brave.kafka.streams; | ||
|
||
import brave.Span; | ||
import brave.Tracer; | ||
import org.apache.kafka.common.header.Headers; | ||
import org.apache.kafka.streams.processor.api.ProcessingContext; | ||
|
||
import static brave.internal.Throwables.propagateIfFatal; | ||
|
||
abstract class BaseTracingProcessor<C extends ProcessingContext, R, P> { | ||
final KafkaStreamsTracing kafkaStreamsTracing; | ||
final Tracer tracer; | ||
final String spanName; | ||
final P delegate; | ||
C context; | ||
|
||
BaseTracingProcessor(KafkaStreamsTracing kafkaStreamsTracing, String spanName, P delegate) { | ||
this.kafkaStreamsTracing = kafkaStreamsTracing; | ||
this.tracer = kafkaStreamsTracing.tracer; | ||
this.spanName = spanName; | ||
this.delegate = delegate; | ||
} | ||
|
||
abstract Headers headers(R record); | ||
|
||
abstract void process(P delegate, R record); | ||
|
||
public void process(R record) { | ||
Span span = kafkaStreamsTracing.nextSpan(context, headers(record)); | ||
if (!span.isNoop()) { | ||
span.name(spanName); | ||
span.start(); | ||
} | ||
|
||
Tracer.SpanInScope scope = tracer.withSpanInScope(span); | ||
Throwable error = null; | ||
try { | ||
process(delegate, record); | ||
} catch (Throwable e) { | ||
error = e; | ||
propagateIfFatal(e); | ||
throw e; | ||
} finally { | ||
// Inject this span so that the next stage uses it as a parent | ||
kafkaStreamsTracing.injector.inject(span.context(), headers(record)); | ||
if (error != null) span.error(error); | ||
span.finish(); | ||
scope.close(); | ||
} | ||
} | ||
} |
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