|
| 1 | +# FlowTracker Kafka Exporter |
| 2 | + |
| 3 | +This is an addon for the [FlowTracker](https://github.com/spdeepak/flowtracker) library. It implements the `Exporter` interface to asynchronously push trace data to a **Apache Kafka** topic using the [confluent-kafka-go](https://github.com/confluentinc/confluent-kafka-go) library. |
| 4 | + |
| 5 | +## 📦 Installation |
| 6 | + |
| 7 | +Since this is an addon module, you must install it alongside the core library: |
| 8 | + |
| 9 | +```bash |
| 10 | +# Install Core |
| 11 | +go get github.com/spdeepak/flowtracker |
| 12 | + |
| 13 | +# Install Kafka Exporter |
| 14 | +go get github.com/spdeepak/flowtracker/kafka-exporter |
| 15 | +``` |
| 16 | + |
| 17 | +*Note: This library uses `confluent-kafka-go` which requires CGO to be enabled.* |
| 18 | + |
| 19 | +## 🚀 Usage |
| 20 | + |
| 21 | +You can configure the exporter in two ways: letting the library manage the connection, or bringing your own producer. |
| 22 | + |
| 23 | +### Option 1: Simple Configuration (Library creates Producer) |
| 24 | +Use this if you want FlowTracker to manage the Kafka connection lifecycle. |
| 25 | + |
| 26 | +```go |
| 27 | +package main |
| 28 | + |
| 29 | +import ( |
| 30 | + "github.com/confluentinc/confluent-kafka-go/kafka" |
| 31 | + "github.com/spdeepak/flowtracker" |
| 32 | + "github.com/spdeepak/flowtracker/kafka-exporter" |
| 33 | +) |
| 34 | + |
| 35 | +func main() { |
| 36 | + // 1. Configure the Exporter |
| 37 | + exporter, err := kafkaexporter.New(kafkaexporter.Config{ |
| 38 | + Topic: "microservice-traces", |
| 39 | + KafkaConfigMap: &kafka.ConfigMap{ |
| 40 | + "bootstrap.servers": "localhost:9092", |
| 41 | + "client.id": "flowtracker-exporter", |
| 42 | + "acks": "all", |
| 43 | + }, |
| 44 | + }) |
| 45 | + if err != nil { |
| 46 | + panic(err) |
| 47 | + } |
| 48 | + // Ensure outstanding messages are flushed on shutdown |
| 49 | + defer exporter.Close() |
| 50 | + |
| 51 | + // 2. Apply Middleware |
| 52 | + mw := flowtracker.NewMiddleware(flowtracker.WithExporter(exporter)) |
| 53 | + |
| 54 | + // ... start your server |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +### Option 2: Reuse Existing Producer |
| 59 | +Use this if your application already has a Kafka Producer instance and you want to reuse the connection. |
| 60 | + |
| 61 | +```go |
| 62 | +func main() { |
| 63 | + // Assuming 'myAppProducer' is your existing *kafka.Producer |
| 64 | + |
| 65 | + exporter, _ := kafkaexporter.New(kafkaexporter.Config{ |
| 66 | + Topic: "microservice-traces", |
| 67 | + Producer: myAppProducer, |
| 68 | + }) |
| 69 | + |
| 70 | + mw := flowtracker.NewMiddleware(flowtracker.WithExporter(exporter)) |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +## 📝 Data Format |
| 75 | + |
| 76 | +The exporter sends data to Kafka in the following format: |
| 77 | + |
| 78 | +* **Key:** The `trace_id` (String). This ensures all spans for a specific trace land on the same Kafka partition. |
| 79 | +* **Value:** JSON String of the `Trace` object. |
| 80 | + |
| 81 | +**Example Payload:** |
| 82 | +```json |
| 83 | +{ |
| 84 | + "trace_id": "trace-1700234-99", |
| 85 | + "spans": [ |
| 86 | + { |
| 87 | + "span_id": "101", |
| 88 | + "name": "GET /api/checkout", |
| 89 | + "duration_ms": 150 |
| 90 | + }, |
| 91 | + { |
| 92 | + "span_id": "202", |
| 93 | + "parent_id": "101", |
| 94 | + "name": "DB: Process Order", |
| 95 | + "duration_ms": 45 |
| 96 | + } |
| 97 | + ] |
| 98 | +} |
| 99 | +``` |
| 100 | + |
| 101 | +## 🔗 Examples |
| 102 | + |
| 103 | +You can find a complete, runnable example of how to use this exporter in the main repository: |
| 104 | + |
| 105 | +👉 **[Click here for the Kafka Example Code](../../examples/kafka)** |
| 106 | + |
| 107 | +*(Note: If the link above doesn't work, navigate to the `examples/kafka` directory in the repository root).* |
0 commit comments