Skip to content

Instantly share code, notes, and snippets.

View TonPC64's full-sized avatar
:shipit:
Gopher

Chanwit Piromplad TonPC64

:shipit:
Gopher
View GitHub Profile
func handler(c *gin.Context) {
tracer := otel.GetTracerProvider().Tracer("gin-hander")
ctx, span := tracer.Start(c.Request.Context(), "handler-span")
defer span.End()
fmt.Println(c.Request.Header)
rand.Seed(time.Now().Unix())
msg := sarama.ProducerMessage{
Topic: "example-topic",
func init() {
config := sarama.NewConfig()
config.Version = sarama.V2_5_0_0
// So we can know the partition and offset of messages.
config.Producer.Return.Successes = true
var err error
producer, err = sarama.NewAsyncProducer([]string{"kafka:9092"}, config)
if err != nil {
log.Fatal().Err(err).Msg("Failed to start Sarama producer")
func printMessage(msg *sarama.ConsumerMessage) {
// Extract tracing info from message
ctx := otel.GetTextMapPropagator().Extract(context.Background(), otelsarama.NewConsumerMessageCarrier(msg))
tr := otel.GetTracerProvider().Tracer("consumer")
_, span := tr.Start(ctx, "consume message", trace.WithAttributes(
semconv.MessagingOperationProcess,
))
defer span.End()
func main() {
startConsumerGroup([]string{"kafka:9092"})
select {}
}
func startConsumerGroup(brokerList []string) {
consumerGroupHandler := Consumer{}
// Wrap instrumentation
handler := otelsarama.WrapConsumerGroupHandler(&consumerGroupHandler)
func callSayHello(ctx context.Context, c api.HelloServiceClient) {
md := metadata.Pairs(
"timestamp", time.Now().Format(time.StampNano),
)
ctx = metadata.NewOutgoingContext(ctx, md)
response, err := c.SayHello(ctx, &api.HelloRequest{Greeting: "World"})
if err != nil {
log.Fatalf("Error when calling SayHello: %s", err)
}
var grpcClient api.HelloServiceClient
func main() {
var conn *grpc.ClientConn
conn, err := grpc.Dial("host.docker.internal:6565", grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithUnaryInterceptor(otelgrpc.UnaryClientInterceptor()),
grpc.WithStreamInterceptor(otelgrpc.StreamClientInterceptor()),
)
if err != nil {
func (s *server) SayHello(ctx context.Context, in *api.HelloRequest) (*api.HelloResponse, error) {
tracer := otel.GetTracerProvider().Tracer("webserver-grpc")
_, span := tracer.Start(ctx, "SayHello-span")
defer span.End()
fmt.Println(ctx)
log.Printf("Received: %v\n", in.GetGreeting())
time.Sleep(50 * time.Millisecond)
return &api.HelloResponse{Reply: "Hello " + in.Greeting}, nil
type server struct {
api.HelloServiceServer
}
func (s *server) SayHello(ctx context.Context, in *api.HelloRequest) (*api.HelloResponse, error) {
tracer := otel.GetTracerProvider().Tracer("webserver-grpc")
_, span := tracer.Start(ctx, "SayHello-span")
defer span.End()
fmt.Println(ctx)
syntax = "proto3";
package api;
option go_package = "../api";
service HelloService {
rpc SayHello (HelloRequest) returns (HelloResponse);
}
message HelloRequest {
// create http client with transport for inject trace signal to http header automatically
client := &http.Client{
Transport: otelhttp.NewTransport(nil),
}
// create http request with context for inject trace signal to http header in request
request, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://echo:8081", nil)
if err != nil {
// if error should set status to span
span.RecordError(err)
span.SetStatus(codes.Error, codes.Error.String())