Package: nucliadb-telemetry
NucliaDB TelemetryOpen telemetry compatible plugin to propagate traceid on FastAPI, Nats and GRPC with Asyncio.ENV vars:JAEGER_ENABLED = True
    JAEGER_HOST = "127.0.0.1"
    JAEGER_PORT = server.portOn FastAPI you should add:tracer_provider=get_telemetry("HTTP_SERVICE")app=FastAPI(title="Test API")# type: ignoreifnottracer_provider.initialized:awaitinit_telemetry(tracer_provider)set_global_textmap(B3MultiFormat())FastAPIInstrumentor.instrument_app(app,tracer_provider=tracer_provider)..awaitinit_telemetry(tracer_provider)# To start asyncio task..On GRPC Server you should add:tracer_provider=get_telemetry("GRPC_SERVER_SERVICE")telemetry_grpc=GRPCTelemetry("GRPC_CLIENT_SERVICE",tracer_provider)ifnottracer_provider.initialized:awaitinit_telemetry(tracer_provider)set_global_textmap(B3MultiFormat())server=telemetry_grpc.init_server()helloworld_pb2_grpc.add_GreeterServicer_to_server(SERVICER,server)..awaitinit_telemetry(tracer_provider)# To start asyncio task..On GRPC Client you should add:tracer_provider=get_telemetry("GRPC_CLIENT_SERVICE")telemetry_grpc=GRPCTelemetry("GRPC_CLIENT_SERVICE",tracer_provider)ifnottracer_provider.initialized:awaitinit_telemetry(tracer_provider)set_global_textmap(B3MultiFormat())channel=telemetry_grpc.init_client(f"localhost:{grpc_service}")stub=helloworld_pb2_grpc.GreeterStub(channel)..awaitinit_telemetry(tracer_provider)# To start asyncio task..On Nats jetstream push subscriber you should add:nc=awaitnats.connect(servers=[self.natsd])js=self.nc.jetstream()tracer_provider=get_telemetry("NATS_SERVICE")ifnottracer_provider.initialized:awaitinit_telemetry(tracer_provider)set_global_textmap(B3MultiFormat())jsotel=JetStreamContextTelemetry(js,"NATS_SERVICE",tracer_provider)subscription=awaitjsotel.subscribe(subject="testing.telemetry",stream="testing",cb=handler,)On Nats publisher you should add:nc=awaitnats.connect(servers=[self.natsd])js=self.nc.jetstream()tracer_provider=get_telemetry("NATS_SERVICE")ifnottracer_provider.initialized:awaitinit_telemetry(tracer_provider)set_global_textmap(B3MultiFormat())jsotel=JetStreamContextTelemetry(js,"NATS_SERVICE",tracer_provider)awaitjsotel.publish("testing.telemetry",request.name.encode())On Nats jetstream pull subscription you can use different patterns if you want to
just get one message and exit or pull several ones. For just one messagenc=awaitnats.connect(servers=[self.natsd])js=self.nc.jetstream()tracer_provider=get_telemetry("NATS_SERVICE")ifnottracer_provider.initialized:awaitinit_telemetry(tracer_provider)set_global_textmap(B3MultiFormat())jsotel=JetStreamContextTelemetry(js,"NATS_SERVICE",tracer_provider)# You can use either pull_subscribe or pull_subscribe_bindsubscription=awaitjsotel.pull_subscribe(subject="testing.telemetry",durable="consumer_name"stream="testing",)asyncdefcallback(message):# Do something with your message# and optionally return somethingreturnTruetry:result=awaitjsotel.pull_one(subscription,callback)excepterrors.TimeoutErrorpassFor multiple messages just wrap it in a loop:whileTrue:try:result=awaitjsotel.pull_one(subscription,callback)excepterrors.TimeoutErrorpassOn Nats client (NO Jestream! ) publisher you should add:nc=awaitnats.connect(servers=[self.natsd])js=self.nc.jetstream()tracer_provider=get_telemetry("NATS_SERVICE")ifnottracer_provider.initialized:awaitinit_telemetry(tracer_provider)set_global_textmap(B3MultiFormat())ncotel=NatsClientTelemetry(nc,"NATS_SERVICE",tracer_provider)awaitncotel.publish("testing.telemetry",request.name.encode())On Nats client (NO Jestream! ) subscriber you should add:nc=awaitnats.connect(servers=[self.natsd])js=self.nc.jetstream()tracer_provider=get_telemetry("NATS_SERVICE")ifnottracer_provider.initialized:awaitinit_telemetry(tracer_provider)set_global_textmap(B3MultiFormat())ncotel=NatsClientContextTelemetry(js,"NATS_SERVICE",tracer_provider)subscription=awaitncotel.subscribe(subject="testing.telemetry",queue="queue_nname",cb=handler,)On Nats client (NO Jestream! ) request you should add:nc=awaitnats.connect(servers=[self.natsd])js=self.nc.jetstream()tracer_provider=get_telemetry("NATS_SERVICE")ifnottracer_provider.initialized:awaitinit_telemetry(tracer_provider)set_global_textmap(B3MultiFormat())ncotel=NatsClientTelemetry(nc,"NATS_SERVICE",tracer_provider)response=awaitncotel.request("testing.telemetry",request.name.encode())And to handle responses on the other side, you can use the same pattern as in plain Nats client
subscriber, just adding themsg.respond()on the handler when done