-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add FetchMessage and CommitMessages functions (#9)
* feat: add FetchMessage and CommitMessages functions * feat: add manuel commit support * feat: add integration test * chore: dummy commit * chore: add manual commit to the readme * fix: add fetchSpan end on consumer close * chore: disable lint for example folder * chore: remove nolint statements within example folder * chore: disable gocritic for example folder * chore: dummy commit --------- Co-authored-by: Abdulsametileri <sametileri07@gmail.com>
- Loading branch information
1 parent
dae8e8b
commit 529dd2c
Showing
10 changed files
with
229 additions
and
22 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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,93 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"time" | ||
|
||
otelkafkakonsumer "github.com/Trendyol/otel-kafka-konsumer" | ||
"github.com/segmentio/kafka-go" | ||
"go.opentelemetry.io/otel" | ||
"go.opentelemetry.io/otel/attribute" | ||
"go.opentelemetry.io/otel/exporters/jaeger" | ||
"go.opentelemetry.io/otel/propagation" | ||
"go.opentelemetry.io/otel/sdk/resource" | ||
"go.opentelemetry.io/otel/sdk/trace" | ||
semconv "go.opentelemetry.io/otel/semconv/v1.17.0" | ||
) | ||
|
||
func initJaegerTracer(url string) *trace.TracerProvider { | ||
exp, err := jaeger.New(jaeger.WithCollectorEndpoint(jaeger.WithEndpoint(url))) | ||
if err != nil { | ||
log.Fatalf("Err initializing jaeger instance %v", err) | ||
} | ||
|
||
tp := trace.NewTracerProvider( | ||
trace.WithBatcher(exp), | ||
trace.WithResource(resource.NewWithAttributes( | ||
semconv.SchemaURL, | ||
semconv.ServiceName("trace-demo"), | ||
attribute.String("environment", "prod"), | ||
)), | ||
) | ||
|
||
otel.SetTracerProvider(tp) | ||
otel.SetTextMapPropagator(propagation.TraceContext{}) | ||
|
||
return tp | ||
} | ||
|
||
func main() { | ||
tp := initJaegerTracer("http://localhost:14268/api/traces") | ||
defer func(tp *trace.TracerProvider, ctx context.Context) { | ||
err := tp.Shutdown(ctx) | ||
if err != nil { | ||
log.Fatal(err.Error()) | ||
} | ||
}(tp, context.Background()) | ||
|
||
segmentioReader := kafka.NewReader(kafka.ReaderConfig{ | ||
Brokers: []string{"localhost:29092"}, | ||
GroupTopics: []string{"opentel"}, | ||
GroupID: "opentel-manualcommit-cg", | ||
}) | ||
|
||
reader, err := otelkafkakonsumer.NewReader( | ||
segmentioReader, | ||
otelkafkakonsumer.WithTracerProvider(tp), | ||
otelkafkakonsumer.WithPropagator(propagation.TraceContext{}), | ||
otelkafkakonsumer.WithAttributes( | ||
[]attribute.KeyValue{ | ||
semconv.MessagingDestinationKindTopic, | ||
semconv.MessagingKafkaClientIDKey.String("opentel-manualcommit-cg"), | ||
}, | ||
), | ||
) | ||
if err != nil { | ||
log.Fatal(err.Error()) | ||
} | ||
|
||
for { | ||
message, err := reader.FetchMessage(context.Background()) | ||
if err != nil { | ||
fmt.Println(err.Error()) | ||
continue | ||
} | ||
fmt.Println(message) | ||
|
||
// Extract tracing info from message | ||
ctx := reader.TraceConfig.Propagator.Extract(context.Background(), otelkafkakonsumer.NewMessageCarrier(message)) | ||
|
||
tr := otel.Tracer("consumer") | ||
parentCtx, span := tr.Start(ctx, "work") | ||
time.Sleep(100 * time.Millisecond) | ||
span.End() | ||
|
||
_, span = tr.Start(parentCtx, "another work") | ||
time.Sleep(50 * time.Millisecond) | ||
span.End() | ||
|
||
reader.CommitMessages(context.Background(), *message) | ||
} | ||
} |
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
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