0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

OpenTelemetry(Java)のAnnotationsを試してみた

Last updated at Posted at 2022-09-18

はじめに

以下にあるAnnotations機能を試してみました。

以前試したチュートリアルの内容を使って試したので、その続きとなってます。

実施内容

ソースコードの修正

  • 修正対象

grpc-java/examples/src/main/java/io/grpc/examples/helloworld/HelloWorldServer.java

  • 修正内容

import文の追加

import io.opentelemetry.instrumentation.annotations.SpanAttribute;
import io.opentelemetry.instrumentation.annotations.WithSpan;

SPANの追加

  static class GreeterImpl extends GreeterGrpc.GreeterImplBase {

    @Override
    public void sayHello(HelloRequest req, StreamObserver<HelloReply> responseObserver) {
      test01();  // (追加部分)追加したメソッドを呼び出すようにする
      HelloReply reply = HelloReply.newBuilder().setMessage("Hello " + req.getName()).build();
      responseObserver.onNext(reply);
      responseObserver.onCompleted();
    }

    // (追加部分)追加したメソッド。このメソッドを1つのSPANとして扱う
    @WithSpan
    public void test01(){
      logger.info("test01");
    }

  }
  • build.gradleの修正
dependencies {
    implementation "io.grpc:grpc-protobuf:${grpcVersion}"
    implementation "io.grpc:grpc-stub:${grpcVersion}"
    compileOnly "org.apache.tomcat:annotations-api:6.0.53"

    // examples/advanced need this for JsonFormat
    implementation "com.google.protobuf:protobuf-java-util:${protobufVersion}"

    runtimeOnly "io.grpc:grpc-netty-shaded:${grpcVersion}"

    testImplementation "io.grpc:grpc-testing:${grpcVersion}"
    testImplementation "junit:junit:4.12"
    testImplementation "org.mockito:mockito-core:3.4.0"

    // (追加部分)ライブラリの追加
    implementation('io.opentelemetry.instrumentation:opentelemetry-instrumentation-annotations:1.18.0-alpha')
}
  • 再ビルド
$ ./gradlew installDist

プログラム実行

  • サーバサイドプログラムの起動
$ ./build/install/examples/bin/hello-world-server
OpenJDK 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended
[otel.javaagent 2022-09-18 02:31:57:354 +0000] [main] INFO io.opentelemetry.javaagent.tooling.VersionLogger - opentelemetry-javaagent - version: 1.18.0
Sep 18, 2022 2:32:00 AM io.grpc.examples.helloworld.HelloWorldServer start
INFO: Server started, listening on 50051
  • (サーバサイドプログラムを起動した状態で別プロンプトを立ち上げて)クライアントプログラムの起動
$ ./build/install/examples/bin/hello-world-client
Sep 18, 2022 2:32:32 AM io.grpc.examples.helloworld.HelloWorldClient greet
INFO: Will try to greet world ...
Sep 18, 2022 2:32:33 AM io.grpc.examples.helloworld.HelloWorldClient greet
INFO: Greeting: Hello world
  • クライアントプログラムを起動すると、サーバサイドプログラム側のプロンプトでOpenTelemetryのログが出力される
    修正前と比較すると test01 が新たなSPANとしてログ表示される(上から3行目)
Sep 18, 2022 2:32:32 AM io.grpc.examples.helloworld.HelloWorldServer$GreeterImpl test01
INFO: test01
[otel.javaagent 2022-09-18 02:32:32:987 +0000] [grpc-default-executor-0] INFO io.opentelemetry.exporter.logging.LoggingSpanExporter - 'GreeterImpl.test01' : dd9921f3c0314654ae197242432cc9f9 4c4efffdbbe35918 INTERNAL [tracer: io.opentelemetry.opentelemetry-instrumentation-annotations-1.16:1.18.0-alpha] AttributesMap{data={thread.name=grpc-default-executor-0, code.namespace=io.grpc.examples.helloworld.HelloWorldServer$GreeterImpl, thread.id=16, code.function=test01}, capacity=128, totalAddedValues=4}
[otel.javaagent 2022-09-18 02:32:33:041 +0000] [grpc-default-executor-0] INFO io.opentelemetry.exporter.logging.LoggingSpanExporter - 'helloworld.Greeter/SayHello' : dd9921f3c0314654ae197242432cc9f9 de6954583ab49d1f SERVER [tracer: io.opentelemetry.grpc-1.6:1.18.0-alpha] AttributesMap{data={net.host.port=50051, rpc.system=grpc, thread.name=grpc-default-executor-0, net.sock.peer.port=56540, net.transport=ip_tcp, thread.id=16, net.host.name=localhost, rpc.method=SayHello, rpc.grpc.status_code=0, rpc.service=helloworld.Greeter, net.sock.peer.addr=127.0.0.1}, capacity=128, totalAddedValues=11}

適当に整形すると以下

[otel.javaagent 2022-09-18 02:32:32:987 +0000] [grpc-default-executor-0] INFO
io.opentelemetry.exporter.logging.LoggingSpanExporter - 'GreeterImpl.test01' :
    dd9921f3c0314654ae197242432cc9f9    # TraceID、helloworld.Greeter/SayHelloと同じ
    4c4efffdbbe35918    # SpanID、helloworld.Greeter/SayHelloと異なる
    INTERNAL [tracer: io.opentelemetry.opentelemetry-instrumentation-annotations-1.16:1.18.0-alpha] 
    AttributesMap { 
      data = {
        thread.name=grpc-default-executor-0,
        code.namespace=io.grpc.examples.helloworld.HelloWorldServer$GreeterImpl,
        thread.id=16,
        code.function=test01
      },
      capacity=128,
      totalAddedValues=4
    }
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?