-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Add Pulsar collector #3788
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
codefromthecrypt
merged 6 commits into
openzipkin:master
from
CodePrometheus:feat-pulsar-collector
Feb 4, 2025
Merged
Add Pulsar collector #3788
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
15983ff
Add Pulsar collector
CodePrometheus d313a63
Fix pulsar collector in round1
CodePrometheus de08976
Fix pulsar collector in round2
CodePrometheus a111b51
Remove docker related parts and polish the code
CodePrometheus 7785798
Merge remote-tracking branch 'origin/master' into feat-pulsar-collector
CodePrometheus 5f70994
Pop compose changes, polish the code and modify docker image
CodePrometheus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| # | ||
| # Copyright The OpenZipkin Authors | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # | ||
|
|
||
| # This file uses the version 2 docker compose file format, described here: | ||
| # https://docs.docker.com/compose/compose-file/#version-2 | ||
| # | ||
| # It extends the default configuration from docker-compose.yml to add a test | ||
| # pulsar server, which is used as a span transport. | ||
|
|
||
| version: '2.4' | ||
|
|
||
| services: | ||
| pulsar: | ||
| image: ghcr.io/openzipkin/zipkin-pulsar:${TAG:-latest} | ||
| container_name: pulsar | ||
| ports: # expose the pulsar port so apps can publish spans. | ||
| - "6650:6650" | ||
| # - "8080:8080" # uncomment to expose the pulsar http port. | ||
|
|
||
| zipkin: | ||
| extends: | ||
| file: docker-compose.yml | ||
| service: zipkin | ||
| # slim doesn't include Pulsar support, so switch to the larger image | ||
| image: ghcr.io/openzipkin/zipkin:${TAG:-latest} | ||
| environment: | ||
| - PULSAR_SERVICE_URL=pulsar://pulsar:6650 | ||
| depends_on: | ||
| pulsar: | ||
| condition: service_healthy |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| # collector-pulsar | ||
|
|
||
| ## PulsarCollector | ||
|
|
||
| This collector is implemented as a Pulsar consumer supporting Pulsar brokers running | ||
| version 4.x or later, and the default subscription type is `Shared`, in Shared subscription type, | ||
| multiple consumers can attach to the same subscription and messages are delivered | ||
| in a round-robin distribution across consumers. | ||
|
|
||
| This collector is implemented as a Pulsar consumer supporting Pulsar brokers running version 4.x or later. | ||
| The default `subscriptionType` is `Shared`, which allows multiple consumers to attach to the same subscription, | ||
| with messages delivered in a round-robin distribution across consumers, the default `subscriptionInitialPosition` | ||
| is `Earliest`, you can modify the consumer settings as needed through the `consumerProps` parameter. | ||
| Also, the client settings can also be modified through the `clientProps` parameter. | ||
|
|
||
| For information about running this collector as a module in Zipkin server, see | ||
| the [Zipkin Server README](../../zipkin-server/README.md#pulsar-collector). | ||
|
|
||
| When using this collector as a library outside of Zipkin server, | ||
| [zipkin2.collector.pulsar.PulsarCollector.Builder](src/main/java/zipkin2/collector/pulsar/PulsarCollector.java) | ||
| includes defaults that will operate against a Pulsar topic name `zipkin`. | ||
|
|
||
| ## Encoding spans into Pulsar messages | ||
|
|
||
| The message's binary data includes a list of spans. Supported encodings | ||
| are the same as the http [POST /spans](https://zipkin.io/zipkin-api/#/paths/%252Fspans) body. | ||
|
|
||
| ### Json | ||
|
|
||
| The message's binary data is a list of spans in json. The first character must be '[' (decimal 91). | ||
|
|
||
| `Codec.JSON.writeSpans(spans)` performs the correct json encoding. | ||
|
|
||
| ### Thrift | ||
|
|
||
| The message's binary data includes a list header followed by N spans serialized in TBinaryProtocol | ||
|
|
||
| `Codec.THRIFT.writeSpans(spans)` encodes spans in the following fashion: | ||
|
|
||
| ``` | ||
| write_byte(12) // type of the list elements: 12 == struct | ||
| write_i32(count) // count of spans that will follow | ||
| for (int i = 0; i < count; i++) { | ||
| writeTBinaryProtocol(spans(i)) | ||
| } | ||
| ``` | ||
|
|
||
| ### Legacy encoding | ||
|
|
||
| Older versions of zipkin accepted a single span per message, as opposed | ||
| to a list per message. This practice is deprecated, but still supported. | ||
|
|
||
| ## Logging | ||
|
|
||
| Zipkin by default suppresses all logging output from Pulsar client operations as they can get quite verbose. Start | ||
| Zipkin | ||
| with `--logging.level.org.apache.pulsar=INFO` or similar to override this during troubleshooting for example. |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <!-- | ||
|
|
||
| Copyright The OpenZipkin Authors | ||
| SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| --> | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
|
|
||
| <parent> | ||
| <groupId>io.zipkin.zipkin2</groupId> | ||
| <artifactId>zipkin-collector-parent</artifactId> | ||
| <version>3.4.5-SNAPSHOT</version> | ||
| </parent> | ||
|
|
||
| <artifactId>zipkin-collector-pulsar</artifactId> | ||
| <name>Collector: Pulsar</name> | ||
|
|
||
| <properties> | ||
| <main.basedir>${project.basedir}/../..</main.basedir> | ||
| <pulsar-client.version>4.0.2</pulsar-client.version> | ||
| </properties> | ||
|
|
||
| <dependencies> | ||
| <dependency> | ||
| <groupId>${project.groupId}</groupId> | ||
| <artifactId>zipkin-collector</artifactId> | ||
| <version>${project.version}</version> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>org.apache.pulsar</groupId> | ||
| <artifactId>pulsar-client</artifactId> | ||
| <version>${pulsar-client.version}</version> | ||
| </dependency> | ||
| </dependencies> | ||
| </project> |
80 changes: 80 additions & 0 deletions
80
zipkin-collector/pulsar/src/main/java/zipkin2/collector/pulsar/LazyPulsarInit.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| /* | ||
| * Copyright The OpenZipkin Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| package zipkin2.collector.pulsar; | ||
|
|
||
| import org.apache.pulsar.client.api.PulsarClient; | ||
| import org.apache.pulsar.client.api.PulsarClientException; | ||
| import zipkin2.CheckResult; | ||
| import zipkin2.collector.Collector; | ||
| import zipkin2.collector.CollectorMetrics; | ||
|
|
||
| import java.util.Map; | ||
| import java.util.concurrent.atomic.AtomicReference; | ||
|
|
||
| class LazyPulsarInit { | ||
|
|
||
| private final Collector collector; | ||
| private final CollectorMetrics metrics; | ||
| private final String topic; | ||
| private final int concurrency; | ||
| private final Map<String, Object> clientProps, consumerProps; | ||
| public volatile PulsarClient result; | ||
| final AtomicReference<CheckResult> failure = new AtomicReference<>(); | ||
|
|
||
| LazyPulsarInit(PulsarCollector.Builder builder) { | ||
| this.collector = builder.delegate.build(); | ||
| this.metrics = builder.metrics; | ||
| this.topic = builder.topic; | ||
| this.concurrency = builder.concurrency; | ||
| this.clientProps = builder.clientProps; | ||
| this.consumerProps = builder.consumerProps; | ||
| } | ||
|
|
||
| void init() { | ||
| if (result == null) { | ||
| synchronized (this) { | ||
| if (result == null) { | ||
| result = subscribe(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private PulsarClient subscribe() { | ||
| PulsarClient client; | ||
| try { | ||
| client = PulsarClient.builder() | ||
| .loadConf(clientProps) | ||
| .build(); | ||
| } catch (Exception e) { | ||
| failure.set(CheckResult.failed(e)); | ||
| throw new RuntimeException("Pulsar client creation failed. " + e.getMessage(), e); | ||
| } | ||
|
|
||
| try { | ||
| for (int i = 0; i < concurrency; i++) { | ||
| PulsarSpanConsumer consumer = new PulsarSpanConsumer(topic, consumerProps, client, collector, metrics); | ||
| consumer.startConsumer(); | ||
| } | ||
| return client; | ||
| } catch (Exception e) { | ||
| try { | ||
| client.close(); | ||
| } catch (PulsarClientException ex) { | ||
| // Nobody cares me. | ||
| } | ||
| failure.set(CheckResult.failed(e)); | ||
| throw new RuntimeException("Pulsar Client is unable to subscribe to the topic(" + topic + "), please check the service.", e); | ||
| } | ||
| } | ||
|
|
||
| void close() throws PulsarClientException { | ||
| PulsarClient maybe = result; | ||
| if (maybe != null) { | ||
| result.close(); | ||
| result = null; | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.