{"id":13565409,"url":"https://github.com/opentracing-contrib/java-kafka-client","last_synced_at":"2025-05-16T08:04:31.293Z","repository":{"id":21727504,"uuid":"93475299","full_name":"opentracing-contrib/java-kafka-client","owner":"opentracing-contrib","description":"OpenTracing Instrumentation for Apache Kafka Client","archived":false,"fork":false,"pushed_at":"2023-09-05T22:04:04.000Z","size":357,"stargazers_count":127,"open_issues_count":16,"forks_count":64,"subscribers_count":9,"default_branch":"master","last_synced_at":"2025-04-12T04:48:03.516Z","etag":null,"topics":["java","kafka","kafka-client","kafka-streams","opentracing","spring-kafka"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/opentracing-contrib.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-06-06T04:15:45.000Z","updated_at":"2025-03-30T22:51:28.000Z","dependencies_parsed_at":"2024-08-01T13:22:13.837Z","dependency_job_id":"e3f97755-c0e0-4264-a5ea-b7c06845624a","html_url":"https://github.com/opentracing-contrib/java-kafka-client","commit_stats":null,"previous_names":[],"tags_count":72,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/opentracing-contrib%2Fjava-kafka-client","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/opentracing-contrib%2Fjava-kafka-client/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/opentracing-contrib%2Fjava-kafka-client/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/opentracing-contrib%2Fjava-kafka-client/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/opentracing-contrib","download_url":"https://codeload.github.com/opentracing-contrib/java-kafka-client/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254493378,"owners_count":22080126,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["java","kafka","kafka-client","kafka-streams","opentracing","spring-kafka"],"created_at":"2024-08-01T13:01:46.373Z","updated_at":"2025-05-16T08:04:26.285Z","avatar_url":"https://github.com/opentracing-contrib.png","language":"Java","readme":"[![Build Status][ci-img]][ci] [![Coverage Status][cov-img]][cov] [![Released Version][maven-img]][maven] [![Apache-2.0 license](https://img.shields.io/badge/license-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)\n\n\n# OpenTracing Apache Kafka Client Instrumentation\nOpenTracing instrumentation for Apache Kafka Client.    \nTwo solutions are provided:\n1. Based on decorated Producer and Consumer\n1. Based on Interceptors\n\n## Requirements\n\n- Java 8\n- Kafka 2.2.0\n\n## Installation\n\n### Kafka Client\n\npom.xml\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003eio.opentracing.contrib\u003c/groupId\u003e\n    \u003cartifactId\u003eopentracing-kafka-client\u003c/artifactId\u003e\n    \u003cversion\u003eVERSION\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n### Kafka Streams\n\npom.xml\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003eio.opentracing.contrib\u003c/groupId\u003e\n    \u003cartifactId\u003eopentracing-kafka-streams\u003c/artifactId\u003e\n    \u003cversion\u003eVERSION\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n### Spring Kafka\n\npom.xml\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003eio.opentracing.contrib\u003c/groupId\u003e\n    \u003cartifactId\u003eopentracing-kafka-spring\u003c/artifactId\u003e\n    \u003cversion\u003eVERSION\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n## Usage\n\n```java\n\n// Instantiate tracer\nTracer tracer = ...\n\n// Optionally register tracer with GlobalTracer\nGlobalTracer.register(tracer);\n```\n\n### Kafka Client\n\n#### Decorators based solution\n\n```java\n\n// Instantiate KafkaProducer\nKafkaProducer\u003cInteger, String\u003e producer = new KafkaProducer\u003c\u003e(senderProps);\n\n//Decorate KafkaProducer with TracingKafkaProducer\nTracingKafkaProducer\u003cInteger, String\u003e tracingProducer = new TracingKafkaProducer\u003c\u003e(producer, \n        tracer);\n\n// Send\ntracingProducer.send(...);\n\n// Instantiate KafkaConsumer\nKafkaConsumer\u003cInteger, String\u003e consumer = new KafkaConsumer\u003c\u003e(consumerProps);\n\n// Decorate KafkaConsumer with TracingKafkaConsumer\nTracingKafkaConsumer\u003cInteger, String\u003e tracingConsumer = new TracingKafkaConsumer\u003c\u003e(consumer, \n        tracer);\n\n//Subscribe\ntracingConsumer.subscribe(Collections.singletonList(\"messages\"));\n\n// Get records\nConsumerRecords\u003cInteger, String\u003e records = tracingConsumer.poll(1000);\n\n// To retrieve SpanContext from polled record (Consumer side)\nConsumerRecord\u003cInteger, String\u003e record = ...\nSpanContext spanContext = TracingKafkaUtils.extractSpanContext(record.headers(), tracer);\n\n```\n\n##### Custom Span Names for Decorators based solution\nThe decorator-based solution includes support for custom span names by passing in a BiFunction object as an additional\nargument to the TracingKafkaConsumer or TracingKafkaProducer constructors, either one of the provided BiFunctions or\nyour own custom one.\n\n```java\n// Create BiFunction for the KafkaProducer that operates on\n// (String operationName, ProducerRecord consumerRecord) and\n// returns a String to be used as the name\nBiFunction\u003cString, ProducerRecord, String\u003e producerSpanNameProvider =\n    (operationName, producerRecord) -\u003e \"CUSTOM_PRODUCER_NAME\";\n\n// Instantiate KafkaProducer\nKafkaProducer\u003cInteger, String\u003e producer = new KafkaProducer\u003c\u003e(senderProps);\n\n//Decorate KafkaProducer with TracingKafkaProducer\nTracingKafkaProducer\u003cInteger, String\u003e tracingProducer = new TracingKafkaProducer\u003c\u003e(producer, \n        tracer,\n        producerSpanNameProvider);\n// Spans created by the tracingProducer will now have \"CUSTOM_PRODUCER_NAME\" as the span name.\n\n\n// Create BiFunction for the KafkaConsumer that operates on\n// (String operationName, ConsumerRecord consumerRecord) and\n// returns a String to be used as the name\nBiFunction\u003cString, ConsumerRecord, String\u003e consumerSpanNameProvider =\n    (operationName, consumerRecord) -\u003e operationName.toUpperCase();\n// Instantiate KafkaConsumer\nKafkaConsumer\u003cInteger, String\u003e consumer = new KafkaConsumer\u003c\u003e(consumerProps);\n// Decorate KafkaConsumer with TracingKafkaConsumer, passing in the consumerSpanNameProvider BiFunction\nTracingKafkaConsumer\u003cInteger, String\u003e tracingConsumer = new TracingKafkaConsumer\u003c\u003e(consumer, \n        tracer,\n        consumerSpanNameProvider);\n// Spans created by the tracingConsumer will now have the capitalized operation name as the span name.\n// \"receive\" -\u003e \"RECEIVE\"\n```  \n\n\n#### Interceptors based solution\n```java\n// Register tracer with GlobalTracer:\nGlobalTracer.register(tracer);\n\n// Add TracingProducerInterceptor to sender properties:\nsenderProps.put(ProducerConfig.INTERCEPTOR_CLASSES_CONFIG, \n          TracingProducerInterceptor.class.getName());\n\n// Instantiate KafkaProducer\nKafkaProducer\u003cInteger, String\u003e producer = new KafkaProducer\u003c\u003e(senderProps);\n\n// Send\nproducer.send(...);\n\n// Add TracingConsumerInterceptor to consumer properties:\nconsumerProps.put(ConsumerConfig.INTERCEPTOR_CLASSES_CONFIG,\n          TracingConsumerInterceptor.class.getName());\n\n// Instantiate KafkaConsumer\nKafkaConsumer\u003cInteger, String\u003e consumer = new KafkaConsumer\u003c\u003e(consumerProps);\n\n//Subscribe\nconsumer.subscribe(Collections.singletonList(\"messages\"));\n\n// Get records\nConsumerRecords\u003cInteger, String\u003e records = consumer.poll(1000);\n\n// To retrieve SpanContext from polled record (Consumer side)\nConsumerRecord\u003cInteger, String\u003e record = ...\nSpanContext spanContext = TracingKafkaUtils.extractSpanContext(record.headers(), tracer);\n\n```\n\n\n### Kafka Streams\n\n```java\n\n// Instantiate TracingKafkaClientSupplier\nKafkaClientSupplier supplier = new TracingKafkaClientSupplier(tracer);\n\n// Provide supplier to KafkaStreams\nKafkaStreams streams = new KafkaStreams(builder.build(), new StreamsConfig(config), supplier);\nstreams.start();\n\n```\n\n### Spring Kafka\n\n```java\n\n// Declare Tracer bean\n@Bean\npublic Tracer tracer() {\n  return ...\n}\n\n\n// Decorate ConsumerFactory with TracingConsumerFactory\n@Bean\npublic ConsumerFactory\u003cInteger, String\u003e consumerFactory() {\n  return new TracingConsumerFactory\u003c\u003e(new DefaultKafkaConsumerFactory\u003c\u003e(consumerProps()), tracer());\n}\n\n// Decorate ProducerFactory with TracingProducerFactory\n@Bean\npublic ProducerFactory\u003cInteger, String\u003e producerFactory() {\n  return new TracingProducerFactory\u003c\u003e(new DefaultKafkaProducerFactory\u003c\u003e(producerProps()), tracer());\n}\n\n// Use decorated ProducerFactory in KafkaTemplate \n@Bean\npublic KafkaTemplate\u003cInteger, String\u003e kafkaTemplate() {\n  return new KafkaTemplate\u003c\u003e(producerFactory());\n}\n\n// Use an aspect to decorate @KafkaListeners\n@Bean\npublic TracingKafkaAspect tracingKafkaAspect() {\n  return new TracingKafkaAspect(tracer());\n}\n```\n\n##### Custom Span Names for Spring Kafka\nThe Spring Kafka factory implementations include support for custom span names by passing in a BiFunction object as an additional\nargument to the TracingConsumerFactory or TracingProducerFactory constructors, either one of the provided BiFunctions or\nyour own custom one.\n\n```java\n// Create BiFunction for the KafkaProducerFactory that operates on\n// (String operationName, ProducerRecord consumerRecord) and\n// returns a String to be used as the name\nBiFunction\u003cString, ProducerRecord, String\u003e producerSpanNameProvider =\n    (operationName, producerRecord) -\u003e \"CUSTOM_PRODUCER_NAME\";\n\n// Decorate ProducerFactory with TracingProducerFactory\n@Bean\npublic ProducerFactory\u003cInteger, String\u003e producerFactory() {\n  return new TracingProducerFactory\u003c\u003e(new DefaultKafkaProducerFactory\u003c\u003e(producerProps()), tracer());\n}\n// Spans created by the tracingProducer will now have \"CUSTOM_PRODUCER_NAME\" as the span name.\n\n\n// Create BiFunction for the KafkaConsumerFactory that operates on\n// (String operationName, ConsumerRecord consumerRecord) and\n// returns a String to be used as the name\nBiFunction\u003cString, ConsumerRecord, String\u003e consumerSpanNameProvider =\n    (operationName, consumerRecord) -\u003e operationName.toUpperCase();\n\n// Decorate ConsumerFactory with TracingConsumerFactory\n@Bean\npublic ConsumerFactory\u003cInteger, String\u003e consumerFactory() {\n  return new TracingConsumerFactory\u003c\u003e(new DefaultKafkaConsumerFactory\u003c\u003e(consumerProps()), tracer());\n}\n// Consumers produced by the traced consumerFactory\n```\n\n#### Pre-made Span Name Providers\n\nThe following BiFunctions are already included in the ClientSpanNameProvider class, with `CONSUMER_OPERATION_NAME` and `PRODUCER_OPERATION_NAME` being the default should no\nspanNameProvider be provided:\n\n- `CONSUMER_OPERATION_NAME` and `PRODUCER_OPERATION_NAME` : Returns the `operationName` as the span name (\"receive\" for Consumer, \"send\" for producer).\n- `CONSUMER_PREFIXED_OPERATION_NAME(String prefix)` and `PRODUCER_PREFIXED_OPERATION_NAME(String prefix)` : Returns a String concatenation of `prefix` and `operatioName`.\n- `CONSUMER_TOPIC` and `PRODUCER_TOPIC` : Returns the Kafka topic name that the record was pushed to/pulled from (`record.topic()`).\n- `PREFIXED_CONSUMER_TOPIC(String prefix)` and `PREFIXED_PRODUCER_TOPIC(String prefix)` : Returns a String concatenation of `prefix` and the Kafka topic name (`record.topic()`).\n- `CONSUMER_OPERATION_NAME_TOPIC` and `PRODUCER_OPERATION_NAME_TOPIC` : Returns \"`operationName` - `record.topic()`\".\n- `CONSUMER_PREFIXED_OPERATION_NAME_TOPIC(String prefix)` and `PRODUCER_PREFIXED_OPERATION_NAME_TOPIC(String prefix)` : Returns a String concatenation of `prefix` and \"`operationName` - `record.topic()`\".\n\n## License\n\n[Apache 2.0 License](./LICENSE).\n\n[ci-img]: https://travis-ci.org/opentracing-contrib/java-kafka-client.svg?branch=master\n[ci]: https://travis-ci.org/opentracing-contrib/java-kafka-client\n[cov-img]: https://coveralls.io/repos/github/opentracing-contrib/java-kafka-client/badge.svg?branch=master\n[cov]: https://coveralls.io/github/opentracing-contrib/java-kafka-client?branch=master\n[maven-img]: https://img.shields.io/maven-central/v/io.opentracing.contrib/opentracing-kafka-client.svg\n[maven]: http://search.maven.org/#search%7Cga%7C1%7Copentracing-kafka-client\n","funding_links":[],"categories":["Java"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fopentracing-contrib%2Fjava-kafka-client","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fopentracing-contrib%2Fjava-kafka-client","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fopentracing-contrib%2Fjava-kafka-client/lists"}