-
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 1 commit
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
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" | ||
|
|
||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| # | ||
| # Copyright The OpenZipkin Authors | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # | ||
|
|
||
|
|
||
| # Use latest from https://hub.docker.com/r/apachepulsar/pulsar/tags | ||
| ARG pulsar_version=4.0.2 | ||
|
|
||
| # We copy files from the context into a scratch container first to avoid a problem where docker and | ||
| # docker compose don't share layer hashes https://github.com/docker/compose/issues/883 normally. | ||
| # COPY --from= works around the issue. | ||
| FROM scratch as scratch | ||
|
|
||
| WORKDIR /docker-bin | ||
| COPY build-bin/docker/docker-healthcheck /docker-bin/ | ||
|
|
||
| ARG pulsar_version | ||
|
|
||
| FROM apachepulsar/pulsar:${pulsar_version} as zipkin-pulsar | ||
| LABEL pulsar-version=$pulsar_version | ||
| LABEL org.opencontainers.image.description="Apache Pulsar on Alpine Linux" | ||
|
|
||
| # Add HEALTHCHECK and ENTRYPOINT scripts into the default search path | ||
| COPY --from=scratch /docker-bin/* /usr/local/bin/ | ||
| # We use start period of 30s to avoid marking the container unhealthy on slow or contended CI hosts | ||
| HEALTHCHECK --interval=1s --start-period=30s --timeout=5s CMD ["docker-healthcheck"] | ||
|
|
||
| # Usually, we read env set from pid 1 to get docker-healthcheck parameters. | ||
| # However, pulsar-server has to start as root even if permissions are dropped | ||
| # later. So, we expose it in the Dockerfile instead. | ||
| ENV HEALTHCHECK_PORT=8080 | ||
| ENV HEALTHCHECK_KIND=tcp | ||
| EXPOSE 8080 6650 | ||
| CMD ["bin/pulsar", "standalone"] |
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,11 @@ | ||
| ## zipkin-pulsar Docker image | ||
|
|
||
| The `zipkin-pulsar` testing image runs Pulsar for [Pulsar collector](../../../zipkin-collector/pulsar) | ||
| integration. | ||
|
|
||
| To build `openzipkin/zipkin-pulsar:test`, from the top-level of the repository, run: | ||
| ```bash | ||
| $ DOCKER_FILE=docker/test-images/zipkin-pulsar/Dockerfile build-bin/docker/docker_build openzipkin/zipkin-pulsar:test | ||
| ``` | ||
|
|
||
| You can use the env variable `JAVA_OPTS` to change settings such as heap size for Pulsar. |
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,51 @@ | ||
| # 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. | ||
|
|
||
| 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> |
79 changes: 79 additions & 0 deletions
79
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,79 @@ | ||
| /* | ||
| * 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.TimeUnit; | ||
| import java.util.concurrent.atomic.AtomicReference; | ||
|
|
||
| public 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; | ||
| } | ||
|
|
||
| public void init() { | ||
reta marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (result == null) { | ||
| synchronized (this) { | ||
| if (result == null) { | ||
| result = subscribe(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private PulsarClient subscribe() { | ||
| PulsarClient client; | ||
| try { | ||
| client = PulsarClient.builder() | ||
| .loadConf(clientProps) | ||
| .operationTimeout(6, TimeUnit.SECONDS) | ||
| .connectionTimeout(12, TimeUnit.SECONDS) | ||
| .build(); | ||
| } catch (Exception e) { | ||
| throw new RuntimeException("Pulsar client create failed" + e.getMessage(), e); | ||
reta marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
reta marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| 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 { | ||
reta marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| client.close(); | ||
| } catch (PulsarClientException ex) { | ||
| // Nobody cares me. | ||
| } | ||
| throw new RuntimeException("Pulsar unable to subscribe the topic(" + topic + "), please check the pulsar service.", e); | ||
reta marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| } | ||
|
|
||
| public void close() throws PulsarClientException { | ||
reta marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| PulsarClient maybe = result; | ||
| if (maybe != null) result.close(); | ||
reta marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
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.