This file contains hidden or 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
// general http handler | |
func httpHandler(w http.ResponseWriter, r *http.Request) { | |
// get tracer provider from otel package and inject ctx to tracer | |
tracer := otel.GetTracerProvider().Tracer("httpHandler") | |
ctx, span := tracer.Start(r.Context(), "httpHandler") | |
defer span.End() | |
// create http client with transport for inject trace signal to http header automatically | |
client := &http.Client{ | |
Transport: otelhttp.NewTransport(nil), |
This file contains hidden or 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
// 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()) |
This file contains hidden or 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
syntax = "proto3"; | |
package api; | |
option go_package = "../api"; | |
service HelloService { | |
rpc SayHello (HelloRequest) returns (HelloResponse); | |
} | |
message HelloRequest { |
This file contains hidden or 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
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) |
This file contains hidden or 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
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 |
This file contains hidden or 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
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 { |
This file contains hidden or 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
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) | |
} |
This file contains hidden or 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
func main() { | |
startConsumerGroup([]string{"kafka:9092"}) | |
select {} | |
} | |
func startConsumerGroup(brokerList []string) { | |
consumerGroupHandler := Consumer{} | |
// Wrap instrumentation | |
handler := otelsarama.WrapConsumerGroupHandler(&consumerGroupHandler) |
This file contains hidden or 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
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() |
This file contains hidden or 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
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") |