├── .github └── workflows │ └── otel-build.yml ├── .gitignore ├── README.md ├── otel-auto-instrumentation-survey ├── .gitignore ├── .idea │ ├── .gitignore │ ├── encodings.xml │ ├── misc.xml │ ├── sqldialects.xml │ ├── uiDesigner.xml │ └── vcs.xml ├── README.md ├── opentelemetry-javaagent.jar ├── pom.xml └── src │ └── main │ ├── java │ └── org │ │ └── example │ │ ├── Main.java │ │ ├── apache_http_client │ │ └── ApacheHttpExecute.java │ │ ├── dubbo │ │ ├── DubboClientApplication.java │ │ ├── DubboServerApplication.java │ │ ├── GreetingService.java │ │ └── GreetingServiceImpl.java │ │ ├── grpc │ │ ├── Example.java │ │ ├── TestClient.java │ │ ├── TestServer.java │ │ ├── TestServiceGrpc.java │ │ └── example.proto │ │ ├── jedis │ │ └── JedisExecute.java │ │ ├── jetty_servlet │ │ ├── HelloServlet.java │ │ └── JettyServer.java │ │ ├── kafka_consumer │ │ ├── KafkaConsumerExecute.java │ │ └── KafkaProducerExecute.java │ │ ├── mongo │ │ └── MongoDB.java │ │ └── mysql │ │ └── MySQLJDBCExample.java │ └── resources │ └── dubbo.properties ├── otel-compressor ├── .gitignore ├── Dockerfile ├── builder-config.yaml ├── config │ ├── compressor-agent.yml │ └── compressor-gateway.yml ├── docker-compose.yml ├── go.work ├── go.work.sum ├── otelcol-dev │ ├── components.go │ ├── components_test.go │ ├── go.mod │ ├── go.sum │ ├── main.go │ ├── main_others.go │ ├── main_windows.go │ └── test.txt ├── pdata │ ├── Makefile │ ├── README.md │ ├── go.mod │ ├── go.sum │ ├── internal │ │ ├── .gitignore │ │ ├── cmd │ │ │ └── pdatagen │ │ │ │ ├── internal │ │ │ │ ├── base_fields.go │ │ │ │ ├── base_slices.go │ │ │ │ ├── base_structs.go │ │ │ │ ├── packages.go │ │ │ │ ├── pcommon_package.go │ │ │ │ ├── plog_package.go │ │ │ │ ├── plogotlp_package.go │ │ │ │ ├── pmetric_package.go │ │ │ │ ├── pmetricotlp_package.go │ │ │ │ ├── primitive_slice_structs.go │ │ │ │ ├── ptrace_package.go │ │ │ │ └── ptraceotlp_package.go │ │ │ │ └── main.go │ │ ├── data │ │ │ ├── bytesid.go │ │ │ ├── package_test.go │ │ │ ├── protogen │ │ │ │ ├── collector │ │ │ │ │ ├── logs │ │ │ │ │ │ └── v1 │ │ │ │ │ │ │ └── logs_service.pb.go │ │ │ │ │ ├── metrics │ │ │ │ │ │ └── v1 │ │ │ │ │ │ │ └── metrics_service.pb.go │ │ │ │ │ └── trace │ │ │ │ │ │ └── v1 │ │ │ │ │ │ └── trace_service.pb.go │ │ │ │ ├── common │ │ │ │ │ └── v1 │ │ │ │ │ │ └── common.pb.go │ │ │ │ ├── logs │ │ │ │ │ └── v1 │ │ │ │ │ │ └── logs.pb.go │ │ │ │ ├── metrics │ │ │ │ │ └── v1 │ │ │ │ │ │ └── metrics.pb.go │ │ │ │ ├── resource │ │ │ │ │ └── v1 │ │ │ │ │ │ └── resource.pb.go │ │ │ │ └── trace │ │ │ │ │ └── v1 │ │ │ │ │ └── trace.pb.go │ │ │ ├── spanid.go │ │ │ ├── spanid_test.go │ │ │ ├── traceid.go │ │ │ └── traceid_test.go │ │ ├── generated_wrapper_byteslice.go │ │ ├── generated_wrapper_float64slice.go │ │ ├── generated_wrapper_instrumentationscope.go │ │ ├── generated_wrapper_resource.go │ │ ├── generated_wrapper_uint64slice.go │ │ ├── json │ │ │ ├── attribute.go │ │ │ ├── attribute_test.go │ │ │ ├── enum.go │ │ │ ├── enum_test.go │ │ │ ├── json.go │ │ │ ├── number.go │ │ │ ├── number_test.go │ │ │ ├── package_test.go │ │ │ ├── resource.go │ │ │ ├── resource_test.go │ │ │ ├── scope.go │ │ │ └── scope_test.go │ │ ├── otlp │ │ │ ├── logs.go │ │ │ ├── logs_test.go │ │ │ ├── metrics.go │ │ │ ├── metrics_test.go │ │ │ ├── package_test.go │ │ │ ├── traces.go │ │ │ └── traces_test.go │ │ ├── state.go │ │ ├── wrapper_logs.go │ │ ├── wrapper_map.go │ │ ├── wrapper_metrics.go │ │ ├── wrapper_slice.go │ │ ├── wrapper_traces.go │ │ ├── wrapper_tracestate.go │ │ └── wrapper_value.go │ ├── pcommon │ │ ├── generated_byteslice.go │ │ ├── generated_byteslice_test.go │ │ ├── generated_float64slice.go │ │ ├── generated_float64slice_test.go │ │ ├── generated_instrumentationscope.go │ │ ├── generated_instrumentationscope_test.go │ │ ├── generated_resource.go │ │ ├── generated_resource_test.go │ │ ├── generated_uint64slice.go │ │ ├── generated_uint64slice_test.go │ │ ├── map.go │ │ ├── map_test.go │ │ ├── package_test.go │ │ ├── slice.go │ │ ├── slice_test.go │ │ ├── spanid.go │ │ ├── spanid_test.go │ │ ├── timestamp.go │ │ ├── timestamp_test.go │ │ ├── trace_state.go │ │ ├── trace_state_test.go │ │ ├── traceid.go │ │ ├── traceid_test.go │ │ ├── value.go │ │ └── value_test.go │ ├── plog │ │ ├── encoding.go │ │ ├── generated_logrecord.go │ │ ├── generated_logrecord_test.go │ │ ├── generated_logrecordslice.go │ │ ├── generated_logrecordslice_test.go │ │ ├── generated_resourcelogs.go │ │ ├── generated_resourcelogs_test.go │ │ ├── generated_resourcelogsslice.go │ │ ├── generated_resourcelogsslice_test.go │ │ ├── generated_scopelogs.go │ │ ├── generated_scopelogs_test.go │ │ ├── generated_scopelogsslice.go │ │ ├── generated_scopelogsslice_test.go │ │ ├── json.go │ │ ├── json_test.go │ │ ├── log_record_flags.go │ │ ├── log_record_flags_test.go │ │ ├── logs.go │ │ ├── logs_test.go │ │ ├── package_test.go │ │ ├── pb.go │ │ ├── pb_test.go │ │ ├── plogotlp │ │ │ ├── generated_exportpartialsuccess.go │ │ │ ├── generated_exportpartialsuccess_test.go │ │ │ ├── grpc.go │ │ │ ├── grpc_test.go │ │ │ ├── package_test.go │ │ │ ├── request.go │ │ │ ├── request_test.go │ │ │ ├── response.go │ │ │ └── response_test.go │ │ ├── severity_number.go │ │ └── severity_number_test.go │ ├── pmetric │ │ ├── aggregation_temporality.go │ │ ├── aggregation_temporality_test.go │ │ ├── encoding.go │ │ ├── exemplar_value_type.go │ │ ├── exemplar_value_type_test.go │ │ ├── generated_exemplar.go │ │ ├── generated_exemplar_test.go │ │ ├── generated_exemplarslice.go │ │ ├── generated_exemplarslice_test.go │ │ ├── generated_exponentialhistogram.go │ │ ├── generated_exponentialhistogram_test.go │ │ ├── generated_exponentialhistogramdatapoint.go │ │ ├── generated_exponentialhistogramdatapoint_test.go │ │ ├── generated_exponentialhistogramdatapointbuckets.go │ │ ├── generated_exponentialhistogramdatapointbuckets_test.go │ │ ├── generated_exponentialhistogramdatapointslice.go │ │ ├── generated_exponentialhistogramdatapointslice_test.go │ │ ├── generated_gauge.go │ │ ├── generated_gauge_test.go │ │ ├── generated_histogram.go │ │ ├── generated_histogram_test.go │ │ ├── generated_histogramdatapoint.go │ │ ├── generated_histogramdatapoint_test.go │ │ ├── generated_histogramdatapointslice.go │ │ ├── generated_histogramdatapointslice_test.go │ │ ├── generated_metric.go │ │ ├── generated_metric_test.go │ │ ├── generated_metricslice.go │ │ ├── generated_metricslice_test.go │ │ ├── generated_numberdatapoint.go │ │ ├── generated_numberdatapoint_test.go │ │ ├── generated_numberdatapointslice.go │ │ ├── generated_numberdatapointslice_test.go │ │ ├── generated_resourcemetrics.go │ │ ├── generated_resourcemetrics_test.go │ │ ├── generated_resourcemetricsslice.go │ │ ├── generated_resourcemetricsslice_test.go │ │ ├── generated_scopemetrics.go │ │ ├── generated_scopemetrics_test.go │ │ ├── generated_scopemetricsslice.go │ │ ├── generated_scopemetricsslice_test.go │ │ ├── generated_sum.go │ │ ├── generated_sum_test.go │ │ ├── generated_summary.go │ │ ├── generated_summary_test.go │ │ ├── generated_summarydatapoint.go │ │ ├── generated_summarydatapoint_test.go │ │ ├── generated_summarydatapointslice.go │ │ ├── generated_summarydatapointslice_test.go │ │ ├── generated_summarydatapointvalueatquantile.go │ │ ├── generated_summarydatapointvalueatquantile_test.go │ │ ├── generated_summarydatapointvalueatquantileslice.go │ │ ├── generated_summarydatapointvalueatquantileslice_test.go │ │ ├── json.go │ │ ├── json_test.go │ │ ├── metric_data_point_flags.go │ │ ├── metric_data_point_flags_test.go │ │ ├── metric_type.go │ │ ├── metric_type_test.go │ │ ├── metrics.go │ │ ├── metrics_test.go │ │ ├── number_data_point_value_type.go │ │ ├── number_data_point_value_type_test.go │ │ ├── package_test.go │ │ ├── pb.go │ │ ├── pb_test.go │ │ └── pmetricotlp │ │ │ ├── generated_exportpartialsuccess.go │ │ │ ├── generated_exportpartialsuccess_test.go │ │ │ ├── grpc.go │ │ │ ├── grpc_test.go │ │ │ ├── package_test.go │ │ │ ├── request.go │ │ │ ├── request_test.go │ │ │ ├── response.go │ │ │ └── response_test.go │ └── ptrace │ │ ├── encoding.go │ │ ├── generated_resourcespans.go │ │ ├── generated_resourcespans_test.go │ │ ├── generated_resourcespansslice.go │ │ ├── generated_resourcespansslice_test.go │ │ ├── generated_scopespans.go │ │ ├── generated_scopespans_test.go │ │ ├── generated_scopespansslice.go │ │ ├── generated_scopespansslice_test.go │ │ ├── generated_span.go │ │ ├── generated_span_test.go │ │ ├── generated_spanevent.go │ │ ├── generated_spanevent_test.go │ │ ├── generated_spaneventslice.go │ │ ├── generated_spaneventslice_test.go │ │ ├── generated_spanlink.go │ │ ├── generated_spanlink_test.go │ │ ├── generated_spanlinkslice.go │ │ ├── generated_spanlinkslice_test.go │ │ ├── generated_spanslice.go │ │ ├── generated_spanslice_test.go │ │ ├── generated_status.go │ │ ├── generated_status_test.go │ │ ├── json.go │ │ ├── json_test.go │ │ ├── package_test.go │ │ ├── pb.go │ │ ├── pb_test.go │ │ ├── ptraceotlp │ │ ├── generated_exportpartialsuccess.go │ │ ├── generated_exportpartialsuccess_test.go │ │ ├── grpc.go │ │ ├── grpc_test.go │ │ ├── package_test.go │ │ ├── request.go │ │ ├── request_test.go │ │ ├── response.go │ │ └── response_test.go │ │ ├── span_kind.go │ │ ├── span_kind_test.go │ │ ├── status_code.go │ │ ├── status_code_test.go │ │ ├── traces.go │ │ └── traces_test.go ├── prefix-compressed-exporter │ ├── config.go │ ├── exporter.go │ ├── factory.go │ ├── go.mod │ ├── go.sum │ └── internal │ │ └── metadata │ │ └── status.go ├── prefix-compressed-receiver │ ├── config.go │ ├── encoder.go │ ├── factory.go │ ├── go.mod │ ├── go.sum │ ├── httphandler.go │ ├── internal │ │ ├── errors │ │ │ └── errors.go │ │ ├── localhostgate │ │ │ └── feature-gate.go │ │ ├── logs │ │ │ └── otlp.go │ │ ├── metadata │ │ │ └── status.go │ │ ├── metrics │ │ │ └── otlp.go │ │ ├── sharedcomponent │ │ │ └── sharedcomponent.go │ │ └── trace │ │ │ └── otlp.go │ └── receiver.go ├── readme.md └── wrk │ ├── index.mjs │ ├── package.json │ └── readme.md ├── static-compressor ├── go.mod ├── main.go ├── readme.md └── reconstruct.go └── train-ticket-workload ├── .gitignore ├── query_scaffold ├── .gitignore ├── README.md ├── __init__.py ├── atomic_queries.py ├── normal_request_manager.py ├── queries.py ├── query_admin_basic_config.py ├── query_admin_basic_price.py ├── query_advanced_ticket.py ├── query_and_cancel.py ├── query_and_collect_ticket.py ├── query_and_enter_station.py ├── query_and_preserve.py ├── query_and_put_consign.py ├── query_and_rebook.py ├── query_food.py ├── query_order_and_pay.py ├── query_route.py ├── query_travel_left.py ├── query_travel_left_parallel.py ├── scenarios.py └── utils.py ├── readme.md └── test.py /.github/workflows/otel-build.yml: -------------------------------------------------------------------------------- 1 | name: Build and Push Docker Image 2 | 3 | on: 4 | push: 5 | branches: 6 | - main 7 | tags: 8 | - 'v*' 9 | 10 | jobs: 11 | build: 12 | runs-on: ubuntu-latest 13 | 14 | steps: 15 | - name: Checkout repository 16 | uses: actions/checkout@v3 17 | 18 | - name: Set up Docker Buildx 19 | uses: docker/setup-buildx-action@v2 20 | 21 | - name: Log in to Docker Hub 22 | uses: docker/login-action@v2 23 | with: 24 | username: ${{ secrets.DOCKER_HUB_USERNAME }} 25 | password: ${{ secrets.DOCKER_HUB_TOKEN }} 26 | 27 | - name: Generate Tag for Current Date 28 | id: generate_tag 29 | run: | 30 | TAG=$(date +'%Y%m%d') 31 | echo "Generated tag: $TAG" 32 | echo "TAG=$TAG" >> $GITHUB_ENV 33 | 34 | - name: Build and Push Docker Image 35 | run: | 36 | # Build the image with the date tag 37 | docker build -t ${{ secrets.DOCKER_HUB_USERNAME }}/otel-compressor:${{ env.TAG }} ./otel-compressor 38 | # Push the image with the date tag 39 | docker push ${{ secrets.DOCKER_HUB_USERNAME }}/otel-compressor:${{ env.TAG }} 40 | # Tag the image with the 'latest' tag 41 | docker tag ${{ secrets.DOCKER_HUB_USERNAME }}/otel-compressor:${{ env.TAG }} ${{ secrets.DOCKER_HUB_USERNAME }}/otel-compressor:latest 42 | # Push the image with the 'latest' tag 43 | docker push ${{ secrets.DOCKER_HUB_USERNAME }}/otel-compressor:latest -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # TraceZip 2 | 3 | It is a collection of works which is made to build a prototype system of `TraceZip`, made by 4 pieces. 4 | 5 | - `otel-auto-instrumentation-survey` is a Java Project, shows how we generated work loads from some famous middlewares. 6 | - `otel-compressor` is an online-version implementation of TraceZip. 7 | - `static-compressor` is an offline-version implementation of TraceZip. You can use it to compress CSV files. 8 | - `train-ticket-workload` show how we generated Train-Ticket benchmark tracing datas. 9 | 10 | For more information, please enter in the corresponding directory and read its README. 11 | 12 | --- 13 | 14 | ## Note on Data Usage: **Cluster-Trace-Microservices-v2022** 15 | 16 | Our research utilizes the **Cluster-Trace-Microservices-v2022** dataset from the Alibaba Cluster Trace Program, which provides fine-granularity, large-scale microarchitectural metrics from Alibaba's colocation datacenters. Specifically, we have extracted and compressed trace data for the same interface to streamline our analysis. 17 | 18 | For further details on this dataset, refer to the official repository: [Alibaba Cluster Data](https://github.com/alibaba/clusterdata). The `static-compressor` compress the CallGraph. 19 | 20 | We express our gratitude to Alibaba Group for making this invaluable dataset available for the research community. 21 | 22 | --- 23 | 24 | ## TrainTicket Benchmark Spans collection 25 | 26 | Our TraceZip compression middleware can be applied to microservice systems that use OpenTelemetry for trace collection. 27 | 28 | For easier experiment reproduction, we later collected the Span data of microservices and middleware in a single-server environment. Here, we provide the [Span data](https://zenodo.org/records/14921120) generated by these services, which you can use to test the compression rate of our program for Spans. We have removed data that might contain author information from the dataset to maintain anonymity during the review process. The total size of the dataset after extraction is approximately 16 GB. 29 | -------------------------------------------------------------------------------- /otel-auto-instrumentation-survey/.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | !**/src/main/**/target/ 4 | !**/src/test/**/target/ 5 | 6 | ### IntelliJ IDEA ### 7 | .idea/modules.xml 8 | .idea/jarRepositories.xml 9 | .idea/compiler.xml 10 | .idea/libraries/ 11 | *.iws 12 | *.iml 13 | *.ipr 14 | 15 | ### Eclipse ### 16 | .apt_generated 17 | .classpath 18 | .factorypath 19 | .project 20 | .settings 21 | .springBeans 22 | .sts4-cache 23 | 24 | ### NetBeans ### 25 | /nbproject/private/ 26 | /nbbuild/ 27 | /dist/ 28 | /nbdist/ 29 | /.nb-gradle/ 30 | build/ 31 | !**/src/main/**/build/ 32 | !**/src/test/**/build/ 33 | 34 | ### VS Code ### 35 | .vscode/ 36 | 37 | ### Mac OS ### 38 | .DS_Store 39 | 40 | # opentelemetry-javaagent.jar -------------------------------------------------------------------------------- /otel-auto-instrumentation-survey/.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | # Editor-based HTTP Client requests 5 | /httpRequests/ 6 | # Datasource local storage ignored files 7 | /dataSources/ 8 | /dataSources.local.xml 9 | -------------------------------------------------------------------------------- /otel-auto-instrumentation-survey/.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /otel-auto-instrumentation-survey/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /otel-auto-instrumentation-survey/.idea/sqldialects.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /otel-auto-instrumentation-survey/.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /otel-auto-instrumentation-survey/README.md: -------------------------------------------------------------------------------- 1 | To run it, add `VM Options`: 2 | ``` 3 | --add-opens 4 | java.base/java.lang=ALL-UNNAMED 5 | --add-opens 6 | java.base/java.util=ALL-UNNAMED 7 | --add-opens 8 | java.base/java.nio=ALL-UNNAMED 9 | --add-opens 10 | java.base/sun.nio.ch=ALL-UNNAMED 11 | --add-opens 12 | java.base/java.math=ALL-UNNAMED 13 | ``` 14 | 15 | Otel Instrumentation Via Java Agent: 16 | ``` 17 | JAVA_TOOL_OPTIONS=-javaagent:/path/to/your/opentelemetry-javaagent.jar; 18 | ``` -------------------------------------------------------------------------------- /otel-auto-instrumentation-survey/opentelemetry-javaagent.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OpsPAI/TraceZip/8b9fc5aa3364cce489f9baac6a1b750f21b24c8e/otel-auto-instrumentation-survey/opentelemetry-javaagent.jar -------------------------------------------------------------------------------- /otel-auto-instrumentation-survey/src/main/java/org/example/Main.java: -------------------------------------------------------------------------------- 1 | package org.example; 2 | 3 | 4 | import org.example.apache_http_client.ApacheHttpExecute; 5 | import org.example.grpc.TestClient; 6 | import org.example.grpc.TestServer; 7 | import org.example.grpc.TestServiceGrpc; 8 | import org.example.jedis.JedisExecute; 9 | import org.example.jetty_servlet.JettyServer; 10 | import org.example.kafka_consumer.KafkaConsumerExecute; 11 | import org.example.kafka_consumer.KafkaProducerExecute; 12 | import org.example.mongo.MongoDB; 13 | import org.example.mysql.MySQLJDBCExample; 14 | 15 | import java.io.IOException; 16 | 17 | import static java.lang.Thread.sleep; 18 | 19 | public class Main { 20 | public static void main(String[] args) throws Exception { 21 | final String EXECUTION = "grpc"; 22 | 23 | switch (EXECUTION) { 24 | case "grpc" -> { 25 | new Thread(() -> { 26 | try { 27 | TestServer.main(null); 28 | } catch (Exception e) { 29 | throw new RuntimeException(e); 30 | } 31 | }); 32 | TestClient.main(null); 33 | } 34 | 35 | case "kafka" -> { 36 | new Thread(() -> KafkaConsumerExecute.main(null)); 37 | for(int i = 0; i < 100; i ++) { 38 | KafkaProducerExecute.main(null); 39 | } 40 | } 41 | 42 | case "redis" -> JedisExecute.main(null); 43 | 44 | case "servlet" -> JettyServer.main(null); 45 | 46 | case "mongodb" -> MongoDB.main(null); 47 | 48 | case "mysql" -> MySQLJDBCExample.main(null); 49 | } 50 | 51 | } 52 | 53 | } -------------------------------------------------------------------------------- /otel-auto-instrumentation-survey/src/main/java/org/example/dubbo/DubboClientApplication.java: -------------------------------------------------------------------------------- 1 | package org.example.dubbo; 2 | 3 | import org.apache.dubbo.config.ApplicationConfig; 4 | import org.apache.dubbo.config.ReferenceConfig; 5 | import org.apache.dubbo.config.RegistryConfig; 6 | 7 | import java.util.UUID; 8 | 9 | public class DubboClientApplication { 10 | 11 | public static void main(String[] args) { 12 | ApplicationConfig application = new ApplicationConfig(); 13 | application.setName("dubbo-client"); 14 | 15 | RegistryConfig registry = new RegistryConfig(); 16 | registry.setAddress("N/A"); // 使用直连模式,不使用注册中心 17 | 18 | ReferenceConfig reference = new ReferenceConfig<>(); 19 | // reference.setApplication(application); 20 | reference.setRegistry(registry); 21 | reference.setInterface(GreetingService.class); 22 | reference.setUrl("dubbo://localhost:20880"); 23 | 24 | GreetingService greetingService = reference.get(); 25 | String message = greetingService.sayHello(UUID.randomUUID().toString()); 26 | System.out.println(message); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /otel-auto-instrumentation-survey/src/main/java/org/example/dubbo/DubboServerApplication.java: -------------------------------------------------------------------------------- 1 | package org.example.dubbo; 2 | 3 | 4 | 5 | import org.apache.dubbo.config.spring.context.annotation.EnableDubbo; 6 | import org.springframework.context.annotation.AnnotationConfigApplicationContext; 7 | import org.springframework.context.annotation.Bean; 8 | import org.springframework.context.annotation.Configuration; 9 | 10 | @EnableDubbo 11 | @Configuration 12 | public class DubboServerApplication { 13 | 14 | public static void main(String[] args) { 15 | AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(DubboServerApplication.class); 16 | context.start(); 17 | System.out.println("Dubbo server started."); 18 | } 19 | 20 | @Bean 21 | public GreetingServiceImpl greetingService() { 22 | return new GreetingServiceImpl(); 23 | } 24 | } -------------------------------------------------------------------------------- /otel-auto-instrumentation-survey/src/main/java/org/example/dubbo/GreetingService.java: -------------------------------------------------------------------------------- 1 | package org.example.dubbo; 2 | 3 | public interface GreetingService { 4 | String sayHello(String name); 5 | } -------------------------------------------------------------------------------- /otel-auto-instrumentation-survey/src/main/java/org/example/dubbo/GreetingServiceImpl.java: -------------------------------------------------------------------------------- 1 | package org.example.dubbo; 2 | 3 | import org.apache.dubbo.config.annotation.DubboService; 4 | @DubboService 5 | public class GreetingServiceImpl implements GreetingService { 6 | @Override 7 | public String sayHello(String name) { 8 | return "Hello, " + name; 9 | } 10 | } -------------------------------------------------------------------------------- /otel-auto-instrumentation-survey/src/main/java/org/example/grpc/example.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | 3 | package test; 4 | 5 | service TestService { 6 | rpc Service1 (ServiceRequest) returns (ServiceResponse); 7 | rpc Service2 (ServiceRequest) returns (ServiceResponse); 8 | rpc Service3 (ServiceRequest) returns (ServiceResponse); 9 | rpc Service4 (ServiceRequest) returns (ServiceResponse); 10 | rpc Service5 (ServiceRequest) returns (ServiceResponse); 11 | } 12 | 13 | message ServiceRequest { 14 | string param1 = 1; 15 | string param2 = 2; 16 | string param3 = 3; 17 | } 18 | 19 | message ServiceResponse { 20 | string result = 1; 21 | } 22 | -------------------------------------------------------------------------------- /otel-auto-instrumentation-survey/src/main/java/org/example/jetty_servlet/HelloServlet.java: -------------------------------------------------------------------------------- 1 | package org.example.jetty_servlet; 2 | 3 | import javax.servlet.annotation.WebServlet; 4 | import javax.servlet.http.HttpServlet; 5 | import javax.servlet.http.HttpServletRequest; 6 | import javax.servlet.http.HttpServletResponse; 7 | import java.io.IOException; 8 | import java.io.PrintWriter; 9 | 10 | @WebServlet("/hello") 11 | public class HelloServlet extends HttpServlet { 12 | 13 | @Override 14 | protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { 15 | // 设置响应内容类型 16 | response.setContentType("text/html;charset=UTF-8"); 17 | 18 | // 实际的逻辑是在这里 19 | PrintWriter out = response.getWriter(); 20 | out.println(""); 21 | out.println("Hello Servlet"); 22 | out.println(""); 23 | out.println("

Hello, GET request!

"); 24 | out.println(""); 25 | out.println(""); 26 | } 27 | 28 | @Override 29 | protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException { 30 | // 设置响应内容类型 31 | response.setContentType("text/html;charset=UTF-8"); 32 | 33 | // 实际的逻辑是在这里 34 | PrintWriter out = response.getWriter(); 35 | out.println(""); 36 | out.println("Hello Servlet"); 37 | out.println(""); 38 | out.println("

Hello, POST request!

"); 39 | out.println(""); 40 | out.println(""); 41 | } 42 | 43 | @Override 44 | protected void doPut(HttpServletRequest request, HttpServletResponse response) throws IOException { 45 | // 设置响应内容类型 46 | response.setContentType("text/html;charset=UTF-8"); 47 | 48 | // 实际的逻辑是在这里 49 | PrintWriter out = response.getWriter(); 50 | out.println(""); 51 | out.println("Hello Servlet"); 52 | out.println(""); 53 | out.println("

Hello, PUT request!

"); 54 | out.println(""); 55 | out.println(""); 56 | } 57 | 58 | @Override 59 | protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws IOException { 60 | // 设置响应内容类型 61 | response.setContentType("text/html;charset=UTF-8"); 62 | 63 | // 实际的逻辑是在这里 64 | PrintWriter out = response.getWriter(); 65 | out.println(""); 66 | out.println("Hello Servlet"); 67 | out.println(""); 68 | out.println("

Hello, DELETE request!

"); 69 | out.println(""); 70 | out.println(""); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /otel-auto-instrumentation-survey/src/main/java/org/example/jetty_servlet/JettyServer.java: -------------------------------------------------------------------------------- 1 | package org.example.jetty_servlet; 2 | 3 | 4 | import org.eclipse.jetty.server.Server; 5 | import org.eclipse.jetty.servlet.ServletContextHandler; 6 | import org.eclipse.jetty.servlet.ServletHolder; 7 | 8 | public class JettyServer { 9 | 10 | public static void main(String[] args) throws Exception { 11 | // 创建 Jetty 服务器 12 | Server server = new Server(9070); 13 | 14 | // 创建 Servlet 上下文处理器 15 | ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS); 16 | context.setContextPath("/"); 17 | 18 | // 添加 Servlet 到上下文 19 | context.addServlet(new ServletHolder(new HelloServlet()), "/1a3fef35-bfb8-440c-b8b8-eb57a3f01d2a"); 20 | context.addServlet(new ServletHolder(new HelloServlet()), "/ca254a0d-786d-4331-becf-37a35d713253"); 21 | context.addServlet(new ServletHolder(new HelloServlet()), "/7ed3f15b-23e6-48c6-8eb4-65f4790dacc6"); 22 | context.addServlet(new ServletHolder(new HelloServlet()), "/2bddf016-7afa-4473-aa15-1d7f917e5ad4"); 23 | 24 | // 设置服务器的处理器 25 | server.setHandler(context); 26 | 27 | // 启动服务器 28 | server.start(); 29 | System.out.println("Jetty server started."); 30 | 31 | // 等待服务器终止 32 | server.join(); 33 | } 34 | } -------------------------------------------------------------------------------- /otel-auto-instrumentation-survey/src/main/java/org/example/kafka_consumer/KafkaConsumerExecute.java: -------------------------------------------------------------------------------- 1 | package org.example.kafka_consumer; 2 | 3 | import org.apache.kafka.clients.consumer.*; 4 | import org.apache.kafka.common.serialization.StringDeserializer; 5 | 6 | import java.time.Duration; 7 | import java.util.Arrays; 8 | import java.util.Properties; 9 | 10 | public class KafkaConsumerExecute { 11 | public static void main(String[] args) { 12 | String[] topics = {"topic-1", "topic-2", "topic-3", "topic-4", "topic-5"}; 13 | Properties props = new Properties(); 14 | props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092"); 15 | props.put(ConsumerConfig.GROUP_ID_CONFIG, "test-group"); 16 | props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName()); 17 | props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName()); 18 | 19 | KafkaConsumer consumer = new KafkaConsumer<>(props); 20 | consumer.subscribe(Arrays.asList(topics)); 21 | while (true) { 22 | ConsumerRecords records = consumer.poll(Duration.ofMillis(100)); 23 | for (ConsumerRecord record : records) { 24 | System.out.printf("Consumed record from topic %s with key %s and value %s%n", record.topic(), record.key(), record.value()); 25 | } 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /otel-auto-instrumentation-survey/src/main/java/org/example/kafka_consumer/KafkaProducerExecute.java: -------------------------------------------------------------------------------- 1 | package org.example.kafka_consumer; 2 | 3 | import org.apache.kafka.clients.producer.KafkaProducer; 4 | import org.apache.kafka.clients.producer.ProducerConfig; 5 | import org.apache.kafka.clients.producer.ProducerRecord; 6 | import org.apache.kafka.common.serialization.StringSerializer; 7 | 8 | import java.util.ArrayList; 9 | import java.util.Properties; 10 | import java.util.Random; 11 | import java.util.UUID; 12 | 13 | public class KafkaProducerExecute { 14 | public static void main(String[] args) throws InterruptedException { 15 | String[] topics = {UUID.randomUUID().toString(), UUID.randomUUID().toString(), UUID.randomUUID().toString(), UUID.randomUUID().toString(), UUID.randomUUID().toString()}; 16 | var keys = new ArrayList(); 17 | for(var i = 0; i < 20; i++) { 18 | keys.add(UUID.randomUUID().toString()); 19 | } 20 | Properties props = new Properties(); 21 | props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092"); 22 | props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName()); 23 | props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName()); 24 | 25 | KafkaProducer producer = new KafkaProducer<>(props); 26 | Random random = new Random(); 27 | 28 | for (int i = 0; i < 3000000; i++) { 29 | String topic = topics[random.nextInt(topics.length)]; 30 | String key = keys.get(random.nextInt(keys.size())); 31 | String value = "value-" + random.nextInt(1000000); 32 | ProducerRecord record = new ProducerRecord<>(topic, key, value); 33 | producer.send(record); 34 | Thread.sleep(1); 35 | } 36 | producer.close(); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /otel-auto-instrumentation-survey/src/main/resources/dubbo.properties: -------------------------------------------------------------------------------- 1 | dubbo.application.name=dubbo-server 2 | dubbo.protocol.name=dubbo 3 | dubbo.protocol.port=20880 4 | dubbo.registry.address=N/A 5 | -------------------------------------------------------------------------------- /otel-compressor/.gitignore: -------------------------------------------------------------------------------- 1 | wrk/node_modules 2 | 3 | wrk_datasets/node_modules 4 | 5 | .tools/ 6 | .vscode/ 7 | 8 | *.csv 9 | dataset/ 10 | 11 | t.json 12 | t.yaml 13 | 14 | *.log 15 | 16 | ocb -------------------------------------------------------------------------------- /otel-compressor/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM golang:latest AS builder 2 | 3 | WORKDIR /otel 4 | 5 | COPY . . 6 | 7 | RUN go build -o /otel/gateway_collector ./otelcol-dev 8 | 9 | FROM ubuntu:latest 10 | 11 | WORKDIR /app 12 | 13 | COPY --from=builder /otel/gateway_collector /app/gateway_collector 14 | COPY --from=builder /otel/config/compressor-agent.yml /app/compressor-agent.yml 15 | 16 | ENTRYPOINT [ "/app/gateway_collector" ] 17 | CMD ["--config", "compressor-agent.yml"] 18 | 19 | EXPOSE 4317 20 | EXPOSE 4318 21 | EXPOSE 8888 22 | EXPOSE 55678 23 | EXPOSE 55679 24 | EXPOSE 6831 25 | EXPOSE 6832 26 | -------------------------------------------------------------------------------- /otel-compressor/builder-config.yaml: -------------------------------------------------------------------------------- 1 | dist: 2 | name: otelcol-dev 3 | description: Basic OTel Collector distribution for Developers 4 | output_path: ./otelcol-dev 5 | otelcol_version: 0.95.0 6 | 7 | exporters: 8 | - gomod: 9 | # NOTE: Prior to v0.86.0 use the `loggingexporter` instead of `debugexporter`. 10 | go.opentelemetry.io/collector/exporter/debugexporter v0.95.0 11 | - gomod: 12 | go.opentelemetry.io/collector/exporter/otlpexporter v0.95.0 13 | 14 | processors: 15 | - gomod: 16 | go.opentelemetry.io/collector/processor/batchprocessor v0.95.0 17 | 18 | receivers: 19 | - gomod: 20 | go.opentelemetry.io/collector/receiver/otlpreceiver v0.95.0 -------------------------------------------------------------------------------- /otel-compressor/config/compressor-agent.yml: -------------------------------------------------------------------------------- 1 | receivers: 2 | otlp: 3 | protocols: 4 | http: 5 | grpc: 6 | 7 | processors: 8 | batch: 9 | send_batch_size: 1024 10 | timeout: 10s 11 | 12 | exporters: 13 | prefix_compressed_exporter: 14 | sample_buffer: 102400 15 | srt_threshold: 4000 16 | attr_limit: 100 17 | calc_zip_rate: true 18 | enable_gzip: true 19 | delete_resource: true 20 | endpoint: http://compressor-gateway:4318 21 | tls: 22 | insecure: true 23 | 24 | service: 25 | pipelines: 26 | traces: 27 | receivers: [otlp] 28 | processors: [batch] 29 | exporters: [prefix_compressed_exporter] 30 | telemetry: 31 | logs: 32 | level: debug -------------------------------------------------------------------------------- /otel-compressor/config/compressor-gateway.yml: -------------------------------------------------------------------------------- 1 | receivers: 2 | prefix_compressed_receiver: 3 | protocols: 4 | http: 5 | endpoint: 0.0.0.0:4318 6 | traces_dictionary_url_path: /v1/tracesdict 7 | 8 | processors: 9 | batch: 10 | 11 | exporters: 12 | # NOTE: Prior to v0.86.0 use `logging` instead of `debug`. 13 | debug: 14 | verbosity: basic 15 | 16 | service: 17 | pipelines: 18 | traces: 19 | receivers: [prefix_compressed_receiver] 20 | processors: [batch] 21 | exporters: [debug] 22 | telemetry: 23 | logs: 24 | level: debug -------------------------------------------------------------------------------- /otel-compressor/docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '3' 2 | services: 3 | compressor-agent: 4 | image: angrychow1/otel-compressor:latest 5 | deploy: 6 | restart_policy: 7 | condition: on-failure 8 | networks: 9 | - my-network 10 | volumes: 11 | - ./config/compressor-agent.yml:/app/agent-config.yml 12 | command: ["--config", "agent-config.yml"] 13 | depends_on: 14 | - compressor-gateway 15 | ports: 16 | - "4318:4318" 17 | 18 | compressor-gateway: 19 | image: angrychow1/otel-compressor:latest 20 | deploy: 21 | restart_policy: 22 | condition: on-failure 23 | networks: 24 | - my-network 25 | volumes: 26 | - ./config/compressor-gateway.yml:/app/gateway-config.yml 27 | command: ["--config", "gateway-config.yml"] 28 | 29 | networks: 30 | my-network: 31 | driver: bridge -------------------------------------------------------------------------------- /otel-compressor/go.work: -------------------------------------------------------------------------------- 1 | go 1.21.3 2 | 3 | use ( 4 | ./otelcol-dev 5 | ./pdata 6 | ./prefix-compressed-exporter 7 | ./prefix-compressed-receiver 8 | ) 9 | -------------------------------------------------------------------------------- /otel-compressor/otelcol-dev/components.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "go.opentelemetry.io/collector/connector" 5 | "go.opentelemetry.io/collector/exporter" 6 | debugexporter "go.opentelemetry.io/collector/exporter/debugexporter" 7 | otlpexporter "go.opentelemetry.io/collector/exporter/otlpexporter" 8 | "go.opentelemetry.io/collector/extension" 9 | "go.opentelemetry.io/collector/otelcol" 10 | "go.opentelemetry.io/collector/processor" 11 | batchprocessor "go.opentelemetry.io/collector/processor/batchprocessor" 12 | "go.opentelemetry.io/collector/receiver" 13 | otlpreceiver "go.opentelemetry.io/collector/receiver/otlpreceiver" 14 | 15 | prefix_compressed_exporter "angrychow/otel/prefix-compressed-exporter" 16 | 17 | prefix_compressed_receiver "angrychow/otel/prefix-compressed-receiver" 18 | 19 | jaegerreceiver "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/jaegerreceiver" 20 | ) 21 | 22 | func components() (otelcol.Factories, error) { 23 | var err error 24 | factories := otelcol.Factories{} 25 | 26 | factories.Extensions, err = extension.MakeFactoryMap() 27 | if err != nil { 28 | return otelcol.Factories{}, err 29 | } 30 | 31 | factories.Receivers, err = receiver.MakeFactoryMap( 32 | otlpreceiver.NewFactory(), 33 | prefix_compressed_receiver.NewFactory(), 34 | jaegerreceiver.NewFactory(), 35 | ) 36 | if err != nil { 37 | return otelcol.Factories{}, err 38 | } 39 | 40 | factories.Exporters, err = exporter.MakeFactoryMap( 41 | debugexporter.NewFactory(), 42 | otlpexporter.NewFactory(), 43 | prefix_compressed_exporter.NewFactory(), 44 | ) 45 | if err != nil { 46 | return otelcol.Factories{}, err 47 | } 48 | 49 | factories.Processors, err = processor.MakeFactoryMap( 50 | batchprocessor.NewFactory(), 51 | ) 52 | if err != nil { 53 | return otelcol.Factories{}, err 54 | } 55 | 56 | factories.Connectors, err = connector.MakeFactoryMap() 57 | if err != nil { 58 | return otelcol.Factories{}, err 59 | } 60 | 61 | return factories, nil 62 | } 63 | -------------------------------------------------------------------------------- /otel-compressor/otelcol-dev/components_test.go: -------------------------------------------------------------------------------- 1 | // Code generated by "go.opentelemetry.io/collector/cmd/builder". DO NOT EDIT. 2 | 3 | package main 4 | 5 | import ( 6 | "testing" 7 | 8 | "github.com/stretchr/testify/assert" 9 | 10 | "go.opentelemetry.io/collector/component/componenttest" 11 | ) 12 | 13 | func TestValidateConfigs(t *testing.T) { 14 | factories, err := components() 15 | assert.NoError(t, err) 16 | 17 | for k, factory := range factories.Receivers { 18 | assert.Equal(t, k, factory.Type()) 19 | assert.NoError(t, componenttest.CheckConfigStruct(factory.CreateDefaultConfig())) 20 | } 21 | 22 | for k, factory := range factories.Processors { 23 | assert.Equal(t, k, factory.Type()) 24 | assert.NoError(t, componenttest.CheckConfigStruct(factory.CreateDefaultConfig())) 25 | } 26 | 27 | for k, factory := range factories.Exporters { 28 | assert.Equal(t, k, factory.Type()) 29 | assert.NoError(t, componenttest.CheckConfigStruct(factory.CreateDefaultConfig())) 30 | } 31 | 32 | for k, factory := range factories.Connectors { 33 | assert.Equal(t, k, factory.Type()) 34 | assert.NoError(t, componenttest.CheckConfigStruct(factory.CreateDefaultConfig())) 35 | } 36 | 37 | for k, factory := range factories.Extensions { 38 | assert.Equal(t, k, factory.Type()) 39 | assert.NoError(t, componenttest.CheckConfigStruct(factory.CreateDefaultConfig())) 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /otel-compressor/otelcol-dev/main.go: -------------------------------------------------------------------------------- 1 | // Code generated by "go.opentelemetry.io/collector/cmd/builder". DO NOT EDIT. 2 | 3 | // Program otelcol-dev is an OpenTelemetry Collector binary. 4 | package main 5 | 6 | import ( 7 | "log" 8 | // "net/http" 9 | // _ "net/http/pprof" 10 | 11 | "go.opentelemetry.io/collector/component" 12 | "go.opentelemetry.io/collector/otelcol" 13 | ) 14 | 15 | func main() { 16 | // PPROF INJECTION 17 | // go func() { 18 | // log.Println(http.ListenAndServe("localhost:6060", nil)) 19 | // }() 20 | info := component.BuildInfo{ 21 | Command: "otelcol-dev", 22 | Description: "Basic OTel Collector distribution for Developers", 23 | Version: "1.0.0", 24 | } 25 | 26 | if err := run(otelcol.CollectorSettings{BuildInfo: info, Factories: components}); err != nil { 27 | log.Fatal(err) 28 | } 29 | } 30 | 31 | func runInteractive(params otelcol.CollectorSettings) error { 32 | cmd := otelcol.NewCommand(params) 33 | if err := cmd.Execute(); err != nil { 34 | log.Fatalf("collector server run finished with error: %v", err) 35 | } 36 | 37 | return nil 38 | } 39 | -------------------------------------------------------------------------------- /otel-compressor/otelcol-dev/main_others.go: -------------------------------------------------------------------------------- 1 | // Code generated by "go.opentelemetry.io/collector/cmd/builder". DO NOT EDIT. 2 | 3 | //go:build !windows 4 | 5 | package main 6 | 7 | import "go.opentelemetry.io/collector/otelcol" 8 | 9 | func run(params otelcol.CollectorSettings) error { 10 | return runInteractive(params) 11 | } 12 | -------------------------------------------------------------------------------- /otel-compressor/otelcol-dev/main_windows.go: -------------------------------------------------------------------------------- 1 | // Code generated by "go.opentelemetry.io/collector/cmd/builder". DO NOT EDIT. 2 | 3 | //go:build windows 4 | 5 | package main 6 | 7 | import ( 8 | "errors" 9 | "fmt" 10 | 11 | "golang.org/x/sys/windows" 12 | "golang.org/x/sys/windows/svc" 13 | 14 | "go.opentelemetry.io/collector/otelcol" 15 | ) 16 | 17 | func run(params otelcol.CollectorSettings) error { 18 | // No need to supply service name when startup is invoked through 19 | // the Service Control Manager directly. 20 | if err := svc.Run("", otelcol.NewSvcHandler(params)); err != nil { 21 | if errors.Is(err, windows.ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) { 22 | // Per https://learn.microsoft.com/en-us/windows/win32/api/winsvc/nf-winsvc-startservicectrldispatchera#return-value 23 | // this means that the process is not running as a service, so run interactively. 24 | return runInteractive(params) 25 | } 26 | 27 | return fmt.Errorf("failed to start collector server: %w", err) 28 | } 29 | 30 | return nil 31 | } 32 | -------------------------------------------------------------------------------- /otel-compressor/otelcol-dev/test.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OpsPAI/TraceZip/8b9fc5aa3364cce489f9baac6a1b750f21b24c8e/otel-compressor/otelcol-dev/test.txt -------------------------------------------------------------------------------- /otel-compressor/pdata/Makefile: -------------------------------------------------------------------------------- 1 | include ../Makefile.Common 2 | -------------------------------------------------------------------------------- /otel-compressor/pdata/go.mod: -------------------------------------------------------------------------------- 1 | module go.opentelemetry.io/collector/pdata 2 | 3 | go 1.21 4 | 5 | require ( 6 | github.com/gogo/protobuf v1.3.2 7 | github.com/json-iterator/go v1.1.12 8 | github.com/stretchr/testify v1.8.4 9 | go.uber.org/goleak v1.3.0 10 | go.uber.org/multierr v1.11.0 11 | google.golang.org/grpc v1.62.0 12 | google.golang.org/protobuf v1.32.0 13 | ) 14 | 15 | require ( 16 | github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect 17 | github.com/golang/protobuf v1.5.3 // indirect 18 | github.com/google/uuid v1.6.0 19 | github.com/kr/pretty v0.3.1 // indirect 20 | github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect 21 | github.com/modern-go/reflect2 v1.0.2 // indirect 22 | github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect 23 | github.com/rogpeppe/go-internal v1.11.0 // indirect 24 | golang.org/x/net v0.21.0 // indirect 25 | golang.org/x/sys v0.21.0 // indirect 26 | golang.org/x/text v0.14.0 // indirect 27 | google.golang.org/genproto/googleapis/rpc v0.0.0-20240221002015-b0ce06bbee7c // indirect 28 | gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect 29 | gopkg.in/yaml.v3 v3.0.1 // indirect 30 | ) 31 | 32 | retract ( 33 | v1.0.0-rc10 // RC version scheme discovered to be alphabetical, use v1.0.0-rcv0011 instead 34 | v0.57.1 // Release failed, use v0.57.2 35 | v0.57.0 // Release failed, use v0.57.2 36 | ) 37 | -------------------------------------------------------------------------------- /otel-compressor/pdata/internal/.gitignore: -------------------------------------------------------------------------------- 1 | .patched-otlp-proto 2 | opentelemetry-proto 3 | -------------------------------------------------------------------------------- /otel-compressor/pdata/internal/cmd/pdatagen/internal/plogotlp_package.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package internal // import "go.opentelemetry.io/collector/pdata/internal/cmd/pdatagen/internal" 5 | import ( 6 | "path/filepath" 7 | ) 8 | 9 | var plogotlp = &Package{ 10 | name: "plogotlp", 11 | path: filepath.Join("plog", "plogotlp"), 12 | imports: []string{ 13 | `otlpcollectorlog "go.opentelemetry.io/collector/pdata/internal/data/protogen/collector/logs/v1"`, 14 | }, 15 | testImports: []string{ 16 | `"testing"`, 17 | ``, 18 | `"github.com/stretchr/testify/assert"`, 19 | }, 20 | structs: []baseStruct{ 21 | exportLogsPartialSuccess, 22 | }, 23 | } 24 | 25 | var exportLogsPartialSuccess = &messageValueStruct{ 26 | structName: "ExportPartialSuccess", 27 | description: "// ExportPartialSuccess represents the details of a partially successful export request.", 28 | originFullName: "otlpcollectorlog.ExportLogsPartialSuccess", 29 | fields: []baseField{ 30 | &primitiveField{ 31 | fieldName: "RejectedLogRecords", 32 | returnType: "int64", 33 | defaultVal: `int64(0)`, 34 | testVal: `int64(13)`, 35 | }, 36 | &primitiveField{ 37 | fieldName: "ErrorMessage", 38 | returnType: "string", 39 | defaultVal: `""`, 40 | testVal: `"error message"`, 41 | }, 42 | }, 43 | } 44 | -------------------------------------------------------------------------------- /otel-compressor/pdata/internal/cmd/pdatagen/internal/pmetricotlp_package.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package internal // import "go.opentelemetry.io/collector/pdata/internal/cmd/pdatagen/internal" 5 | 6 | import ( 7 | "path/filepath" 8 | ) 9 | 10 | var pmetricotlp = &Package{ 11 | name: "pmetricotlp", 12 | path: filepath.Join("pmetric", "pmetricotlp"), 13 | imports: []string{ 14 | `otlpcollectormetrics "go.opentelemetry.io/collector/pdata/internal/data/protogen/collector/metrics/v1"`, 15 | }, 16 | testImports: []string{ 17 | `"testing"`, 18 | ``, 19 | `"github.com/stretchr/testify/assert"`, 20 | }, 21 | structs: []baseStruct{ 22 | exportMetricsPartialSuccess, 23 | }, 24 | } 25 | 26 | var exportMetricsPartialSuccess = &messageValueStruct{ 27 | structName: "ExportPartialSuccess", 28 | description: "// ExportPartialSuccess represents the details of a partially successful export request.", 29 | originFullName: "otlpcollectormetrics.ExportMetricsPartialSuccess", 30 | fields: []baseField{ 31 | &primitiveField{ 32 | fieldName: "RejectedDataPoints", 33 | returnType: "int64", 34 | defaultVal: `int64(0)`, 35 | testVal: `int64(13)`, 36 | }, 37 | &primitiveField{ 38 | fieldName: "ErrorMessage", 39 | returnType: "string", 40 | defaultVal: `""`, 41 | testVal: `"error message"`, 42 | }, 43 | }, 44 | } 45 | -------------------------------------------------------------------------------- /otel-compressor/pdata/internal/cmd/pdatagen/internal/ptraceotlp_package.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package internal // import "go.opentelemetry.io/collector/pdata/internal/cmd/pdatagen/internal" 5 | 6 | import ( 7 | "path/filepath" 8 | ) 9 | 10 | var ptraceotlp = &Package{ 11 | name: "ptraceotlp", 12 | path: filepath.Join("ptrace", "ptraceotlp"), 13 | imports: []string{ 14 | `otlpcollectortrace "go.opentelemetry.io/collector/pdata/internal/data/protogen/collector/trace/v1"`, 15 | }, 16 | testImports: []string{ 17 | `"testing"`, 18 | ``, 19 | `"github.com/stretchr/testify/assert"`, 20 | }, 21 | structs: []baseStruct{ 22 | exportTracePartialSuccess, 23 | }, 24 | } 25 | 26 | var exportTracePartialSuccess = &messageValueStruct{ 27 | structName: "ExportPartialSuccess", 28 | description: "// ExportPartialSuccess represents the details of a partially successful export request.", 29 | originFullName: "otlpcollectortrace.ExportTracePartialSuccess", 30 | fields: []baseField{ 31 | &primitiveField{ 32 | fieldName: "RejectedSpans", 33 | returnType: "int64", 34 | defaultVal: `int64(0)`, 35 | testVal: `int64(13)`, 36 | }, 37 | &primitiveField{ 38 | fieldName: "ErrorMessage", 39 | returnType: "string", 40 | defaultVal: `""`, 41 | testVal: `"error message"`, 42 | }, 43 | }, 44 | } 45 | -------------------------------------------------------------------------------- /otel-compressor/pdata/internal/cmd/pdatagen/main.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package main 5 | 6 | import ( 7 | "go.opentelemetry.io/collector/pdata/internal/cmd/pdatagen/internal" 8 | ) 9 | 10 | func check(e error) { 11 | if e != nil { 12 | panic(e) 13 | } 14 | } 15 | 16 | func main() { 17 | for _, fp := range internal.AllPackages { 18 | check(fp.GenerateFiles()) 19 | check(fp.GenerateTestFiles()) 20 | check(fp.GenerateInternalFiles()) 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /otel-compressor/pdata/internal/data/bytesid.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package data // import "go.opentelemetry.io/collector/pdata/internal/data" 5 | 6 | import ( 7 | "encoding/hex" 8 | "errors" 9 | "fmt" 10 | ) 11 | 12 | // marshalJSON converts trace id into a hex string enclosed in quotes. 13 | // Called by Protobuf JSON deserialization. 14 | func marshalJSON(id []byte) ([]byte, error) { 15 | // Plus 2 quote chars at the start and end. 16 | hexLen := hex.EncodedLen(len(id)) + 2 17 | 18 | b := make([]byte, hexLen) 19 | hex.Encode(b[1:hexLen-1], id) 20 | b[0], b[hexLen-1] = '"', '"' 21 | 22 | return b, nil 23 | } 24 | 25 | // unmarshalJSON inflates trace id from hex string, possibly enclosed in quotes. 26 | // Called by Protobuf JSON deserialization. 27 | func unmarshalJSON(dst []byte, src []byte) error { 28 | if l := len(src); l >= 2 && src[0] == '"' && src[l-1] == '"' { 29 | src = src[1 : l-1] 30 | } 31 | nLen := len(src) 32 | if nLen == 0 { 33 | return nil 34 | } 35 | 36 | if len(dst) != hex.DecodedLen(nLen) { 37 | return errors.New("invalid length for ID") 38 | } 39 | 40 | _, err := hex.Decode(dst, src) 41 | if err != nil { 42 | return fmt.Errorf("cannot unmarshal ID from string '%s': %w", string(src), err) 43 | } 44 | return nil 45 | } 46 | -------------------------------------------------------------------------------- /otel-compressor/pdata/internal/data/package_test.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package data 5 | 6 | import ( 7 | "testing" 8 | 9 | "go.uber.org/goleak" 10 | ) 11 | 12 | func TestMain(m *testing.M) { 13 | goleak.VerifyTestMain(m) 14 | } 15 | -------------------------------------------------------------------------------- /otel-compressor/pdata/internal/data/spanid.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package data // import "go.opentelemetry.io/collector/pdata/internal/data" 5 | 6 | import ( 7 | "errors" 8 | 9 | "github.com/gogo/protobuf/proto" 10 | ) 11 | 12 | const spanIDSize = 8 13 | 14 | var ( 15 | errMarshalSpanID = errors.New("marshal: invalid buffer length for SpanID") 16 | errUnmarshalSpanID = errors.New("unmarshal: invalid SpanID length") 17 | ) 18 | 19 | // SpanID is a custom data type that is used for all span_id fields in OTLP 20 | // Protobuf messages. 21 | type SpanID [spanIDSize]byte 22 | 23 | var _ proto.Sizer = (*SpanID)(nil) 24 | 25 | // Size returns the size of the data to serialize. 26 | func (sid SpanID) Size() int { 27 | if sid.IsEmpty() { 28 | return 0 29 | } 30 | return spanIDSize 31 | } 32 | 33 | // IsEmpty returns true if id contains at least one non-zero byte. 34 | func (sid SpanID) IsEmpty() bool { 35 | return sid == [spanIDSize]byte{} 36 | } 37 | 38 | // MarshalTo converts trace ID into a binary representation. Called by Protobuf serialization. 39 | func (sid SpanID) MarshalTo(data []byte) (n int, err error) { 40 | if sid.IsEmpty() { 41 | return 0, nil 42 | } 43 | 44 | if len(data) < spanIDSize { 45 | return 0, errMarshalSpanID 46 | } 47 | 48 | return copy(data, sid[:]), nil 49 | } 50 | 51 | // Unmarshal inflates this trace ID from binary representation. Called by Protobuf serialization. 52 | func (sid *SpanID) Unmarshal(data []byte) error { 53 | if len(data) == 0 { 54 | *sid = [spanIDSize]byte{} 55 | return nil 56 | } 57 | 58 | if len(data) != spanIDSize { 59 | return errUnmarshalSpanID 60 | } 61 | 62 | copy(sid[:], data) 63 | return nil 64 | } 65 | 66 | // MarshalJSON converts SpanID into a hex string enclosed in quotes. 67 | func (sid SpanID) MarshalJSON() ([]byte, error) { 68 | if sid.IsEmpty() { 69 | return []byte(`""`), nil 70 | } 71 | return marshalJSON(sid[:]) 72 | } 73 | 74 | // UnmarshalJSON decodes SpanID from hex string, possibly enclosed in quotes. 75 | // Called by Protobuf JSON deserialization. 76 | func (sid *SpanID) UnmarshalJSON(data []byte) error { 77 | *sid = [spanIDSize]byte{} 78 | return unmarshalJSON(sid[:], data) 79 | } 80 | -------------------------------------------------------------------------------- /otel-compressor/pdata/internal/data/traceid.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package data // import "go.opentelemetry.io/collector/pdata/internal/data" 5 | 6 | import ( 7 | "errors" 8 | 9 | "github.com/gogo/protobuf/proto" 10 | ) 11 | 12 | const traceIDSize = 16 13 | 14 | var ( 15 | errMarshalTraceID = errors.New("marshal: invalid buffer length for TraceID") 16 | errUnmarshalTraceID = errors.New("unmarshal: invalid TraceID length") 17 | ) 18 | 19 | // TraceID is a custom data type that is used for all trace_id fields in OTLP 20 | // Protobuf messages. 21 | type TraceID [traceIDSize]byte 22 | 23 | var _ proto.Sizer = (*SpanID)(nil) 24 | 25 | // Size returns the size of the data to serialize. 26 | func (tid TraceID) Size() int { 27 | if tid.IsEmpty() { 28 | return 0 29 | } 30 | return traceIDSize 31 | } 32 | 33 | // IsEmpty returns true if id contains at leas one non-zero byte. 34 | func (tid TraceID) IsEmpty() bool { 35 | return tid == [traceIDSize]byte{} 36 | } 37 | 38 | // MarshalTo converts trace ID into a binary representation. Called by Protobuf serialization. 39 | func (tid TraceID) MarshalTo(data []byte) (n int, err error) { 40 | if tid.IsEmpty() { 41 | return 0, nil 42 | } 43 | 44 | if len(data) < traceIDSize { 45 | return 0, errMarshalTraceID 46 | } 47 | 48 | return copy(data, tid[:]), nil 49 | } 50 | 51 | // Unmarshal inflates this trace ID from binary representation. Called by Protobuf serialization. 52 | func (tid *TraceID) Unmarshal(data []byte) error { 53 | if len(data) == 0 { 54 | *tid = [traceIDSize]byte{} 55 | return nil 56 | } 57 | 58 | if len(data) != traceIDSize { 59 | return errUnmarshalTraceID 60 | } 61 | 62 | copy(tid[:], data) 63 | return nil 64 | } 65 | 66 | // MarshalJSON converts trace id into a hex string enclosed in quotes. 67 | func (tid TraceID) MarshalJSON() ([]byte, error) { 68 | if tid.IsEmpty() { 69 | return []byte(`""`), nil 70 | } 71 | return marshalJSON(tid[:]) 72 | } 73 | 74 | // UnmarshalJSON inflates trace id from hex string, possibly enclosed in quotes. 75 | // Called by Protobuf JSON deserialization. 76 | func (tid *TraceID) UnmarshalJSON(data []byte) error { 77 | *tid = [traceIDSize]byte{} 78 | return unmarshalJSON(tid[:], data) 79 | } 80 | -------------------------------------------------------------------------------- /otel-compressor/pdata/internal/generated_wrapper_byteslice.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | // Code generated by "pdata/internal/cmd/pdatagen/main.go". DO NOT EDIT. 5 | // To regenerate this file run "make genpdata". 6 | 7 | package internal 8 | 9 | type ByteSlice struct { 10 | orig *[]byte 11 | state *State 12 | } 13 | 14 | func GetOrigByteSlice(ms ByteSlice) *[]byte { 15 | return ms.orig 16 | } 17 | 18 | func GetByteSliceState(ms ByteSlice) *State { 19 | return ms.state 20 | } 21 | 22 | func NewByteSlice(orig *[]byte, state *State) ByteSlice { 23 | return ByteSlice{orig: orig, state: state} 24 | } 25 | -------------------------------------------------------------------------------- /otel-compressor/pdata/internal/generated_wrapper_float64slice.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | // Code generated by "pdata/internal/cmd/pdatagen/main.go". DO NOT EDIT. 5 | // To regenerate this file run "make genpdata". 6 | 7 | package internal 8 | 9 | type Float64Slice struct { 10 | orig *[]float64 11 | state *State 12 | } 13 | 14 | func GetOrigFloat64Slice(ms Float64Slice) *[]float64 { 15 | return ms.orig 16 | } 17 | 18 | func GetFloat64SliceState(ms Float64Slice) *State { 19 | return ms.state 20 | } 21 | 22 | func NewFloat64Slice(orig *[]float64, state *State) Float64Slice { 23 | return Float64Slice{orig: orig, state: state} 24 | } 25 | -------------------------------------------------------------------------------- /otel-compressor/pdata/internal/generated_wrapper_instrumentationscope.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | // Code generated by "pdata/internal/cmd/pdatagen/main.go". DO NOT EDIT. 5 | // To regenerate this file run "make genpdata". 6 | 7 | package internal 8 | 9 | import ( 10 | otlpcommon "go.opentelemetry.io/collector/pdata/internal/data/protogen/common/v1" 11 | ) 12 | 13 | type InstrumentationScope struct { 14 | orig *otlpcommon.InstrumentationScope 15 | state *State 16 | } 17 | 18 | func GetOrigInstrumentationScope(ms InstrumentationScope) *otlpcommon.InstrumentationScope { 19 | return ms.orig 20 | } 21 | 22 | func GetInstrumentationScopeState(ms InstrumentationScope) *State { 23 | return ms.state 24 | } 25 | 26 | func NewInstrumentationScope(orig *otlpcommon.InstrumentationScope, state *State) InstrumentationScope { 27 | return InstrumentationScope{orig: orig, state: state} 28 | } 29 | 30 | func GenerateTestInstrumentationScope() InstrumentationScope { 31 | orig := otlpcommon.InstrumentationScope{} 32 | state := StateMutable 33 | tv := NewInstrumentationScope(&orig, &state) 34 | FillTestInstrumentationScope(tv) 35 | return tv 36 | } 37 | 38 | func FillTestInstrumentationScope(tv InstrumentationScope) { 39 | tv.orig.Name = "test_name" 40 | tv.orig.Version = "test_version" 41 | FillTestMap(NewMap(&tv.orig.Attributes, tv.state)) 42 | tv.orig.DroppedAttributesCount = uint32(17) 43 | } 44 | -------------------------------------------------------------------------------- /otel-compressor/pdata/internal/generated_wrapper_resource.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | // Code generated by "pdata/internal/cmd/pdatagen/main.go". DO NOT EDIT. 5 | // To regenerate this file run "make genpdata". 6 | 7 | package internal 8 | 9 | import ( 10 | otlpresource "go.opentelemetry.io/collector/pdata/internal/data/protogen/resource/v1" 11 | ) 12 | 13 | type Resource struct { 14 | orig *otlpresource.Resource 15 | state *State 16 | } 17 | 18 | func GetOrigResource(ms Resource) *otlpresource.Resource { 19 | return ms.orig 20 | } 21 | 22 | func GetResourceState(ms Resource) *State { 23 | return ms.state 24 | } 25 | 26 | func NewResource(orig *otlpresource.Resource, state *State) Resource { 27 | return Resource{orig: orig, state: state} 28 | } 29 | 30 | func GenerateTestResource() Resource { 31 | orig := otlpresource.Resource{} 32 | state := StateMutable 33 | tv := NewResource(&orig, &state) 34 | FillTestResource(tv) 35 | return tv 36 | } 37 | 38 | func FillTestResource(tv Resource) { 39 | FillTestMap(NewMap(&tv.orig.Attributes, tv.state)) 40 | tv.orig.DroppedAttributesCount = uint32(17) 41 | } 42 | -------------------------------------------------------------------------------- /otel-compressor/pdata/internal/generated_wrapper_uint64slice.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | // Code generated by "pdata/internal/cmd/pdatagen/main.go". DO NOT EDIT. 5 | // To regenerate this file run "make genpdata". 6 | 7 | package internal 8 | 9 | type UInt64Slice struct { 10 | orig *[]uint64 11 | state *State 12 | } 13 | 14 | func GetOrigUInt64Slice(ms UInt64Slice) *[]uint64 { 15 | return ms.orig 16 | } 17 | 18 | func GetUInt64SliceState(ms UInt64Slice) *State { 19 | return ms.state 20 | } 21 | 22 | func NewUInt64Slice(orig *[]uint64, state *State) UInt64Slice { 23 | return UInt64Slice{orig: orig, state: state} 24 | } 25 | -------------------------------------------------------------------------------- /otel-compressor/pdata/internal/json/enum.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package json // import "go.opentelemetry.io/collector/pdata/internal/json" 5 | 6 | import ( 7 | jsoniter "github.com/json-iterator/go" 8 | ) 9 | 10 | // ReadEnumValue returns the enum integer value representation. Accepts both enum names and enum integer values. 11 | // See https://developers.google.com/protocol-buffers/docs/proto3#json. 12 | func ReadEnumValue(iter *jsoniter.Iterator, valueMap map[string]int32) int32 { 13 | switch iter.WhatIsNext() { 14 | case jsoniter.NumberValue: 15 | return iter.ReadInt32() 16 | case jsoniter.StringValue: 17 | val, ok := valueMap[iter.ReadString()] 18 | // Same behavior with official protbuf JSON decoder, 19 | // see https://github.com/open-telemetry/opentelemetry-proto-go/pull/81 20 | if !ok { 21 | iter.ReportError("ReadEnumValue", "unknown string value") 22 | return 0 23 | } 24 | return val 25 | default: 26 | iter.ReportError("ReadEnumValue", "unsupported value type") 27 | return 0 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /otel-compressor/pdata/internal/json/enum_test.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package json 5 | 6 | import ( 7 | "testing" 8 | 9 | jsoniter "github.com/json-iterator/go" 10 | "github.com/stretchr/testify/assert" 11 | ) 12 | 13 | func TestReadEnumValue(t *testing.T) { 14 | valueMap := map[string]int32{ 15 | "undefined": 0, 16 | "foo": 1, 17 | "bar": 2, 18 | } 19 | tests := []struct { 20 | name string 21 | jsonStr string 22 | want int32 23 | wantErr bool 24 | }{ 25 | { 26 | name: "foo string", 27 | jsonStr: "\"foo\"\n", 28 | want: 1, 29 | }, 30 | { 31 | name: "foo number", 32 | jsonStr: "1\n", 33 | want: 1, 34 | }, 35 | { 36 | name: "unknown number", 37 | jsonStr: "5\n", 38 | want: 5, 39 | }, 40 | { 41 | name: "bar string", 42 | jsonStr: "\"bar\"\n", 43 | want: 2, 44 | }, 45 | { 46 | name: "bar number", 47 | jsonStr: "2\n", 48 | want: 2, 49 | }, 50 | { 51 | name: "unknown string", 52 | jsonStr: "\"baz\"\n", 53 | wantErr: true, 54 | }, 55 | { 56 | name: "wrong type", 57 | jsonStr: "true", 58 | wantErr: true, 59 | }, 60 | } 61 | for _, tt := range tests { 62 | t.Run(tt.name, func(t *testing.T) { 63 | iter := jsoniter.ConfigFastest.BorrowIterator([]byte(tt.jsonStr)) 64 | defer jsoniter.ConfigFastest.ReturnIterator(iter) 65 | val := ReadEnumValue(iter, valueMap) 66 | if tt.wantErr { 67 | assert.Error(t, iter.Error) 68 | return 69 | } 70 | assert.NoError(t, iter.Error) 71 | assert.Equal(t, tt.want, val) 72 | }) 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /otel-compressor/pdata/internal/json/json.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package json // import "go.opentelemetry.io/collector/pdata/internal/json" 5 | 6 | import ( 7 | "io" 8 | 9 | "github.com/gogo/protobuf/jsonpb" 10 | "github.com/gogo/protobuf/proto" 11 | ) 12 | 13 | var marshaler = &jsonpb.Marshaler{ 14 | // https://github.com/open-telemetry/opentelemetry-specification/pull/2758 15 | EnumsAsInts: true, 16 | // https://github.com/open-telemetry/opentelemetry-specification/pull/2829 17 | OrigName: false, 18 | } 19 | 20 | func Marshal(out io.Writer, pb proto.Message) error { 21 | return marshaler.Marshal(out, pb) 22 | } 23 | -------------------------------------------------------------------------------- /otel-compressor/pdata/internal/json/package_test.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package json 5 | 6 | import ( 7 | "testing" 8 | 9 | "go.uber.org/goleak" 10 | ) 11 | 12 | func TestMain(m *testing.M) { 13 | goleak.VerifyTestMain(m) 14 | } 15 | -------------------------------------------------------------------------------- /otel-compressor/pdata/internal/json/resource.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package json // import "go.opentelemetry.io/collector/pdata/internal/json" 5 | 6 | import ( 7 | jsoniter "github.com/json-iterator/go" 8 | 9 | otlpresource "go.opentelemetry.io/collector/pdata/internal/data/protogen/resource/v1" 10 | ) 11 | 12 | func ReadResource(iter *jsoniter.Iterator, resource *otlpresource.Resource) { 13 | iter.ReadObjectCB(func(iter *jsoniter.Iterator, f string) bool { 14 | switch f { 15 | case "attributes": 16 | iter.ReadArrayCB(func(iter *jsoniter.Iterator) bool { 17 | resource.Attributes = append(resource.Attributes, ReadAttribute(iter)) 18 | return true 19 | }) 20 | case "droppedAttributesCount", "dropped_attributes_count": 21 | resource.DroppedAttributesCount = ReadUint32(iter) 22 | default: 23 | iter.Skip() 24 | } 25 | return true 26 | }) 27 | } 28 | -------------------------------------------------------------------------------- /otel-compressor/pdata/internal/json/resource_test.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package json // import "go.opentelemetry.io/collector/pdata/internal/json" 5 | 6 | import ( 7 | "testing" 8 | 9 | jsoniter "github.com/json-iterator/go" 10 | "github.com/stretchr/testify/assert" 11 | 12 | otlpcommon "go.opentelemetry.io/collector/pdata/internal/data/protogen/common/v1" 13 | otlpresource "go.opentelemetry.io/collector/pdata/internal/data/protogen/resource/v1" 14 | ) 15 | 16 | func TestReadResource(t *testing.T) { 17 | tests := []struct { 18 | name string 19 | jsonStr string 20 | want *otlpresource.Resource 21 | }{ 22 | { 23 | name: "resource", 24 | jsonStr: `{"attributes":[{"key":"host.name","value":{"stringValue":"testHost"}}],"dropped_attributes_count":1}`, 25 | want: &otlpresource.Resource{ 26 | Attributes: []otlpcommon.KeyValue{ 27 | { 28 | Key: "host.name", 29 | Value: otlpcommon.AnyValue{ 30 | Value: &otlpcommon.AnyValue_StringValue{ 31 | StringValue: "testHost", 32 | }, 33 | }, 34 | }, 35 | }, 36 | DroppedAttributesCount: 1, 37 | }, 38 | }, 39 | { 40 | name: "Unknown field", 41 | jsonStr: `{"attributes":[{"key":"host.name","value":{"stringValue":"testHost"}}],"test":1}`, 42 | want: &otlpresource.Resource{ 43 | Attributes: []otlpcommon.KeyValue{ 44 | { 45 | Key: "host.name", 46 | Value: otlpcommon.AnyValue{ 47 | Value: &otlpcommon.AnyValue_StringValue{ 48 | StringValue: "testHost", 49 | }, 50 | }, 51 | }, 52 | }, 53 | DroppedAttributesCount: 0, 54 | }, 55 | }, 56 | } 57 | for _, tt := range tests { 58 | t.Run(tt.name, func(t *testing.T) { 59 | iter := jsoniter.ConfigFastest.BorrowIterator([]byte(tt.jsonStr)) 60 | defer jsoniter.ConfigFastest.ReturnIterator(iter) 61 | got := &otlpresource.Resource{} 62 | ReadResource(iter, got) 63 | assert.NoError(t, iter.Error) 64 | assert.Equal(t, tt.want, got) 65 | }) 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /otel-compressor/pdata/internal/json/scope.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package json // import "go.opentelemetry.io/collector/pdata/internal/json" 5 | 6 | import ( 7 | jsoniter "github.com/json-iterator/go" 8 | 9 | otlpcommon "go.opentelemetry.io/collector/pdata/internal/data/protogen/common/v1" 10 | ) 11 | 12 | func ReadScope(iter *jsoniter.Iterator, scope *otlpcommon.InstrumentationScope) { 13 | iter.ReadObjectCB(func(iter *jsoniter.Iterator, f string) bool { 14 | switch f { 15 | case "name": 16 | scope.Name = iter.ReadString() 17 | case "version": 18 | scope.Version = iter.ReadString() 19 | case "attributes": 20 | iter.ReadArrayCB(func(iter *jsoniter.Iterator) bool { 21 | scope.Attributes = append(scope.Attributes, ReadAttribute(iter)) 22 | return true 23 | }) 24 | case "droppedAttributesCount", "dropped_attributes_count": 25 | scope.DroppedAttributesCount = ReadUint32(iter) 26 | default: 27 | iter.Skip() 28 | } 29 | return true 30 | }) 31 | } 32 | -------------------------------------------------------------------------------- /otel-compressor/pdata/internal/otlp/logs.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package otlp // import "go.opentelemetry.io/collector/pdata/internal/otlp" 5 | 6 | import ( 7 | otlplogs "go.opentelemetry.io/collector/pdata/internal/data/protogen/logs/v1" 8 | ) 9 | 10 | // MigrateLogs implements any translation needed due to deprecation in OTLP logs protocol. 11 | // Any plog.Unmarshaler implementation from OTLP (proto/json) MUST call this, and the gRPC Server implementation. 12 | func MigrateLogs(rls []*otlplogs.ResourceLogs) { 13 | for _, rl := range rls { 14 | if len(rl.ScopeLogs) == 0 { 15 | rl.ScopeLogs = rl.DeprecatedScopeLogs 16 | } 17 | rl.DeprecatedScopeLogs = nil 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /otel-compressor/pdata/internal/otlp/logs_test.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package otlp 5 | 6 | import ( 7 | "testing" 8 | 9 | "github.com/stretchr/testify/assert" 10 | 11 | otlplogs "go.opentelemetry.io/collector/pdata/internal/data/protogen/logs/v1" 12 | ) 13 | 14 | func TestDeprecatedScopeLogs(t *testing.T) { 15 | sl := new(otlplogs.ScopeLogs) 16 | rls := []*otlplogs.ResourceLogs{ 17 | { 18 | ScopeLogs: []*otlplogs.ScopeLogs{sl}, 19 | DeprecatedScopeLogs: []*otlplogs.ScopeLogs{sl}, 20 | }, 21 | { 22 | ScopeLogs: []*otlplogs.ScopeLogs{}, 23 | DeprecatedScopeLogs: []*otlplogs.ScopeLogs{sl}, 24 | }, 25 | } 26 | 27 | MigrateLogs(rls) 28 | assert.Same(t, sl, rls[0].ScopeLogs[0]) 29 | assert.Same(t, sl, rls[1].ScopeLogs[0]) 30 | assert.Nil(t, rls[0].DeprecatedScopeLogs) 31 | assert.Nil(t, rls[0].DeprecatedScopeLogs) 32 | } 33 | -------------------------------------------------------------------------------- /otel-compressor/pdata/internal/otlp/metrics.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package otlp // import "go.opentelemetry.io/collector/pdata/internal/otlp" 5 | 6 | import ( 7 | otlpmetrics "go.opentelemetry.io/collector/pdata/internal/data/protogen/metrics/v1" 8 | ) 9 | 10 | // MigrateMetrics implements any translation needed due to deprecation in OTLP metrics protocol. 11 | // Any pmetric.Unmarshaler implementation from OTLP (proto/json) MUST call this, and the gRPC Server implementation. 12 | func MigrateMetrics(rms []*otlpmetrics.ResourceMetrics) { 13 | for _, rm := range rms { 14 | if len(rm.ScopeMetrics) == 0 { 15 | rm.ScopeMetrics = rm.DeprecatedScopeMetrics 16 | } 17 | rm.DeprecatedScopeMetrics = nil 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /otel-compressor/pdata/internal/otlp/metrics_test.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package otlp 5 | 6 | import ( 7 | "testing" 8 | 9 | "github.com/stretchr/testify/assert" 10 | 11 | otlpmetrics "go.opentelemetry.io/collector/pdata/internal/data/protogen/metrics/v1" 12 | ) 13 | 14 | func TestDeprecatedScopeMetrics(t *testing.T) { 15 | sm := new(otlpmetrics.ScopeMetrics) 16 | rms := []*otlpmetrics.ResourceMetrics{ 17 | { 18 | ScopeMetrics: []*otlpmetrics.ScopeMetrics{sm}, 19 | DeprecatedScopeMetrics: []*otlpmetrics.ScopeMetrics{sm}, 20 | }, 21 | { 22 | ScopeMetrics: []*otlpmetrics.ScopeMetrics{}, 23 | DeprecatedScopeMetrics: []*otlpmetrics.ScopeMetrics{sm}, 24 | }, 25 | } 26 | 27 | MigrateMetrics(rms) 28 | assert.Same(t, sm, rms[0].ScopeMetrics[0]) 29 | assert.Same(t, sm, rms[1].ScopeMetrics[0]) 30 | assert.Nil(t, rms[0].DeprecatedScopeMetrics) 31 | assert.Nil(t, rms[0].DeprecatedScopeMetrics) 32 | } 33 | -------------------------------------------------------------------------------- /otel-compressor/pdata/internal/otlp/package_test.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package otlp 5 | 6 | import ( 7 | "testing" 8 | 9 | "go.uber.org/goleak" 10 | ) 11 | 12 | func TestMain(m *testing.M) { 13 | goleak.VerifyTestMain(m) 14 | } 15 | -------------------------------------------------------------------------------- /otel-compressor/pdata/internal/otlp/traces.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package otlp // import "go.opentelemetry.io/collector/pdata/internal/otlp" 5 | 6 | import ( 7 | otlptrace "go.opentelemetry.io/collector/pdata/internal/data/protogen/trace/v1" 8 | ) 9 | 10 | // MigrateTraces implements any translation needed due to deprecation in OTLP traces protocol. 11 | // Any ptrace.Unmarshaler implementation from OTLP (proto/json) MUST call this, and the gRPC Server implementation. 12 | func MigrateTraces(rss []*otlptrace.ResourceSpans) { 13 | // for _, rs := range rss { 14 | // if len(rs.ScopeSpans) == 0 { 15 | // rs.ScopeSpans = rs.DeprecatedScopeSpans 16 | // } 17 | // rs.DeprecatedScopeSpans = nil 18 | // } 19 | } 20 | -------------------------------------------------------------------------------- /otel-compressor/pdata/internal/otlp/traces_test.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package otlp 5 | 6 | import ( 7 | "testing" 8 | 9 | "github.com/stretchr/testify/assert" 10 | 11 | otlptrace "go.opentelemetry.io/collector/pdata/internal/data/protogen/trace/v1" 12 | ) 13 | 14 | func TestDeprecatedScopeSpans(t *testing.T) { 15 | ss := new(otlptrace.ScopeSpans) 16 | rss := []*otlptrace.ResourceSpans{ 17 | { 18 | ScopeSpans: []*otlptrace.ScopeSpans{ss}, 19 | // DeprecatedScopeSpans: []*otlptrace.ScopeSpans{ss}, 20 | }, 21 | { 22 | ScopeSpans: []*otlptrace.ScopeSpans{}, 23 | // DeprecatedScopeSpans: []*otlptrace.ScopeSpans{ss}, 24 | }, 25 | } 26 | 27 | MigrateTraces(rss) 28 | assert.Same(t, ss, rss[0].ScopeSpans[0]) 29 | assert.Same(t, ss, rss[1].ScopeSpans[0]) 30 | // assert.Nil(t, rss[0].DeprecatedScopeSpans) 31 | // assert.Nil(t, rss[0].DeprecatedScopeSpans) 32 | } 33 | -------------------------------------------------------------------------------- /otel-compressor/pdata/internal/state.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package internal // import "go.opentelemetry.io/collector/pdata/internal" 5 | 6 | // State defines an ownership state of pmetric.Metrics, plog.Logs or ptrace.Traces. 7 | type State int32 8 | 9 | const ( 10 | // StateMutable indicates that the data is exclusive to the current consumer. 11 | StateMutable State = iota 12 | 13 | // StateReadOnly indicates that the data is shared with other consumers. 14 | StateReadOnly 15 | ) 16 | 17 | // AssertMutable panics if the state is not StateMutable. 18 | func (state *State) AssertMutable() { 19 | if *state != StateMutable { 20 | panic("invalid access to shared data") 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /otel-compressor/pdata/internal/wrapper_logs.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package internal // import "go.opentelemetry.io/collector/pdata/internal" 5 | 6 | import ( 7 | otlpcollectorlog "go.opentelemetry.io/collector/pdata/internal/data/protogen/collector/logs/v1" 8 | otlplogs "go.opentelemetry.io/collector/pdata/internal/data/protogen/logs/v1" 9 | ) 10 | 11 | type Logs struct { 12 | orig *otlpcollectorlog.ExportLogsServiceRequest 13 | state *State 14 | } 15 | 16 | func GetOrigLogs(ms Logs) *otlpcollectorlog.ExportLogsServiceRequest { 17 | return ms.orig 18 | } 19 | 20 | func GetLogsState(ms Logs) *State { 21 | return ms.state 22 | } 23 | 24 | func SetLogsState(ms Logs, state State) { 25 | *ms.state = state 26 | } 27 | 28 | func NewLogs(orig *otlpcollectorlog.ExportLogsServiceRequest, state *State) Logs { 29 | return Logs{orig: orig, state: state} 30 | } 31 | 32 | // LogsToProto internal helper to convert Logs to protobuf representation. 33 | func LogsToProto(l Logs) otlplogs.LogsData { 34 | return otlplogs.LogsData{ 35 | ResourceLogs: l.orig.ResourceLogs, 36 | } 37 | } 38 | 39 | // LogsFromProto internal helper to convert protobuf representation to Logs. 40 | // This function set exclusive state assuming that it's called only once per Logs. 41 | func LogsFromProto(orig otlplogs.LogsData) Logs { 42 | state := StateMutable 43 | return NewLogs(&otlpcollectorlog.ExportLogsServiceRequest{ 44 | ResourceLogs: orig.ResourceLogs, 45 | }, &state) 46 | } 47 | -------------------------------------------------------------------------------- /otel-compressor/pdata/internal/wrapper_map.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package internal // import "go.opentelemetry.io/collector/pdata/internal" 5 | 6 | import ( 7 | otlpcommon "go.opentelemetry.io/collector/pdata/internal/data/protogen/common/v1" 8 | ) 9 | 10 | type Map struct { 11 | orig *[]otlpcommon.KeyValue 12 | state *State 13 | } 14 | 15 | func GetOrigMap(ms Map) *[]otlpcommon.KeyValue { 16 | return ms.orig 17 | } 18 | 19 | func GetMapState(ms Map) *State { 20 | return ms.state 21 | } 22 | 23 | func NewMap(orig *[]otlpcommon.KeyValue, state *State) Map { 24 | return Map{orig: orig, state: state} 25 | } 26 | 27 | func GenerateTestMap() Map { 28 | var orig []otlpcommon.KeyValue 29 | state := StateMutable 30 | ms := NewMap(&orig, &state) 31 | FillTestMap(ms) 32 | return ms 33 | } 34 | 35 | func FillTestMap(dest Map) { 36 | *dest.orig = nil 37 | *dest.orig = append(*dest.orig, otlpcommon.KeyValue{Key: "k", Value: otlpcommon.AnyValue{Value: &otlpcommon.AnyValue_StringValue{StringValue: "v"}}}) 38 | } 39 | -------------------------------------------------------------------------------- /otel-compressor/pdata/internal/wrapper_metrics.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package internal // import "go.opentelemetry.io/collector/pdata/internal" 5 | 6 | import ( 7 | otlpcollectormetrics "go.opentelemetry.io/collector/pdata/internal/data/protogen/collector/metrics/v1" 8 | otlpmetrics "go.opentelemetry.io/collector/pdata/internal/data/protogen/metrics/v1" 9 | ) 10 | 11 | type Metrics struct { 12 | orig *otlpcollectormetrics.ExportMetricsServiceRequest 13 | state *State 14 | } 15 | 16 | func GetOrigMetrics(ms Metrics) *otlpcollectormetrics.ExportMetricsServiceRequest { 17 | return ms.orig 18 | } 19 | 20 | func GetMetricsState(ms Metrics) *State { 21 | return ms.state 22 | } 23 | 24 | func SetMetricsState(ms Metrics, state State) { 25 | *ms.state = state 26 | } 27 | 28 | func NewMetrics(orig *otlpcollectormetrics.ExportMetricsServiceRequest, state *State) Metrics { 29 | return Metrics{orig: orig, state: state} 30 | } 31 | 32 | // MetricsToProto internal helper to convert Metrics to protobuf representation. 33 | func MetricsToProto(l Metrics) otlpmetrics.MetricsData { 34 | return otlpmetrics.MetricsData{ 35 | ResourceMetrics: l.orig.ResourceMetrics, 36 | } 37 | } 38 | 39 | // MetricsFromProto internal helper to convert protobuf representation to Metrics. 40 | // This function set exclusive state assuming that it's called only once per Metrics. 41 | func MetricsFromProto(orig otlpmetrics.MetricsData) Metrics { 42 | state := StateMutable 43 | return NewMetrics(&otlpcollectormetrics.ExportMetricsServiceRequest{ 44 | ResourceMetrics: orig.ResourceMetrics, 45 | }, &state) 46 | } 47 | -------------------------------------------------------------------------------- /otel-compressor/pdata/internal/wrapper_slice.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package internal // import "go.opentelemetry.io/collector/pdata/internal" 5 | 6 | import ( 7 | otlpcommon "go.opentelemetry.io/collector/pdata/internal/data/protogen/common/v1" 8 | ) 9 | 10 | type Slice struct { 11 | orig *[]otlpcommon.AnyValue 12 | state *State 13 | } 14 | 15 | func GetOrigSlice(ms Slice) *[]otlpcommon.AnyValue { 16 | return ms.orig 17 | } 18 | 19 | func GetSliceState(ms Slice) *State { 20 | return ms.state 21 | } 22 | 23 | func NewSlice(orig *[]otlpcommon.AnyValue, state *State) Slice { 24 | return Slice{orig: orig, state: state} 25 | } 26 | 27 | func GenerateTestSlice() Slice { 28 | orig := []otlpcommon.AnyValue{} 29 | state := StateMutable 30 | tv := NewSlice(&orig, &state) 31 | FillTestSlice(tv) 32 | return tv 33 | } 34 | 35 | func FillTestSlice(tv Slice) { 36 | *tv.orig = make([]otlpcommon.AnyValue, 7) 37 | for i := 0; i < 7; i++ { 38 | state := StateMutable 39 | FillTestValue(NewValue(&(*tv.orig)[i], &state)) 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /otel-compressor/pdata/internal/wrapper_traces.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package internal // import "go.opentelemetry.io/collector/pdata/internal" 5 | 6 | import ( 7 | otlpcollectortrace "go.opentelemetry.io/collector/pdata/internal/data/protogen/collector/trace/v1" 8 | otlptrace "go.opentelemetry.io/collector/pdata/internal/data/protogen/trace/v1" 9 | ) 10 | 11 | type Traces struct { 12 | orig *otlpcollectortrace.ExportTraceServiceRequest 13 | state *State 14 | } 15 | 16 | func GetOrigTraces(ms Traces) *otlpcollectortrace.ExportTraceServiceRequest { 17 | return ms.orig 18 | } 19 | 20 | func GetTracesState(ms Traces) *State { 21 | return ms.state 22 | } 23 | 24 | func SetTracesState(ms Traces, state State) { 25 | *ms.state = state 26 | } 27 | 28 | func NewTraces(orig *otlpcollectortrace.ExportTraceServiceRequest, state *State) Traces { 29 | return Traces{orig: orig, state: state} 30 | } 31 | 32 | // TracesToProto internal helper to convert Traces to protobuf representation. 33 | func TracesToProto(l Traces) otlptrace.TracesData { 34 | return otlptrace.TracesData{ 35 | ResourceSpans: l.orig.ResourceSpans, 36 | } 37 | } 38 | 39 | // TracesFromProto internal helper to convert protobuf representation to Traces. 40 | // This function set exclusive state assuming that it's called only once per Traces. 41 | func TracesFromProto(orig otlptrace.TracesData) Traces { 42 | state := StateMutable 43 | return NewTraces(&otlpcollectortrace.ExportTraceServiceRequest{ 44 | ResourceSpans: orig.ResourceSpans, 45 | }, &state) 46 | } 47 | -------------------------------------------------------------------------------- /otel-compressor/pdata/internal/wrapper_tracestate.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package internal // import "go.opentelemetry.io/collector/pdata/internal" 5 | 6 | type TraceState struct { 7 | orig *string 8 | state *State 9 | } 10 | 11 | func GetOrigTraceState(ms TraceState) *string { 12 | return ms.orig 13 | } 14 | 15 | func GetTraceStateState(ms TraceState) *State { 16 | return ms.state 17 | } 18 | 19 | func NewTraceState(orig *string, state *State) TraceState { 20 | return TraceState{orig: orig, state: state} 21 | } 22 | 23 | func GenerateTestTraceState() TraceState { 24 | var orig string 25 | state := StateMutable 26 | ms := NewTraceState(&orig, &state) 27 | FillTestTraceState(ms) 28 | return ms 29 | } 30 | 31 | func FillTestTraceState(dest TraceState) { 32 | *dest.orig = "rojo=00f067aa0ba902b7" 33 | } 34 | -------------------------------------------------------------------------------- /otel-compressor/pdata/internal/wrapper_value.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package internal // import "go.opentelemetry.io/collector/pdata/internal" 5 | 6 | import ( 7 | otlpcommon "go.opentelemetry.io/collector/pdata/internal/data/protogen/common/v1" 8 | ) 9 | 10 | type Value struct { 11 | orig *otlpcommon.AnyValue 12 | state *State 13 | } 14 | 15 | func GetOrigValue(ms Value) *otlpcommon.AnyValue { 16 | return ms.orig 17 | } 18 | 19 | func GetValueState(ms Value) *State { 20 | return ms.state 21 | } 22 | 23 | func NewValue(orig *otlpcommon.AnyValue, state *State) Value { 24 | return Value{orig: orig, state: state} 25 | } 26 | 27 | func FillTestValue(dest Value) { 28 | dest.orig.Value = &otlpcommon.AnyValue_StringValue{StringValue: "v"} 29 | } 30 | 31 | func GenerateTestValue() Value { 32 | var orig otlpcommon.AnyValue 33 | state := StateMutable 34 | ms := NewValue(&orig, &state) 35 | FillTestValue(ms) 36 | return ms 37 | } 38 | -------------------------------------------------------------------------------- /otel-compressor/pdata/pcommon/generated_byteslice_test.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | // Code generated by "pdata/internal/cmd/pdatagen/main.go". DO NOT EDIT. 5 | // To regenerate this file run "make genpdata". 6 | 7 | package pcommon 8 | 9 | import ( 10 | "testing" 11 | 12 | "github.com/stretchr/testify/assert" 13 | 14 | "go.opentelemetry.io/collector/pdata/internal" 15 | ) 16 | 17 | func TestNewByteSlice(t *testing.T) { 18 | ms := NewByteSlice() 19 | assert.Equal(t, 0, ms.Len()) 20 | ms.FromRaw([]byte{1, 2, 3}) 21 | assert.Equal(t, 3, ms.Len()) 22 | assert.Equal(t, []byte{1, 2, 3}, ms.AsRaw()) 23 | ms.SetAt(1, byte(5)) 24 | assert.Equal(t, []byte{1, 5, 3}, ms.AsRaw()) 25 | ms.FromRaw([]byte{3}) 26 | assert.Equal(t, 1, ms.Len()) 27 | assert.Equal(t, byte(3), ms.At(0)) 28 | 29 | cp := NewByteSlice() 30 | ms.CopyTo(cp) 31 | ms.SetAt(0, byte(2)) 32 | assert.Equal(t, byte(2), ms.At(0)) 33 | assert.Equal(t, byte(3), cp.At(0)) 34 | ms.CopyTo(cp) 35 | assert.Equal(t, byte(2), cp.At(0)) 36 | 37 | mv := NewByteSlice() 38 | ms.MoveTo(mv) 39 | assert.Equal(t, 0, ms.Len()) 40 | assert.Equal(t, 1, mv.Len()) 41 | assert.Equal(t, byte(2), mv.At(0)) 42 | ms.FromRaw([]byte{1, 2, 3}) 43 | ms.MoveTo(mv) 44 | assert.Equal(t, 3, mv.Len()) 45 | assert.Equal(t, byte(1), mv.At(0)) 46 | } 47 | 48 | func TestByteSliceReadOnly(t *testing.T) { 49 | raw := []byte{1, 2, 3} 50 | state := internal.StateReadOnly 51 | ms := ByteSlice(internal.NewByteSlice(&raw, &state)) 52 | 53 | assert.Equal(t, 3, ms.Len()) 54 | assert.Equal(t, byte(1), ms.At(0)) 55 | assert.Panics(t, func() { ms.Append(1) }) 56 | assert.Panics(t, func() { ms.EnsureCapacity(2) }) 57 | assert.Equal(t, raw, ms.AsRaw()) 58 | assert.Panics(t, func() { ms.FromRaw(raw) }) 59 | 60 | ms2 := NewByteSlice() 61 | ms.CopyTo(ms2) 62 | assert.Equal(t, ms.AsRaw(), ms2.AsRaw()) 63 | assert.Panics(t, func() { ms2.CopyTo(ms) }) 64 | 65 | assert.Panics(t, func() { ms.MoveTo(ms2) }) 66 | assert.Panics(t, func() { ms2.MoveTo(ms) }) 67 | } 68 | 69 | func TestByteSliceAppend(t *testing.T) { 70 | ms := NewByteSlice() 71 | ms.FromRaw([]byte{1, 2, 3}) 72 | ms.Append(4, 5) 73 | assert.Equal(t, 5, ms.Len()) 74 | assert.Equal(t, byte(5), ms.At(4)) 75 | } 76 | 77 | func TestByteSliceEnsureCapacity(t *testing.T) { 78 | ms := NewByteSlice() 79 | ms.EnsureCapacity(4) 80 | assert.Equal(t, 4, cap(*ms.getOrig())) 81 | ms.EnsureCapacity(2) 82 | assert.Equal(t, 4, cap(*ms.getOrig())) 83 | } 84 | -------------------------------------------------------------------------------- /otel-compressor/pdata/pcommon/generated_float64slice_test.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | // Code generated by "pdata/internal/cmd/pdatagen/main.go". DO NOT EDIT. 5 | // To regenerate this file run "make genpdata". 6 | 7 | package pcommon 8 | 9 | import ( 10 | "testing" 11 | 12 | "github.com/stretchr/testify/assert" 13 | 14 | "go.opentelemetry.io/collector/pdata/internal" 15 | ) 16 | 17 | func TestNewFloat64Slice(t *testing.T) { 18 | ms := NewFloat64Slice() 19 | assert.Equal(t, 0, ms.Len()) 20 | ms.FromRaw([]float64{1, 2, 3}) 21 | assert.Equal(t, 3, ms.Len()) 22 | assert.Equal(t, []float64{1, 2, 3}, ms.AsRaw()) 23 | ms.SetAt(1, float64(5)) 24 | assert.Equal(t, []float64{1, 5, 3}, ms.AsRaw()) 25 | ms.FromRaw([]float64{3}) 26 | assert.Equal(t, 1, ms.Len()) 27 | assert.Equal(t, float64(3), ms.At(0)) 28 | 29 | cp := NewFloat64Slice() 30 | ms.CopyTo(cp) 31 | ms.SetAt(0, float64(2)) 32 | assert.Equal(t, float64(2), ms.At(0)) 33 | assert.Equal(t, float64(3), cp.At(0)) 34 | ms.CopyTo(cp) 35 | assert.Equal(t, float64(2), cp.At(0)) 36 | 37 | mv := NewFloat64Slice() 38 | ms.MoveTo(mv) 39 | assert.Equal(t, 0, ms.Len()) 40 | assert.Equal(t, 1, mv.Len()) 41 | assert.Equal(t, float64(2), mv.At(0)) 42 | ms.FromRaw([]float64{1, 2, 3}) 43 | ms.MoveTo(mv) 44 | assert.Equal(t, 3, mv.Len()) 45 | assert.Equal(t, float64(1), mv.At(0)) 46 | } 47 | 48 | func TestFloat64SliceReadOnly(t *testing.T) { 49 | raw := []float64{1, 2, 3} 50 | state := internal.StateReadOnly 51 | ms := Float64Slice(internal.NewFloat64Slice(&raw, &state)) 52 | 53 | assert.Equal(t, 3, ms.Len()) 54 | assert.Equal(t, float64(1), ms.At(0)) 55 | assert.Panics(t, func() { ms.Append(1) }) 56 | assert.Panics(t, func() { ms.EnsureCapacity(2) }) 57 | assert.Equal(t, raw, ms.AsRaw()) 58 | assert.Panics(t, func() { ms.FromRaw(raw) }) 59 | 60 | ms2 := NewFloat64Slice() 61 | ms.CopyTo(ms2) 62 | assert.Equal(t, ms.AsRaw(), ms2.AsRaw()) 63 | assert.Panics(t, func() { ms2.CopyTo(ms) }) 64 | 65 | assert.Panics(t, func() { ms.MoveTo(ms2) }) 66 | assert.Panics(t, func() { ms2.MoveTo(ms) }) 67 | } 68 | 69 | func TestFloat64SliceAppend(t *testing.T) { 70 | ms := NewFloat64Slice() 71 | ms.FromRaw([]float64{1, 2, 3}) 72 | ms.Append(4, 5) 73 | assert.Equal(t, 5, ms.Len()) 74 | assert.Equal(t, float64(5), ms.At(4)) 75 | } 76 | 77 | func TestFloat64SliceEnsureCapacity(t *testing.T) { 78 | ms := NewFloat64Slice() 79 | ms.EnsureCapacity(4) 80 | assert.Equal(t, 4, cap(*ms.getOrig())) 81 | ms.EnsureCapacity(2) 82 | assert.Equal(t, 4, cap(*ms.getOrig())) 83 | } 84 | -------------------------------------------------------------------------------- /otel-compressor/pdata/pcommon/generated_resource_test.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | // Code generated by "pdata/internal/cmd/pdatagen/main.go". DO NOT EDIT. 5 | // To regenerate this file run "make genpdata". 6 | 7 | package pcommon 8 | 9 | import ( 10 | "testing" 11 | 12 | "github.com/stretchr/testify/assert" 13 | 14 | "go.opentelemetry.io/collector/pdata/internal" 15 | otlpresource "go.opentelemetry.io/collector/pdata/internal/data/protogen/resource/v1" 16 | ) 17 | 18 | func TestResource_MoveTo(t *testing.T) { 19 | ms := Resource(internal.GenerateTestResource()) 20 | dest := NewResource() 21 | ms.MoveTo(dest) 22 | assert.Equal(t, NewResource(), ms) 23 | assert.Equal(t, Resource(internal.GenerateTestResource()), dest) 24 | sharedState := internal.StateReadOnly 25 | assert.Panics(t, func() { ms.MoveTo(newResource(&otlpresource.Resource{}, &sharedState)) }) 26 | assert.Panics(t, func() { newResource(&otlpresource.Resource{}, &sharedState).MoveTo(dest) }) 27 | } 28 | 29 | func TestResource_CopyTo(t *testing.T) { 30 | ms := NewResource() 31 | orig := NewResource() 32 | orig.CopyTo(ms) 33 | assert.Equal(t, orig, ms) 34 | orig = Resource(internal.GenerateTestResource()) 35 | orig.CopyTo(ms) 36 | assert.Equal(t, orig, ms) 37 | sharedState := internal.StateReadOnly 38 | assert.Panics(t, func() { ms.CopyTo(newResource(&otlpresource.Resource{}, &sharedState)) }) 39 | } 40 | 41 | func TestResource_Attributes(t *testing.T) { 42 | ms := NewResource() 43 | assert.Equal(t, NewMap(), ms.Attributes()) 44 | internal.FillTestMap(internal.Map(ms.Attributes())) 45 | assert.Equal(t, Map(internal.GenerateTestMap()), ms.Attributes()) 46 | } 47 | 48 | func TestResource_DroppedAttributesCount(t *testing.T) { 49 | ms := NewResource() 50 | assert.Equal(t, uint32(0), ms.DroppedAttributesCount()) 51 | ms.SetDroppedAttributesCount(uint32(17)) 52 | assert.Equal(t, uint32(17), ms.DroppedAttributesCount()) 53 | sharedState := internal.StateReadOnly 54 | assert.Panics(t, func() { newResource(&otlpresource.Resource{}, &sharedState).SetDroppedAttributesCount(uint32(17)) }) 55 | } 56 | -------------------------------------------------------------------------------- /otel-compressor/pdata/pcommon/generated_uint64slice_test.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | // Code generated by "pdata/internal/cmd/pdatagen/main.go". DO NOT EDIT. 5 | // To regenerate this file run "make genpdata". 6 | 7 | package pcommon 8 | 9 | import ( 10 | "testing" 11 | 12 | "github.com/stretchr/testify/assert" 13 | 14 | "go.opentelemetry.io/collector/pdata/internal" 15 | ) 16 | 17 | func TestNewUInt64Slice(t *testing.T) { 18 | ms := NewUInt64Slice() 19 | assert.Equal(t, 0, ms.Len()) 20 | ms.FromRaw([]uint64{1, 2, 3}) 21 | assert.Equal(t, 3, ms.Len()) 22 | assert.Equal(t, []uint64{1, 2, 3}, ms.AsRaw()) 23 | ms.SetAt(1, uint64(5)) 24 | assert.Equal(t, []uint64{1, 5, 3}, ms.AsRaw()) 25 | ms.FromRaw([]uint64{3}) 26 | assert.Equal(t, 1, ms.Len()) 27 | assert.Equal(t, uint64(3), ms.At(0)) 28 | 29 | cp := NewUInt64Slice() 30 | ms.CopyTo(cp) 31 | ms.SetAt(0, uint64(2)) 32 | assert.Equal(t, uint64(2), ms.At(0)) 33 | assert.Equal(t, uint64(3), cp.At(0)) 34 | ms.CopyTo(cp) 35 | assert.Equal(t, uint64(2), cp.At(0)) 36 | 37 | mv := NewUInt64Slice() 38 | ms.MoveTo(mv) 39 | assert.Equal(t, 0, ms.Len()) 40 | assert.Equal(t, 1, mv.Len()) 41 | assert.Equal(t, uint64(2), mv.At(0)) 42 | ms.FromRaw([]uint64{1, 2, 3}) 43 | ms.MoveTo(mv) 44 | assert.Equal(t, 3, mv.Len()) 45 | assert.Equal(t, uint64(1), mv.At(0)) 46 | } 47 | 48 | func TestUInt64SliceReadOnly(t *testing.T) { 49 | raw := []uint64{1, 2, 3} 50 | state := internal.StateReadOnly 51 | ms := UInt64Slice(internal.NewUInt64Slice(&raw, &state)) 52 | 53 | assert.Equal(t, 3, ms.Len()) 54 | assert.Equal(t, uint64(1), ms.At(0)) 55 | assert.Panics(t, func() { ms.Append(1) }) 56 | assert.Panics(t, func() { ms.EnsureCapacity(2) }) 57 | assert.Equal(t, raw, ms.AsRaw()) 58 | assert.Panics(t, func() { ms.FromRaw(raw) }) 59 | 60 | ms2 := NewUInt64Slice() 61 | ms.CopyTo(ms2) 62 | assert.Equal(t, ms.AsRaw(), ms2.AsRaw()) 63 | assert.Panics(t, func() { ms2.CopyTo(ms) }) 64 | 65 | assert.Panics(t, func() { ms.MoveTo(ms2) }) 66 | assert.Panics(t, func() { ms2.MoveTo(ms) }) 67 | } 68 | 69 | func TestUInt64SliceAppend(t *testing.T) { 70 | ms := NewUInt64Slice() 71 | ms.FromRaw([]uint64{1, 2, 3}) 72 | ms.Append(4, 5) 73 | assert.Equal(t, 5, ms.Len()) 74 | assert.Equal(t, uint64(5), ms.At(4)) 75 | } 76 | 77 | func TestUInt64SliceEnsureCapacity(t *testing.T) { 78 | ms := NewUInt64Slice() 79 | ms.EnsureCapacity(4) 80 | assert.Equal(t, 4, cap(*ms.getOrig())) 81 | ms.EnsureCapacity(2) 82 | assert.Equal(t, 4, cap(*ms.getOrig())) 83 | } 84 | -------------------------------------------------------------------------------- /otel-compressor/pdata/pcommon/package_test.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package pcommon 5 | 6 | import ( 7 | "testing" 8 | 9 | "go.uber.org/goleak" 10 | ) 11 | 12 | func TestMain(m *testing.M) { 13 | goleak.VerifyTestMain(m) 14 | } 15 | -------------------------------------------------------------------------------- /otel-compressor/pdata/pcommon/spanid.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package pcommon // import "go.opentelemetry.io/collector/pdata/pcommon" 5 | import ( 6 | "encoding/hex" 7 | 8 | "go.opentelemetry.io/collector/pdata/internal/data" 9 | ) 10 | 11 | var emptySpanID = SpanID([8]byte{}) 12 | 13 | // SpanID is span identifier. 14 | type SpanID [8]byte 15 | 16 | // NewSpanIDEmpty returns a new empty (all zero bytes) SpanID. 17 | func NewSpanIDEmpty() SpanID { 18 | return emptySpanID 19 | } 20 | 21 | // String returns string representation of the SpanID. 22 | // 23 | // Important: Don't rely on this method to get a string identifier of SpanID, 24 | // Use hex.EncodeToString explicitly instead. 25 | // This method meant to implement Stringer interface for display purposes only. 26 | func (ms SpanID) String() string { 27 | if ms.IsEmpty() { 28 | return "" 29 | } 30 | return hex.EncodeToString(ms[:]) 31 | } 32 | 33 | // IsEmpty returns true if id doesn't contain at least one non-zero byte. 34 | func (ms SpanID) IsEmpty() bool { 35 | return data.SpanID(ms).IsEmpty() 36 | } 37 | -------------------------------------------------------------------------------- /otel-compressor/pdata/pcommon/spanid_test.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package pcommon 5 | 6 | import ( 7 | "testing" 8 | 9 | "github.com/stretchr/testify/assert" 10 | ) 11 | 12 | func TestSpanID(t *testing.T) { 13 | sid := SpanID([8]byte{1, 2, 3, 4, 4, 3, 2, 1}) 14 | assert.Equal(t, [8]byte{1, 2, 3, 4, 4, 3, 2, 1}, [8]byte(sid)) 15 | assert.False(t, sid.IsEmpty()) 16 | } 17 | 18 | func TestNewSpanIDEmpty(t *testing.T) { 19 | sid := NewSpanIDEmpty() 20 | assert.Equal(t, [8]byte{}, [8]byte(sid)) 21 | assert.True(t, sid.IsEmpty()) 22 | } 23 | 24 | func TestSpanIDString(t *testing.T) { 25 | sid := SpanID([8]byte{}) 26 | assert.Equal(t, "", sid.String()) 27 | 28 | sid = SpanID([8]byte{0x12, 0x23, 0xAD, 0x12, 0x23, 0xAD, 0x12, 0x23}) 29 | assert.Equal(t, "1223ad1223ad1223", sid.String()) 30 | } 31 | 32 | func TestSpanIDImmutable(t *testing.T) { 33 | initialBytes := [8]byte{0x12, 0x23, 0xAD, 0x12, 0x23, 0xAD, 0x12, 0x23} 34 | sid := SpanID(initialBytes) 35 | assert.Equal(t, SpanID(initialBytes), sid) 36 | 37 | // Get the bytes and try to mutate. 38 | sid[4] = 0x89 39 | 40 | // Does not change the already created SpanID. 41 | assert.NotEqual(t, SpanID(initialBytes), sid) 42 | } 43 | -------------------------------------------------------------------------------- /otel-compressor/pdata/pcommon/timestamp.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package pcommon // import "go.opentelemetry.io/collector/pdata/pcommon" 5 | 6 | import ( 7 | "time" 8 | ) 9 | 10 | // Timestamp is a time specified as UNIX Epoch time in nanoseconds since 11 | // 1970-01-01 00:00:00 +0000 UTC. 12 | type Timestamp uint64 13 | 14 | // NewTimestampFromTime constructs a new Timestamp from the provided time.Time. 15 | func NewTimestampFromTime(t time.Time) Timestamp { 16 | return Timestamp(uint64(t.UnixNano())) 17 | } 18 | 19 | // AsTime converts this to a time.Time. 20 | func (ts Timestamp) AsTime() time.Time { 21 | return time.Unix(0, int64(ts)).UTC() 22 | } 23 | 24 | // String returns the string representation of this in UTC. 25 | func (ts Timestamp) String() string { 26 | return ts.AsTime().String() 27 | } 28 | -------------------------------------------------------------------------------- /otel-compressor/pdata/pcommon/timestamp_test.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package pcommon 5 | 6 | import ( 7 | "testing" 8 | "time" 9 | 10 | "github.com/stretchr/testify/assert" 11 | ) 12 | 13 | func TestUnixNanosConverters(t *testing.T) { 14 | t1 := time.Date(2020, 03, 24, 1, 13, 23, 789, time.UTC) 15 | tun := Timestamp(t1.UnixNano()) 16 | 17 | assert.EqualValues(t, uint64(1585012403000000789), tun) 18 | assert.EqualValues(t, tun, NewTimestampFromTime(t1)) 19 | assert.EqualValues(t, t1, NewTimestampFromTime(t1).AsTime()) 20 | assert.Equal(t, "2020-03-24 01:13:23.000000789 +0000 UTC", t1.String()) 21 | } 22 | 23 | func TestZeroTimestamp(t *testing.T) { 24 | assert.Equal(t, time.Unix(0, 0).UTC(), Timestamp(0).AsTime()) 25 | assert.Zero(t, NewTimestampFromTime(time.Unix(0, 0).UTC())) 26 | assert.Equal(t, "1970-01-01 00:00:00 +0000 UTC", Timestamp(0).String()) 27 | } 28 | -------------------------------------------------------------------------------- /otel-compressor/pdata/pcommon/trace_state.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package pcommon // import "go.opentelemetry.io/collector/pdata/pcommon" 5 | 6 | import ( 7 | "go.opentelemetry.io/collector/pdata/internal" 8 | ) 9 | 10 | // TraceState represents the trace state from the w3c-trace-context. 11 | // 12 | // Must use NewTraceState function to create new instances. 13 | // Important: zero-initialized instance is not valid for use. 14 | type TraceState internal.TraceState 15 | 16 | func NewTraceState() TraceState { 17 | state := internal.StateMutable 18 | return TraceState(internal.NewTraceState(new(string), &state)) 19 | } 20 | 21 | func (ms TraceState) getOrig() *string { 22 | return internal.GetOrigTraceState(internal.TraceState(ms)) 23 | } 24 | 25 | func (ms TraceState) getState() *internal.State { 26 | return internal.GetTraceStateState(internal.TraceState(ms)) 27 | } 28 | 29 | // AsRaw returns the string representation of the tracestate in w3c-trace-context format: https://www.w3.org/TR/trace-context/#tracestate-header 30 | func (ms TraceState) AsRaw() string { 31 | return *ms.getOrig() 32 | } 33 | 34 | // FromRaw copies the string representation in w3c-trace-context format of the tracestate into this TraceState. 35 | func (ms TraceState) FromRaw(v string) { 36 | ms.getState().AssertMutable() 37 | *ms.getOrig() = v 38 | } 39 | 40 | // MoveTo moves the TraceState instance overriding the destination 41 | // and resetting the current instance to its zero value. 42 | func (ms TraceState) MoveTo(dest TraceState) { 43 | ms.getState().AssertMutable() 44 | dest.getState().AssertMutable() 45 | *dest.getOrig() = *ms.getOrig() 46 | *ms.getOrig() = "" 47 | } 48 | 49 | // CopyTo copies the TraceState instance overriding the destination. 50 | func (ms TraceState) CopyTo(dest TraceState) { 51 | dest.getState().AssertMutable() 52 | *dest.getOrig() = *ms.getOrig() 53 | } 54 | -------------------------------------------------------------------------------- /otel-compressor/pdata/pcommon/trace_state_test.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package pcommon 5 | 6 | import ( 7 | "testing" 8 | 9 | "github.com/stretchr/testify/assert" 10 | 11 | "go.opentelemetry.io/collector/pdata/internal" 12 | ) 13 | 14 | func TestTraceState_MoveTo(t *testing.T) { 15 | ms := TraceState(internal.GenerateTestTraceState()) 16 | dest := NewTraceState() 17 | ms.MoveTo(dest) 18 | assert.Equal(t, NewTraceState(), ms) 19 | assert.Equal(t, TraceState(internal.GenerateTestTraceState()), dest) 20 | } 21 | 22 | func TestTraceState_CopyTo(t *testing.T) { 23 | ms := NewTraceState() 24 | orig := NewTraceState() 25 | orig.CopyTo(ms) 26 | assert.Equal(t, orig, ms) 27 | orig = TraceState(internal.GenerateTestTraceState()) 28 | orig.CopyTo(ms) 29 | assert.Equal(t, orig, ms) 30 | } 31 | 32 | func TestTraceState_FromRaw_AsRaw(t *testing.T) { 33 | ms := NewTraceState() 34 | assert.Equal(t, "", ms.AsRaw()) 35 | ms.FromRaw("congo=t61rcWkgMzE") 36 | assert.Equal(t, "congo=t61rcWkgMzE", ms.AsRaw()) 37 | } 38 | 39 | func TestInvalidTraceState(t *testing.T) { 40 | v := TraceState{} 41 | 42 | assert.Panics(t, func() { v.AsRaw() }) 43 | assert.Panics(t, func() { v.FromRaw("") }) 44 | assert.Panics(t, func() { v.MoveTo(TraceState{}) }) 45 | assert.Panics(t, func() { v.CopyTo(TraceState{}) }) 46 | } 47 | -------------------------------------------------------------------------------- /otel-compressor/pdata/pcommon/traceid.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package pcommon // import "go.opentelemetry.io/collector/pdata/pcommon" 5 | 6 | import ( 7 | "encoding/hex" 8 | 9 | "go.opentelemetry.io/collector/pdata/internal/data" 10 | ) 11 | 12 | var emptyTraceID = TraceID([16]byte{}) 13 | 14 | // TraceID is a trace identifier. 15 | type TraceID [16]byte 16 | 17 | // NewTraceIDEmpty returns a new empty (all zero bytes) TraceID. 18 | func NewTraceIDEmpty() TraceID { 19 | return emptyTraceID 20 | } 21 | 22 | // String returns string representation of the TraceID. 23 | // 24 | // Important: Don't rely on this method to get a string identifier of TraceID. 25 | // Use hex.EncodeToString explicitly instead. 26 | // This method meant to implement Stringer interface for display purposes only. 27 | func (ms TraceID) String() string { 28 | if ms.IsEmpty() { 29 | return "" 30 | } 31 | return hex.EncodeToString(ms[:]) 32 | } 33 | 34 | // IsEmpty returns true if id doesn't contain at least one non-zero byte. 35 | func (ms TraceID) IsEmpty() bool { 36 | return data.TraceID(ms).IsEmpty() 37 | } 38 | -------------------------------------------------------------------------------- /otel-compressor/pdata/pcommon/traceid_test.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package pcommon 5 | 6 | import ( 7 | "testing" 8 | 9 | "github.com/stretchr/testify/assert" 10 | ) 11 | 12 | func TestTraceID(t *testing.T) { 13 | tid := TraceID([16]byte{1, 2, 3, 4, 5, 6, 7, 8, 8, 7, 6, 5, 4, 3, 2, 1}) 14 | assert.Equal(t, [16]byte{1, 2, 3, 4, 5, 6, 7, 8, 8, 7, 6, 5, 4, 3, 2, 1}, [16]byte(tid)) 15 | assert.False(t, tid.IsEmpty()) 16 | } 17 | 18 | func TestNewTraceIDEmpty(t *testing.T) { 19 | tid := NewTraceIDEmpty() 20 | assert.Equal(t, [16]byte{}, [16]byte(tid)) 21 | assert.True(t, tid.IsEmpty()) 22 | } 23 | 24 | func TestTraceIDString(t *testing.T) { 25 | tid := TraceID([16]byte{}) 26 | assert.Equal(t, "", tid.String()) 27 | 28 | tid = [16]byte{0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78} 29 | assert.Equal(t, "12345678123456781234567812345678", tid.String()) 30 | } 31 | 32 | func TestTraceIDImmutable(t *testing.T) { 33 | initialBytes := [16]byte{0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78} 34 | tid := TraceID(initialBytes) 35 | assert.Equal(t, TraceID(initialBytes), tid) 36 | 37 | // Get the bytes and try to mutate. 38 | tid[4] = 0x23 39 | 40 | // Does not change the already created TraceID. 41 | assert.NotEqual(t, TraceID(initialBytes), tid) 42 | } 43 | -------------------------------------------------------------------------------- /otel-compressor/pdata/plog/encoding.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package plog // import "go.opentelemetry.io/collector/pdata/plog" 5 | 6 | // MarshalSizer is the interface that groups the basic Marshal and Size methods 7 | type MarshalSizer interface { 8 | Marshaler 9 | Sizer 10 | } 11 | 12 | // Marshaler marshals pdata.Logs into bytes. 13 | type Marshaler interface { 14 | // MarshalLogs the given pdata.Logs into bytes. 15 | // If the error is not nil, the returned bytes slice cannot be used. 16 | MarshalLogs(ld Logs) ([]byte, error) 17 | } 18 | 19 | // Unmarshaler unmarshalls bytes into pdata.Logs. 20 | type Unmarshaler interface { 21 | // UnmarshalLogs the given bytes into pdata.Logs. 22 | // If the error is not nil, the returned pdata.Logs cannot be used. 23 | UnmarshalLogs(buf []byte) (Logs, error) 24 | } 25 | 26 | // Sizer is an optional interface implemented by the Marshaler, 27 | // that calculates the size of a marshaled Logs. 28 | type Sizer interface { 29 | // LogsSize returns the size in bytes of a marshaled Logs. 30 | LogsSize(ld Logs) int 31 | } 32 | -------------------------------------------------------------------------------- /otel-compressor/pdata/plog/generated_scopelogs.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | // Code generated by "pdata/internal/cmd/pdatagen/main.go". DO NOT EDIT. 5 | // To regenerate this file run "make genpdata". 6 | 7 | package plog 8 | 9 | import ( 10 | "go.opentelemetry.io/collector/pdata/internal" 11 | otlplogs "go.opentelemetry.io/collector/pdata/internal/data/protogen/logs/v1" 12 | "go.opentelemetry.io/collector/pdata/pcommon" 13 | ) 14 | 15 | // ScopeLogs is a collection of logs from a LibraryInstrumentation. 16 | // 17 | // This is a reference type, if passed by value and callee modifies it the 18 | // caller will see the modification. 19 | // 20 | // Must use NewScopeLogs function to create new instances. 21 | // Important: zero-initialized instance is not valid for use. 22 | type ScopeLogs struct { 23 | orig *otlplogs.ScopeLogs 24 | state *internal.State 25 | } 26 | 27 | func newScopeLogs(orig *otlplogs.ScopeLogs, state *internal.State) ScopeLogs { 28 | return ScopeLogs{orig: orig, state: state} 29 | } 30 | 31 | // NewScopeLogs creates a new empty ScopeLogs. 32 | // 33 | // This must be used only in testing code. Users should use "AppendEmpty" when part of a Slice, 34 | // OR directly access the member if this is embedded in another struct. 35 | func NewScopeLogs() ScopeLogs { 36 | state := internal.StateMutable 37 | return newScopeLogs(&otlplogs.ScopeLogs{}, &state) 38 | } 39 | 40 | // MoveTo moves all properties from the current struct overriding the destination and 41 | // resetting the current instance to its zero value 42 | func (ms ScopeLogs) MoveTo(dest ScopeLogs) { 43 | ms.state.AssertMutable() 44 | dest.state.AssertMutable() 45 | *dest.orig = *ms.orig 46 | *ms.orig = otlplogs.ScopeLogs{} 47 | } 48 | 49 | // Scope returns the scope associated with this ScopeLogs. 50 | func (ms ScopeLogs) Scope() pcommon.InstrumentationScope { 51 | return pcommon.InstrumentationScope(internal.NewInstrumentationScope(&ms.orig.Scope, ms.state)) 52 | } 53 | 54 | // SchemaUrl returns the schemaurl associated with this ScopeLogs. 55 | func (ms ScopeLogs) SchemaUrl() string { 56 | return ms.orig.SchemaUrl 57 | } 58 | 59 | // SetSchemaUrl replaces the schemaurl associated with this ScopeLogs. 60 | func (ms ScopeLogs) SetSchemaUrl(v string) { 61 | ms.state.AssertMutable() 62 | ms.orig.SchemaUrl = v 63 | } 64 | 65 | // LogRecords returns the LogRecords associated with this ScopeLogs. 66 | func (ms ScopeLogs) LogRecords() LogRecordSlice { 67 | return newLogRecordSlice(&ms.orig.LogRecords, ms.state) 68 | } 69 | 70 | // CopyTo copies all properties from the current struct overriding the destination. 71 | func (ms ScopeLogs) CopyTo(dest ScopeLogs) { 72 | dest.state.AssertMutable() 73 | ms.Scope().CopyTo(dest.Scope()) 74 | dest.SetSchemaUrl(ms.SchemaUrl()) 75 | ms.LogRecords().CopyTo(dest.LogRecords()) 76 | } 77 | -------------------------------------------------------------------------------- /otel-compressor/pdata/plog/generated_scopelogs_test.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | // Code generated by "pdata/internal/cmd/pdatagen/main.go". DO NOT EDIT. 5 | // To regenerate this file run "make genpdata". 6 | 7 | package plog 8 | 9 | import ( 10 | "testing" 11 | 12 | "github.com/stretchr/testify/assert" 13 | 14 | "go.opentelemetry.io/collector/pdata/internal" 15 | otlplogs "go.opentelemetry.io/collector/pdata/internal/data/protogen/logs/v1" 16 | "go.opentelemetry.io/collector/pdata/pcommon" 17 | ) 18 | 19 | func TestScopeLogs_MoveTo(t *testing.T) { 20 | ms := generateTestScopeLogs() 21 | dest := NewScopeLogs() 22 | ms.MoveTo(dest) 23 | assert.Equal(t, NewScopeLogs(), ms) 24 | assert.Equal(t, generateTestScopeLogs(), dest) 25 | sharedState := internal.StateReadOnly 26 | assert.Panics(t, func() { ms.MoveTo(newScopeLogs(&otlplogs.ScopeLogs{}, &sharedState)) }) 27 | assert.Panics(t, func() { newScopeLogs(&otlplogs.ScopeLogs{}, &sharedState).MoveTo(dest) }) 28 | } 29 | 30 | func TestScopeLogs_CopyTo(t *testing.T) { 31 | ms := NewScopeLogs() 32 | orig := NewScopeLogs() 33 | orig.CopyTo(ms) 34 | assert.Equal(t, orig, ms) 35 | orig = generateTestScopeLogs() 36 | orig.CopyTo(ms) 37 | assert.Equal(t, orig, ms) 38 | sharedState := internal.StateReadOnly 39 | assert.Panics(t, func() { ms.CopyTo(newScopeLogs(&otlplogs.ScopeLogs{}, &sharedState)) }) 40 | } 41 | 42 | func TestScopeLogs_Scope(t *testing.T) { 43 | ms := NewScopeLogs() 44 | internal.FillTestInstrumentationScope(internal.InstrumentationScope(ms.Scope())) 45 | assert.Equal(t, pcommon.InstrumentationScope(internal.GenerateTestInstrumentationScope()), ms.Scope()) 46 | } 47 | 48 | func TestScopeLogs_SchemaUrl(t *testing.T) { 49 | ms := NewScopeLogs() 50 | assert.Equal(t, "", ms.SchemaUrl()) 51 | ms.SetSchemaUrl("https://opentelemetry.io/schemas/1.5.0") 52 | assert.Equal(t, "https://opentelemetry.io/schemas/1.5.0", ms.SchemaUrl()) 53 | sharedState := internal.StateReadOnly 54 | assert.Panics(t, func() { 55 | newScopeLogs(&otlplogs.ScopeLogs{}, &sharedState).SetSchemaUrl("https://opentelemetry.io/schemas/1.5.0") 56 | }) 57 | } 58 | 59 | func TestScopeLogs_LogRecords(t *testing.T) { 60 | ms := NewScopeLogs() 61 | assert.Equal(t, NewLogRecordSlice(), ms.LogRecords()) 62 | fillTestLogRecordSlice(ms.LogRecords()) 63 | assert.Equal(t, generateTestLogRecordSlice(), ms.LogRecords()) 64 | } 65 | 66 | func generateTestScopeLogs() ScopeLogs { 67 | tv := NewScopeLogs() 68 | fillTestScopeLogs(tv) 69 | return tv 70 | } 71 | 72 | func fillTestScopeLogs(tv ScopeLogs) { 73 | internal.FillTestInstrumentationScope(internal.NewInstrumentationScope(&tv.orig.Scope, tv.state)) 74 | tv.orig.SchemaUrl = "https://opentelemetry.io/schemas/1.5.0" 75 | fillTestLogRecordSlice(newLogRecordSlice(&tv.orig.LogRecords, tv.state)) 76 | } 77 | -------------------------------------------------------------------------------- /otel-compressor/pdata/plog/log_record_flags.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package plog // import "go.opentelemetry.io/collector/pdata/plog" 5 | 6 | const isSampledMask = uint32(1) 7 | 8 | var DefaultLogRecordFlags = LogRecordFlags(0) 9 | 10 | // LogRecordFlags defines flags for the LogRecord. The 8 least significant bits are the trace flags as 11 | // defined in W3C Trace Context specification. 24 most significant bits are reserved and must be set to 0. 12 | type LogRecordFlags uint32 13 | 14 | // IsSampled returns true if the LogRecordFlags contains the IsSampled flag. 15 | func (ms LogRecordFlags) IsSampled() bool { 16 | return uint32(ms)&isSampledMask != 0 17 | } 18 | 19 | // WithIsSampled returns a new LogRecordFlags, with the IsSampled flag set to the given value. 20 | func (ms LogRecordFlags) WithIsSampled(b bool) LogRecordFlags { 21 | orig := uint32(ms) 22 | if b { 23 | orig |= isSampledMask 24 | } else { 25 | orig &^= isSampledMask 26 | } 27 | return LogRecordFlags(orig) 28 | } 29 | -------------------------------------------------------------------------------- /otel-compressor/pdata/plog/log_record_flags_test.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package plog 5 | 6 | import ( 7 | "testing" 8 | 9 | "github.com/stretchr/testify/assert" 10 | ) 11 | 12 | func TestLogRecordFlags(t *testing.T) { 13 | flags := LogRecordFlags(1) 14 | assert.True(t, flags.IsSampled()) 15 | assert.EqualValues(t, uint32(1), flags) 16 | 17 | flags = flags.WithIsSampled(false) 18 | assert.False(t, flags.IsSampled()) 19 | assert.EqualValues(t, uint32(0), flags) 20 | 21 | flags = flags.WithIsSampled(true) 22 | assert.True(t, flags.IsSampled()) 23 | assert.EqualValues(t, uint32(1), flags) 24 | } 25 | 26 | func TestDefaultLogRecordFlags(t *testing.T) { 27 | flags := DefaultLogRecordFlags 28 | assert.False(t, flags.IsSampled()) 29 | assert.EqualValues(t, uint32(0), flags) 30 | } 31 | -------------------------------------------------------------------------------- /otel-compressor/pdata/plog/logs.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package plog // import "go.opentelemetry.io/collector/pdata/plog" 5 | 6 | import ( 7 | "go.opentelemetry.io/collector/pdata/internal" 8 | otlpcollectorlog "go.opentelemetry.io/collector/pdata/internal/data/protogen/collector/logs/v1" 9 | ) 10 | 11 | // Logs is the top-level struct that is propagated through the logs pipeline. 12 | // Use NewLogs to create new instance, zero-initialized instance is not valid for use. 13 | type Logs internal.Logs 14 | 15 | func newLogs(orig *otlpcollectorlog.ExportLogsServiceRequest) Logs { 16 | state := internal.StateMutable 17 | return Logs(internal.NewLogs(orig, &state)) 18 | } 19 | 20 | func (ms Logs) getOrig() *otlpcollectorlog.ExportLogsServiceRequest { 21 | return internal.GetOrigLogs(internal.Logs(ms)) 22 | } 23 | 24 | func (ms Logs) getState() *internal.State { 25 | return internal.GetLogsState(internal.Logs(ms)) 26 | } 27 | 28 | // NewLogs creates a new Logs struct. 29 | func NewLogs() Logs { 30 | return newLogs(&otlpcollectorlog.ExportLogsServiceRequest{}) 31 | } 32 | 33 | // IsReadOnly returns true if this Logs instance is read-only. 34 | func (ms Logs) IsReadOnly() bool { 35 | return *ms.getState() == internal.StateReadOnly 36 | } 37 | 38 | // CopyTo copies the Logs instance overriding the destination. 39 | func (ms Logs) CopyTo(dest Logs) { 40 | ms.ResourceLogs().CopyTo(dest.ResourceLogs()) 41 | } 42 | 43 | // LogRecordCount calculates the total number of log records. 44 | func (ms Logs) LogRecordCount() int { 45 | logCount := 0 46 | rss := ms.ResourceLogs() 47 | for i := 0; i < rss.Len(); i++ { 48 | rs := rss.At(i) 49 | ill := rs.ScopeLogs() 50 | for i := 0; i < ill.Len(); i++ { 51 | logs := ill.At(i) 52 | logCount += logs.LogRecords().Len() 53 | } 54 | } 55 | return logCount 56 | } 57 | 58 | // ResourceLogs returns the ResourceLogsSlice associated with this Logs. 59 | func (ms Logs) ResourceLogs() ResourceLogsSlice { 60 | return newResourceLogsSlice(&ms.getOrig().ResourceLogs, internal.GetLogsState(internal.Logs(ms))) 61 | } 62 | 63 | // MarkReadOnly marks the Logs as shared so that no further modifications can be done on it. 64 | func (ms Logs) MarkReadOnly() { 65 | internal.SetLogsState(internal.Logs(ms), internal.StateReadOnly) 66 | } 67 | -------------------------------------------------------------------------------- /otel-compressor/pdata/plog/package_test.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package plog 5 | 6 | import ( 7 | "testing" 8 | 9 | "go.uber.org/goleak" 10 | ) 11 | 12 | func TestMain(m *testing.M) { 13 | goleak.VerifyTestMain(m) 14 | } 15 | -------------------------------------------------------------------------------- /otel-compressor/pdata/plog/pb.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package plog // import "go.opentelemetry.io/collector/pdata/plog" 5 | 6 | import ( 7 | "go.opentelemetry.io/collector/pdata/internal" 8 | otlplogs "go.opentelemetry.io/collector/pdata/internal/data/protogen/logs/v1" 9 | ) 10 | 11 | var _ MarshalSizer = (*ProtoMarshaler)(nil) 12 | 13 | type ProtoMarshaler struct{} 14 | 15 | func (e *ProtoMarshaler) MarshalLogs(ld Logs) ([]byte, error) { 16 | pb := internal.LogsToProto(internal.Logs(ld)) 17 | return pb.Marshal() 18 | } 19 | 20 | func (e *ProtoMarshaler) LogsSize(ld Logs) int { 21 | pb := internal.LogsToProto(internal.Logs(ld)) 22 | return pb.Size() 23 | } 24 | 25 | var _ Unmarshaler = (*ProtoUnmarshaler)(nil) 26 | 27 | type ProtoUnmarshaler struct{} 28 | 29 | func (d *ProtoUnmarshaler) UnmarshalLogs(buf []byte) (Logs, error) { 30 | pb := otlplogs.LogsData{} 31 | err := pb.Unmarshal(buf) 32 | return Logs(internal.LogsFromProto(pb)), err 33 | } 34 | -------------------------------------------------------------------------------- /otel-compressor/pdata/plog/pb_test.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package plog 5 | 6 | import ( 7 | "testing" 8 | "time" 9 | 10 | "github.com/stretchr/testify/assert" 11 | "github.com/stretchr/testify/require" 12 | 13 | "go.opentelemetry.io/collector/pdata/pcommon" 14 | ) 15 | 16 | func TestProtoLogsUnmarshalerError(t *testing.T) { 17 | p := &ProtoUnmarshaler{} 18 | _, err := p.UnmarshalLogs([]byte("+$%")) 19 | assert.Error(t, err) 20 | } 21 | 22 | func TestProtoSizer(t *testing.T) { 23 | marshaler := &ProtoMarshaler{} 24 | ld := NewLogs() 25 | ld.ResourceLogs().AppendEmpty().ScopeLogs().AppendEmpty().LogRecords().AppendEmpty().SetSeverityText("error") 26 | 27 | size := marshaler.LogsSize(ld) 28 | 29 | bytes, err := marshaler.MarshalLogs(ld) 30 | require.NoError(t, err) 31 | assert.Equal(t, len(bytes), size) 32 | 33 | } 34 | 35 | func TestProtoSizerEmptyLogs(t *testing.T) { 36 | sizer := &ProtoMarshaler{} 37 | assert.Equal(t, 0, sizer.LogsSize(NewLogs())) 38 | } 39 | 40 | func BenchmarkLogsToProto(b *testing.B) { 41 | marshaler := &ProtoMarshaler{} 42 | logs := generateBenchmarkLogs(128) 43 | b.ResetTimer() 44 | for n := 0; n < b.N; n++ { 45 | buf, err := marshaler.MarshalLogs(logs) 46 | require.NoError(b, err) 47 | assert.NotEqual(b, 0, len(buf)) 48 | } 49 | } 50 | 51 | func BenchmarkLogsFromProto(b *testing.B) { 52 | marshaler := &ProtoMarshaler{} 53 | unmarshaler := &ProtoUnmarshaler{} 54 | baseLogs := generateBenchmarkLogs(128) 55 | buf, err := marshaler.MarshalLogs(baseLogs) 56 | require.NoError(b, err) 57 | assert.NotEqual(b, 0, len(buf)) 58 | b.ResetTimer() 59 | b.ReportAllocs() 60 | for n := 0; n < b.N; n++ { 61 | logs, err := unmarshaler.UnmarshalLogs(buf) 62 | require.NoError(b, err) 63 | assert.Equal(b, baseLogs.ResourceLogs().Len(), logs.ResourceLogs().Len()) 64 | } 65 | } 66 | 67 | func generateBenchmarkLogs(logsCount int) Logs { 68 | endTime := pcommon.NewTimestampFromTime(time.Now()) 69 | 70 | md := NewLogs() 71 | ilm := md.ResourceLogs().AppendEmpty().ScopeLogs().AppendEmpty() 72 | ilm.LogRecords().EnsureCapacity(logsCount) 73 | for i := 0; i < logsCount; i++ { 74 | im := ilm.LogRecords().AppendEmpty() 75 | im.SetTimestamp(endTime) 76 | } 77 | return md 78 | } 79 | -------------------------------------------------------------------------------- /otel-compressor/pdata/plog/plogotlp/package_test.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package plogotlp 5 | 6 | import ( 7 | "testing" 8 | 9 | "go.uber.org/goleak" 10 | ) 11 | 12 | func TestMain(m *testing.M) { 13 | goleak.VerifyTestMain(m) 14 | } 15 | -------------------------------------------------------------------------------- /otel-compressor/pdata/plog/plogotlp/request.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package plogotlp // import "go.opentelemetry.io/collector/pdata/plog/plogotlp" 5 | 6 | import ( 7 | "bytes" 8 | 9 | "go.opentelemetry.io/collector/pdata/internal" 10 | otlpcollectorlog "go.opentelemetry.io/collector/pdata/internal/data/protogen/collector/logs/v1" 11 | "go.opentelemetry.io/collector/pdata/internal/json" 12 | "go.opentelemetry.io/collector/pdata/internal/otlp" 13 | "go.opentelemetry.io/collector/pdata/plog" 14 | ) 15 | 16 | var jsonUnmarshaler = &plog.JSONUnmarshaler{} 17 | 18 | // ExportRequest represents the request for gRPC/HTTP client/server. 19 | // It's a wrapper for plog.Logs data. 20 | type ExportRequest struct { 21 | orig *otlpcollectorlog.ExportLogsServiceRequest 22 | state *internal.State 23 | } 24 | 25 | // NewExportRequest returns an empty ExportRequest. 26 | func NewExportRequest() ExportRequest { 27 | state := internal.StateMutable 28 | return ExportRequest{ 29 | orig: &otlpcollectorlog.ExportLogsServiceRequest{}, 30 | state: &state, 31 | } 32 | } 33 | 34 | // NewExportRequestFromLogs returns a ExportRequest from plog.Logs. 35 | // Because ExportRequest is a wrapper for plog.Logs, 36 | // any changes to the provided Logs struct will be reflected in the ExportRequest and vice versa. 37 | func NewExportRequestFromLogs(ld plog.Logs) ExportRequest { 38 | return ExportRequest{ 39 | orig: internal.GetOrigLogs(internal.Logs(ld)), 40 | state: internal.GetLogsState(internal.Logs(ld)), 41 | } 42 | } 43 | 44 | // MarshalProto marshals ExportRequest into proto bytes. 45 | func (ms ExportRequest) MarshalProto() ([]byte, error) { 46 | return ms.orig.Marshal() 47 | } 48 | 49 | // UnmarshalProto unmarshalls ExportRequest from proto bytes. 50 | func (ms ExportRequest) UnmarshalProto(data []byte) error { 51 | if err := ms.orig.Unmarshal(data); err != nil { 52 | return err 53 | } 54 | otlp.MigrateLogs(ms.orig.ResourceLogs) 55 | return nil 56 | } 57 | 58 | // MarshalJSON marshals ExportRequest into JSON bytes. 59 | func (ms ExportRequest) MarshalJSON() ([]byte, error) { 60 | var buf bytes.Buffer 61 | if err := json.Marshal(&buf, ms.orig); err != nil { 62 | return nil, err 63 | } 64 | return buf.Bytes(), nil 65 | } 66 | 67 | // UnmarshalJSON unmarshalls ExportRequest from JSON bytes. 68 | func (ms ExportRequest) UnmarshalJSON(data []byte) error { 69 | ld, err := jsonUnmarshaler.UnmarshalLogs(data) 70 | if err != nil { 71 | return err 72 | } 73 | *ms.orig = *internal.GetOrigLogs(internal.Logs(ld)) 74 | return nil 75 | } 76 | 77 | func (ms ExportRequest) Logs() plog.Logs { 78 | return plog.Logs(internal.NewLogs(ms.orig, ms.state)) 79 | } 80 | -------------------------------------------------------------------------------- /otel-compressor/pdata/plog/plogotlp/request_test.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package plogotlp 5 | 6 | import ( 7 | "encoding/json" 8 | "strings" 9 | "testing" 10 | 11 | "github.com/stretchr/testify/assert" 12 | ) 13 | 14 | var _ json.Unmarshaler = ExportRequest{} 15 | var _ json.Marshaler = ExportRequest{} 16 | 17 | var logsRequestJSON = []byte(` 18 | { 19 | "resourceLogs": [ 20 | { 21 | "resource": {}, 22 | "scopeLogs": [ 23 | { 24 | "scope": {}, 25 | "logRecords": [ 26 | { 27 | "body": { 28 | "stringValue": "test_log_record" 29 | }, 30 | "traceId": "", 31 | "spanId": "" 32 | } 33 | ] 34 | } 35 | ] 36 | } 37 | ] 38 | }`) 39 | 40 | func TestRequestToPData(t *testing.T) { 41 | tr := NewExportRequest() 42 | assert.Equal(t, tr.Logs().LogRecordCount(), 0) 43 | tr.Logs().ResourceLogs().AppendEmpty().ScopeLogs().AppendEmpty().LogRecords().AppendEmpty() 44 | assert.Equal(t, tr.Logs().LogRecordCount(), 1) 45 | } 46 | 47 | func TestRequestJSON(t *testing.T) { 48 | lr := NewExportRequest() 49 | assert.NoError(t, lr.UnmarshalJSON(logsRequestJSON)) 50 | assert.Equal(t, "test_log_record", lr.Logs().ResourceLogs().At(0).ScopeLogs().At(0).LogRecords().At(0).Body().AsString()) 51 | 52 | got, err := lr.MarshalJSON() 53 | assert.NoError(t, err) 54 | assert.Equal(t, strings.Join(strings.Fields(string(logsRequestJSON)), ""), string(got)) 55 | } 56 | -------------------------------------------------------------------------------- /otel-compressor/pdata/plog/plogotlp/response_test.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package plogotlp 5 | 6 | import ( 7 | "encoding/json" 8 | "testing" 9 | 10 | jsoniter "github.com/json-iterator/go" 11 | "github.com/stretchr/testify/assert" 12 | ) 13 | 14 | var _ json.Unmarshaler = ExportResponse{} 15 | var _ json.Marshaler = ExportResponse{} 16 | 17 | func TestExportResponseJSON(t *testing.T) { 18 | jsonStr := `{"partialSuccess": {"rejectedLogRecords":1, "errorMessage":"nothing"}}` 19 | val := NewExportResponse() 20 | assert.NoError(t, val.UnmarshalJSON([]byte(jsonStr))) 21 | expected := NewExportResponse() 22 | expected.PartialSuccess().SetRejectedLogRecords(1) 23 | expected.PartialSuccess().SetErrorMessage("nothing") 24 | assert.Equal(t, expected, val) 25 | } 26 | 27 | func TestUnmarshalJSONExportResponse(t *testing.T) { 28 | jsonStr := `{"extra":"", "partialSuccess": {}}` 29 | val := NewExportResponse() 30 | assert.NoError(t, val.UnmarshalJSON([]byte(jsonStr))) 31 | assert.Equal(t, NewExportResponse(), val) 32 | } 33 | 34 | func TestUnmarshalJsoniterExportPartialSuccess(t *testing.T) { 35 | jsonStr := `{"extra":""}` 36 | iter := jsoniter.ConfigFastest.BorrowIterator([]byte(jsonStr)) 37 | defer jsoniter.ConfigFastest.ReturnIterator(iter) 38 | val := NewExportPartialSuccess() 39 | val.unmarshalJsoniter(iter) 40 | assert.NoError(t, iter.Error) 41 | assert.Equal(t, NewExportPartialSuccess(), val) 42 | } 43 | -------------------------------------------------------------------------------- /otel-compressor/pdata/plog/severity_number_test.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package plog // import "go.opentelemetry.io/collector/pdata/plog" 5 | 6 | import ( 7 | "testing" 8 | 9 | "github.com/stretchr/testify/assert" 10 | ) 11 | 12 | func TestSeverityNumberString(t *testing.T) { 13 | assert.EqualValues(t, "Unspecified", SeverityNumberUnspecified.String()) 14 | assert.EqualValues(t, "Trace", SeverityNumberTrace.String()) 15 | assert.EqualValues(t, "Trace2", SeverityNumberTrace2.String()) 16 | assert.EqualValues(t, "Trace3", SeverityNumberTrace3.String()) 17 | assert.EqualValues(t, "Trace4", SeverityNumberTrace4.String()) 18 | assert.EqualValues(t, "Debug", SeverityNumberDebug.String()) 19 | assert.EqualValues(t, "Debug2", SeverityNumberDebug2.String()) 20 | assert.EqualValues(t, "Debug3", SeverityNumberDebug3.String()) 21 | assert.EqualValues(t, "Debug4", SeverityNumberDebug4.String()) 22 | assert.EqualValues(t, "Info", SeverityNumberInfo.String()) 23 | assert.EqualValues(t, "Info2", SeverityNumberInfo2.String()) 24 | assert.EqualValues(t, "Info3", SeverityNumberInfo3.String()) 25 | assert.EqualValues(t, "Info4", SeverityNumberInfo4.String()) 26 | assert.EqualValues(t, "Warn", SeverityNumberWarn.String()) 27 | assert.EqualValues(t, "Warn2", SeverityNumberWarn2.String()) 28 | assert.EqualValues(t, "Warn3", SeverityNumberWarn3.String()) 29 | assert.EqualValues(t, "Warn4", SeverityNumberWarn4.String()) 30 | assert.EqualValues(t, "Error", SeverityNumberError.String()) 31 | assert.EqualValues(t, "Error2", SeverityNumberError2.String()) 32 | assert.EqualValues(t, "Error3", SeverityNumberError3.String()) 33 | assert.EqualValues(t, "Error4", SeverityNumberError4.String()) 34 | assert.EqualValues(t, "Fatal", SeverityNumberFatal.String()) 35 | assert.EqualValues(t, "Fatal2", SeverityNumberFatal2.String()) 36 | assert.EqualValues(t, "Fatal3", SeverityNumberFatal3.String()) 37 | assert.EqualValues(t, "Fatal4", SeverityNumberFatal4.String()) 38 | assert.EqualValues(t, "", SeverityNumber(100).String()) 39 | } 40 | -------------------------------------------------------------------------------- /otel-compressor/pdata/pmetric/aggregation_temporality.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package pmetric // import "go.opentelemetry.io/collector/pdata/pmetric" 5 | 6 | import ( 7 | otlpmetrics "go.opentelemetry.io/collector/pdata/internal/data/protogen/metrics/v1" 8 | ) 9 | 10 | // AggregationTemporality defines how a metric aggregator reports aggregated values. 11 | // It describes how those values relate to the time interval over which they are aggregated. 12 | type AggregationTemporality int32 13 | 14 | const ( 15 | // AggregationTemporalityUnspecified is the default AggregationTemporality, it MUST NOT be used. 16 | AggregationTemporalityUnspecified = AggregationTemporality(otlpmetrics.AggregationTemporality_AGGREGATION_TEMPORALITY_UNSPECIFIED) 17 | // AggregationTemporalityDelta is a AggregationTemporality for a metric aggregator which reports changes since last report time. 18 | AggregationTemporalityDelta = AggregationTemporality(otlpmetrics.AggregationTemporality_AGGREGATION_TEMPORALITY_DELTA) 19 | // AggregationTemporalityCumulative is a AggregationTemporality for a metric aggregator which reports changes since a fixed start time. 20 | AggregationTemporalityCumulative = AggregationTemporality(otlpmetrics.AggregationTemporality_AGGREGATION_TEMPORALITY_CUMULATIVE) 21 | ) 22 | 23 | // String returns the string representation of the AggregationTemporality. 24 | func (at AggregationTemporality) String() string { 25 | switch at { 26 | case AggregationTemporalityUnspecified: 27 | return "Unspecified" 28 | case AggregationTemporalityDelta: 29 | return "Delta" 30 | case AggregationTemporalityCumulative: 31 | return "Cumulative" 32 | } 33 | return "" 34 | } 35 | -------------------------------------------------------------------------------- /otel-compressor/pdata/pmetric/aggregation_temporality_test.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package pmetric // import "go.opentelemetry.io/collector/pdata/pmetric" 5 | 6 | import ( 7 | "testing" 8 | 9 | "github.com/stretchr/testify/assert" 10 | ) 11 | 12 | func TestAggregationTemporalityString(t *testing.T) { 13 | assert.Equal(t, "Unspecified", AggregationTemporalityUnspecified.String()) 14 | assert.Equal(t, "Delta", AggregationTemporalityDelta.String()) 15 | assert.Equal(t, "Cumulative", AggregationTemporalityCumulative.String()) 16 | assert.Equal(t, "", (AggregationTemporalityCumulative + 1).String()) 17 | } 18 | -------------------------------------------------------------------------------- /otel-compressor/pdata/pmetric/encoding.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package pmetric // import "go.opentelemetry.io/collector/pdata/pmetric" 5 | 6 | // MarshalSizer is the interface that groups the basic Marshal and Size methods 7 | type MarshalSizer interface { 8 | Marshaler 9 | Sizer 10 | } 11 | 12 | // Marshaler marshals pmetric.Metrics into bytes. 13 | type Marshaler interface { 14 | // MarshalMetrics the given pmetric.Metrics into bytes. 15 | // If the error is not nil, the returned bytes slice cannot be used. 16 | MarshalMetrics(md Metrics) ([]byte, error) 17 | } 18 | 19 | // Unmarshaler unmarshalls bytes into pmetric.Metrics. 20 | type Unmarshaler interface { 21 | // UnmarshalMetrics the given bytes into pmetric.Metrics. 22 | // If the error is not nil, the returned pmetric.Metrics cannot be used. 23 | UnmarshalMetrics(buf []byte) (Metrics, error) 24 | } 25 | 26 | // Sizer is an optional interface implemented by the Marshaler, that calculates the size of a marshaled Metrics. 27 | type Sizer interface { 28 | // MetricsSize returns the size in bytes of a marshaled Metrics. 29 | MetricsSize(md Metrics) int 30 | } 31 | -------------------------------------------------------------------------------- /otel-compressor/pdata/pmetric/exemplar_value_type.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package pmetric // import "go.opentelemetry.io/collector/pdata/pmetric" 5 | 6 | // ExemplarValueType specifies the type of Exemplar measurement value. 7 | type ExemplarValueType int32 8 | 9 | const ( 10 | // ExemplarValueTypeEmpty means that exemplar value is unset. 11 | ExemplarValueTypeEmpty ExemplarValueType = iota 12 | ExemplarValueTypeInt 13 | ExemplarValueTypeDouble 14 | ) 15 | 16 | // String returns the string representation of the ExemplarValueType. 17 | func (nt ExemplarValueType) String() string { 18 | switch nt { 19 | case ExemplarValueTypeEmpty: 20 | return "Empty" 21 | case ExemplarValueTypeInt: 22 | return "Int" 23 | case ExemplarValueTypeDouble: 24 | return "Double" 25 | } 26 | return "" 27 | } 28 | -------------------------------------------------------------------------------- /otel-compressor/pdata/pmetric/exemplar_value_type_test.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package pmetric // import "go.opentelemetry.io/collector/pdata/pmetric" 5 | 6 | import ( 7 | "testing" 8 | 9 | "github.com/stretchr/testify/assert" 10 | ) 11 | 12 | func TestExemplarValueTypeString(t *testing.T) { 13 | assert.Equal(t, "Empty", ExemplarValueTypeEmpty.String()) 14 | assert.Equal(t, "Int", ExemplarValueTypeInt.String()) 15 | assert.Equal(t, "Double", ExemplarValueTypeDouble.String()) 16 | assert.Equal(t, "", (ExemplarValueTypeDouble + 1).String()) 17 | } 18 | -------------------------------------------------------------------------------- /otel-compressor/pdata/pmetric/generated_exponentialhistogram_test.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | // Code generated by "pdata/internal/cmd/pdatagen/main.go". DO NOT EDIT. 5 | // To regenerate this file run "make genpdata". 6 | 7 | package pmetric 8 | 9 | import ( 10 | "testing" 11 | 12 | "github.com/stretchr/testify/assert" 13 | 14 | "go.opentelemetry.io/collector/pdata/internal" 15 | otlpmetrics "go.opentelemetry.io/collector/pdata/internal/data/protogen/metrics/v1" 16 | ) 17 | 18 | func TestExponentialHistogram_MoveTo(t *testing.T) { 19 | ms := generateTestExponentialHistogram() 20 | dest := NewExponentialHistogram() 21 | ms.MoveTo(dest) 22 | assert.Equal(t, NewExponentialHistogram(), ms) 23 | assert.Equal(t, generateTestExponentialHistogram(), dest) 24 | sharedState := internal.StateReadOnly 25 | assert.Panics(t, func() { ms.MoveTo(newExponentialHistogram(&otlpmetrics.ExponentialHistogram{}, &sharedState)) }) 26 | assert.Panics(t, func() { newExponentialHistogram(&otlpmetrics.ExponentialHistogram{}, &sharedState).MoveTo(dest) }) 27 | } 28 | 29 | func TestExponentialHistogram_CopyTo(t *testing.T) { 30 | ms := NewExponentialHistogram() 31 | orig := NewExponentialHistogram() 32 | orig.CopyTo(ms) 33 | assert.Equal(t, orig, ms) 34 | orig = generateTestExponentialHistogram() 35 | orig.CopyTo(ms) 36 | assert.Equal(t, orig, ms) 37 | sharedState := internal.StateReadOnly 38 | assert.Panics(t, func() { ms.CopyTo(newExponentialHistogram(&otlpmetrics.ExponentialHistogram{}, &sharedState)) }) 39 | } 40 | 41 | func TestExponentialHistogram_AggregationTemporality(t *testing.T) { 42 | ms := NewExponentialHistogram() 43 | assert.Equal(t, AggregationTemporality(otlpmetrics.AggregationTemporality(0)), ms.AggregationTemporality()) 44 | testValAggregationTemporality := AggregationTemporality(otlpmetrics.AggregationTemporality(1)) 45 | ms.SetAggregationTemporality(testValAggregationTemporality) 46 | assert.Equal(t, testValAggregationTemporality, ms.AggregationTemporality()) 47 | } 48 | 49 | func TestExponentialHistogram_DataPoints(t *testing.T) { 50 | ms := NewExponentialHistogram() 51 | assert.Equal(t, NewExponentialHistogramDataPointSlice(), ms.DataPoints()) 52 | fillTestExponentialHistogramDataPointSlice(ms.DataPoints()) 53 | assert.Equal(t, generateTestExponentialHistogramDataPointSlice(), ms.DataPoints()) 54 | } 55 | 56 | func generateTestExponentialHistogram() ExponentialHistogram { 57 | tv := NewExponentialHistogram() 58 | fillTestExponentialHistogram(tv) 59 | return tv 60 | } 61 | 62 | func fillTestExponentialHistogram(tv ExponentialHistogram) { 63 | tv.orig.AggregationTemporality = otlpmetrics.AggregationTemporality(1) 64 | fillTestExponentialHistogramDataPointSlice(newExponentialHistogramDataPointSlice(&tv.orig.DataPoints, tv.state)) 65 | } 66 | -------------------------------------------------------------------------------- /otel-compressor/pdata/pmetric/generated_gauge.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | // Code generated by "pdata/internal/cmd/pdatagen/main.go". DO NOT EDIT. 5 | // To regenerate this file run "make genpdata". 6 | 7 | package pmetric 8 | 9 | import ( 10 | "go.opentelemetry.io/collector/pdata/internal" 11 | otlpmetrics "go.opentelemetry.io/collector/pdata/internal/data/protogen/metrics/v1" 12 | ) 13 | 14 | // Gauge represents the type of a numeric metric that always exports the "current value" for every data point. 15 | // 16 | // This is a reference type, if passed by value and callee modifies it the 17 | // caller will see the modification. 18 | // 19 | // Must use NewGauge function to create new instances. 20 | // Important: zero-initialized instance is not valid for use. 21 | type Gauge struct { 22 | orig *otlpmetrics.Gauge 23 | state *internal.State 24 | } 25 | 26 | func newGauge(orig *otlpmetrics.Gauge, state *internal.State) Gauge { 27 | return Gauge{orig: orig, state: state} 28 | } 29 | 30 | // NewGauge creates a new empty Gauge. 31 | // 32 | // This must be used only in testing code. Users should use "AppendEmpty" when part of a Slice, 33 | // OR directly access the member if this is embedded in another struct. 34 | func NewGauge() Gauge { 35 | state := internal.StateMutable 36 | return newGauge(&otlpmetrics.Gauge{}, &state) 37 | } 38 | 39 | // MoveTo moves all properties from the current struct overriding the destination and 40 | // resetting the current instance to its zero value 41 | func (ms Gauge) MoveTo(dest Gauge) { 42 | ms.state.AssertMutable() 43 | dest.state.AssertMutable() 44 | *dest.orig = *ms.orig 45 | *ms.orig = otlpmetrics.Gauge{} 46 | } 47 | 48 | // DataPoints returns the DataPoints associated with this Gauge. 49 | func (ms Gauge) DataPoints() NumberDataPointSlice { 50 | return newNumberDataPointSlice(&ms.orig.DataPoints, ms.state) 51 | } 52 | 53 | // CopyTo copies all properties from the current struct overriding the destination. 54 | func (ms Gauge) CopyTo(dest Gauge) { 55 | dest.state.AssertMutable() 56 | ms.DataPoints().CopyTo(dest.DataPoints()) 57 | } 58 | -------------------------------------------------------------------------------- /otel-compressor/pdata/pmetric/generated_gauge_test.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | // Code generated by "pdata/internal/cmd/pdatagen/main.go". DO NOT EDIT. 5 | // To regenerate this file run "make genpdata". 6 | 7 | package pmetric 8 | 9 | import ( 10 | "testing" 11 | 12 | "github.com/stretchr/testify/assert" 13 | 14 | "go.opentelemetry.io/collector/pdata/internal" 15 | otlpmetrics "go.opentelemetry.io/collector/pdata/internal/data/protogen/metrics/v1" 16 | ) 17 | 18 | func TestGauge_MoveTo(t *testing.T) { 19 | ms := generateTestGauge() 20 | dest := NewGauge() 21 | ms.MoveTo(dest) 22 | assert.Equal(t, NewGauge(), ms) 23 | assert.Equal(t, generateTestGauge(), dest) 24 | sharedState := internal.StateReadOnly 25 | assert.Panics(t, func() { ms.MoveTo(newGauge(&otlpmetrics.Gauge{}, &sharedState)) }) 26 | assert.Panics(t, func() { newGauge(&otlpmetrics.Gauge{}, &sharedState).MoveTo(dest) }) 27 | } 28 | 29 | func TestGauge_CopyTo(t *testing.T) { 30 | ms := NewGauge() 31 | orig := NewGauge() 32 | orig.CopyTo(ms) 33 | assert.Equal(t, orig, ms) 34 | orig = generateTestGauge() 35 | orig.CopyTo(ms) 36 | assert.Equal(t, orig, ms) 37 | sharedState := internal.StateReadOnly 38 | assert.Panics(t, func() { ms.CopyTo(newGauge(&otlpmetrics.Gauge{}, &sharedState)) }) 39 | } 40 | 41 | func TestGauge_DataPoints(t *testing.T) { 42 | ms := NewGauge() 43 | assert.Equal(t, NewNumberDataPointSlice(), ms.DataPoints()) 44 | fillTestNumberDataPointSlice(ms.DataPoints()) 45 | assert.Equal(t, generateTestNumberDataPointSlice(), ms.DataPoints()) 46 | } 47 | 48 | func generateTestGauge() Gauge { 49 | tv := NewGauge() 50 | fillTestGauge(tv) 51 | return tv 52 | } 53 | 54 | func fillTestGauge(tv Gauge) { 55 | fillTestNumberDataPointSlice(newNumberDataPointSlice(&tv.orig.DataPoints, tv.state)) 56 | } 57 | -------------------------------------------------------------------------------- /otel-compressor/pdata/pmetric/generated_histogram_test.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | // Code generated by "pdata/internal/cmd/pdatagen/main.go". DO NOT EDIT. 5 | // To regenerate this file run "make genpdata". 6 | 7 | package pmetric 8 | 9 | import ( 10 | "testing" 11 | 12 | "github.com/stretchr/testify/assert" 13 | 14 | "go.opentelemetry.io/collector/pdata/internal" 15 | otlpmetrics "go.opentelemetry.io/collector/pdata/internal/data/protogen/metrics/v1" 16 | ) 17 | 18 | func TestHistogram_MoveTo(t *testing.T) { 19 | ms := generateTestHistogram() 20 | dest := NewHistogram() 21 | ms.MoveTo(dest) 22 | assert.Equal(t, NewHistogram(), ms) 23 | assert.Equal(t, generateTestHistogram(), dest) 24 | sharedState := internal.StateReadOnly 25 | assert.Panics(t, func() { ms.MoveTo(newHistogram(&otlpmetrics.Histogram{}, &sharedState)) }) 26 | assert.Panics(t, func() { newHistogram(&otlpmetrics.Histogram{}, &sharedState).MoveTo(dest) }) 27 | } 28 | 29 | func TestHistogram_CopyTo(t *testing.T) { 30 | ms := NewHistogram() 31 | orig := NewHistogram() 32 | orig.CopyTo(ms) 33 | assert.Equal(t, orig, ms) 34 | orig = generateTestHistogram() 35 | orig.CopyTo(ms) 36 | assert.Equal(t, orig, ms) 37 | sharedState := internal.StateReadOnly 38 | assert.Panics(t, func() { ms.CopyTo(newHistogram(&otlpmetrics.Histogram{}, &sharedState)) }) 39 | } 40 | 41 | func TestHistogram_AggregationTemporality(t *testing.T) { 42 | ms := NewHistogram() 43 | assert.Equal(t, AggregationTemporality(otlpmetrics.AggregationTemporality(0)), ms.AggregationTemporality()) 44 | testValAggregationTemporality := AggregationTemporality(otlpmetrics.AggregationTemporality(1)) 45 | ms.SetAggregationTemporality(testValAggregationTemporality) 46 | assert.Equal(t, testValAggregationTemporality, ms.AggregationTemporality()) 47 | } 48 | 49 | func TestHistogram_DataPoints(t *testing.T) { 50 | ms := NewHistogram() 51 | assert.Equal(t, NewHistogramDataPointSlice(), ms.DataPoints()) 52 | fillTestHistogramDataPointSlice(ms.DataPoints()) 53 | assert.Equal(t, generateTestHistogramDataPointSlice(), ms.DataPoints()) 54 | } 55 | 56 | func generateTestHistogram() Histogram { 57 | tv := NewHistogram() 58 | fillTestHistogram(tv) 59 | return tv 60 | } 61 | 62 | func fillTestHistogram(tv Histogram) { 63 | tv.orig.AggregationTemporality = otlpmetrics.AggregationTemporality(1) 64 | fillTestHistogramDataPointSlice(newHistogramDataPointSlice(&tv.orig.DataPoints, tv.state)) 65 | } 66 | -------------------------------------------------------------------------------- /otel-compressor/pdata/pmetric/generated_sum_test.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | // Code generated by "pdata/internal/cmd/pdatagen/main.go". DO NOT EDIT. 5 | // To regenerate this file run "make genpdata". 6 | 7 | package pmetric 8 | 9 | import ( 10 | "testing" 11 | 12 | "github.com/stretchr/testify/assert" 13 | 14 | "go.opentelemetry.io/collector/pdata/internal" 15 | otlpmetrics "go.opentelemetry.io/collector/pdata/internal/data/protogen/metrics/v1" 16 | ) 17 | 18 | func TestSum_MoveTo(t *testing.T) { 19 | ms := generateTestSum() 20 | dest := NewSum() 21 | ms.MoveTo(dest) 22 | assert.Equal(t, NewSum(), ms) 23 | assert.Equal(t, generateTestSum(), dest) 24 | sharedState := internal.StateReadOnly 25 | assert.Panics(t, func() { ms.MoveTo(newSum(&otlpmetrics.Sum{}, &sharedState)) }) 26 | assert.Panics(t, func() { newSum(&otlpmetrics.Sum{}, &sharedState).MoveTo(dest) }) 27 | } 28 | 29 | func TestSum_CopyTo(t *testing.T) { 30 | ms := NewSum() 31 | orig := NewSum() 32 | orig.CopyTo(ms) 33 | assert.Equal(t, orig, ms) 34 | orig = generateTestSum() 35 | orig.CopyTo(ms) 36 | assert.Equal(t, orig, ms) 37 | sharedState := internal.StateReadOnly 38 | assert.Panics(t, func() { ms.CopyTo(newSum(&otlpmetrics.Sum{}, &sharedState)) }) 39 | } 40 | 41 | func TestSum_AggregationTemporality(t *testing.T) { 42 | ms := NewSum() 43 | assert.Equal(t, AggregationTemporality(otlpmetrics.AggregationTemporality(0)), ms.AggregationTemporality()) 44 | testValAggregationTemporality := AggregationTemporality(otlpmetrics.AggregationTemporality(1)) 45 | ms.SetAggregationTemporality(testValAggregationTemporality) 46 | assert.Equal(t, testValAggregationTemporality, ms.AggregationTemporality()) 47 | } 48 | 49 | func TestSum_IsMonotonic(t *testing.T) { 50 | ms := NewSum() 51 | assert.Equal(t, false, ms.IsMonotonic()) 52 | ms.SetIsMonotonic(true) 53 | assert.Equal(t, true, ms.IsMonotonic()) 54 | sharedState := internal.StateReadOnly 55 | assert.Panics(t, func() { newSum(&otlpmetrics.Sum{}, &sharedState).SetIsMonotonic(true) }) 56 | } 57 | 58 | func TestSum_DataPoints(t *testing.T) { 59 | ms := NewSum() 60 | assert.Equal(t, NewNumberDataPointSlice(), ms.DataPoints()) 61 | fillTestNumberDataPointSlice(ms.DataPoints()) 62 | assert.Equal(t, generateTestNumberDataPointSlice(), ms.DataPoints()) 63 | } 64 | 65 | func generateTestSum() Sum { 66 | tv := NewSum() 67 | fillTestSum(tv) 68 | return tv 69 | } 70 | 71 | func fillTestSum(tv Sum) { 72 | tv.orig.AggregationTemporality = otlpmetrics.AggregationTemporality(1) 73 | tv.orig.IsMonotonic = true 74 | fillTestNumberDataPointSlice(newNumberDataPointSlice(&tv.orig.DataPoints, tv.state)) 75 | } 76 | -------------------------------------------------------------------------------- /otel-compressor/pdata/pmetric/generated_summary.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | // Code generated by "pdata/internal/cmd/pdatagen/main.go". DO NOT EDIT. 5 | // To regenerate this file run "make genpdata". 6 | 7 | package pmetric 8 | 9 | import ( 10 | "go.opentelemetry.io/collector/pdata/internal" 11 | otlpmetrics "go.opentelemetry.io/collector/pdata/internal/data/protogen/metrics/v1" 12 | ) 13 | 14 | // Summary represents the type of a metric that is calculated by aggregating as a Summary of all reported double measurements over a time interval. 15 | // 16 | // This is a reference type, if passed by value and callee modifies it the 17 | // caller will see the modification. 18 | // 19 | // Must use NewSummary function to create new instances. 20 | // Important: zero-initialized instance is not valid for use. 21 | type Summary struct { 22 | orig *otlpmetrics.Summary 23 | state *internal.State 24 | } 25 | 26 | func newSummary(orig *otlpmetrics.Summary, state *internal.State) Summary { 27 | return Summary{orig: orig, state: state} 28 | } 29 | 30 | // NewSummary creates a new empty Summary. 31 | // 32 | // This must be used only in testing code. Users should use "AppendEmpty" when part of a Slice, 33 | // OR directly access the member if this is embedded in another struct. 34 | func NewSummary() Summary { 35 | state := internal.StateMutable 36 | return newSummary(&otlpmetrics.Summary{}, &state) 37 | } 38 | 39 | // MoveTo moves all properties from the current struct overriding the destination and 40 | // resetting the current instance to its zero value 41 | func (ms Summary) MoveTo(dest Summary) { 42 | ms.state.AssertMutable() 43 | dest.state.AssertMutable() 44 | *dest.orig = *ms.orig 45 | *ms.orig = otlpmetrics.Summary{} 46 | } 47 | 48 | // DataPoints returns the DataPoints associated with this Summary. 49 | func (ms Summary) DataPoints() SummaryDataPointSlice { 50 | return newSummaryDataPointSlice(&ms.orig.DataPoints, ms.state) 51 | } 52 | 53 | // CopyTo copies all properties from the current struct overriding the destination. 54 | func (ms Summary) CopyTo(dest Summary) { 55 | dest.state.AssertMutable() 56 | ms.DataPoints().CopyTo(dest.DataPoints()) 57 | } 58 | -------------------------------------------------------------------------------- /otel-compressor/pdata/pmetric/generated_summary_test.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | // Code generated by "pdata/internal/cmd/pdatagen/main.go". DO NOT EDIT. 5 | // To regenerate this file run "make genpdata". 6 | 7 | package pmetric 8 | 9 | import ( 10 | "testing" 11 | 12 | "github.com/stretchr/testify/assert" 13 | 14 | "go.opentelemetry.io/collector/pdata/internal" 15 | otlpmetrics "go.opentelemetry.io/collector/pdata/internal/data/protogen/metrics/v1" 16 | ) 17 | 18 | func TestSummary_MoveTo(t *testing.T) { 19 | ms := generateTestSummary() 20 | dest := NewSummary() 21 | ms.MoveTo(dest) 22 | assert.Equal(t, NewSummary(), ms) 23 | assert.Equal(t, generateTestSummary(), dest) 24 | sharedState := internal.StateReadOnly 25 | assert.Panics(t, func() { ms.MoveTo(newSummary(&otlpmetrics.Summary{}, &sharedState)) }) 26 | assert.Panics(t, func() { newSummary(&otlpmetrics.Summary{}, &sharedState).MoveTo(dest) }) 27 | } 28 | 29 | func TestSummary_CopyTo(t *testing.T) { 30 | ms := NewSummary() 31 | orig := NewSummary() 32 | orig.CopyTo(ms) 33 | assert.Equal(t, orig, ms) 34 | orig = generateTestSummary() 35 | orig.CopyTo(ms) 36 | assert.Equal(t, orig, ms) 37 | sharedState := internal.StateReadOnly 38 | assert.Panics(t, func() { ms.CopyTo(newSummary(&otlpmetrics.Summary{}, &sharedState)) }) 39 | } 40 | 41 | func TestSummary_DataPoints(t *testing.T) { 42 | ms := NewSummary() 43 | assert.Equal(t, NewSummaryDataPointSlice(), ms.DataPoints()) 44 | fillTestSummaryDataPointSlice(ms.DataPoints()) 45 | assert.Equal(t, generateTestSummaryDataPointSlice(), ms.DataPoints()) 46 | } 47 | 48 | func generateTestSummary() Summary { 49 | tv := NewSummary() 50 | fillTestSummary(tv) 51 | return tv 52 | } 53 | 54 | func fillTestSummary(tv Summary) { 55 | fillTestSummaryDataPointSlice(newSummaryDataPointSlice(&tv.orig.DataPoints, tv.state)) 56 | } 57 | -------------------------------------------------------------------------------- /otel-compressor/pdata/pmetric/metric_data_point_flags.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package pmetric // import "go.opentelemetry.io/collector/pdata/pmetric" 5 | 6 | const noRecordValueMask = uint32(1) 7 | 8 | var DefaultDataPointFlags = DataPointFlags(0) 9 | 10 | // DataPointFlags defines how a metric aggregator reports aggregated values. 11 | // It describes how those values relate to the time interval over which they are aggregated. 12 | type DataPointFlags uint32 13 | 14 | // NoRecordedValue returns true if the DataPointFlags contains the NoRecordedValue flag. 15 | func (ms DataPointFlags) NoRecordedValue() bool { 16 | return uint32(ms)&noRecordValueMask != 0 17 | } 18 | 19 | // WithNoRecordedValue returns a new DataPointFlags, with the NoRecordedValue flag set to the given value. 20 | func (ms DataPointFlags) WithNoRecordedValue(b bool) DataPointFlags { 21 | orig := uint32(ms) 22 | if b { 23 | orig |= noRecordValueMask 24 | } else { 25 | orig &^= noRecordValueMask 26 | } 27 | return DataPointFlags(orig) 28 | } 29 | -------------------------------------------------------------------------------- /otel-compressor/pdata/pmetric/metric_data_point_flags_test.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package pmetric 5 | 6 | import ( 7 | "testing" 8 | 9 | "github.com/stretchr/testify/assert" 10 | ) 11 | 12 | func TestLogRecordFlags(t *testing.T) { 13 | flags := DataPointFlags(1) 14 | assert.True(t, flags.NoRecordedValue()) 15 | assert.EqualValues(t, uint32(1), flags) 16 | 17 | flags = flags.WithNoRecordedValue(false) 18 | assert.False(t, flags.NoRecordedValue()) 19 | assert.EqualValues(t, uint32(0), flags) 20 | 21 | flags = flags.WithNoRecordedValue(true) 22 | assert.True(t, flags.NoRecordedValue()) 23 | assert.EqualValues(t, uint32(1), flags) 24 | } 25 | 26 | func TestDefaultLogRecordFlags(t *testing.T) { 27 | flags := DefaultDataPointFlags 28 | assert.False(t, flags.NoRecordedValue()) 29 | assert.EqualValues(t, uint32(0), flags) 30 | } 31 | -------------------------------------------------------------------------------- /otel-compressor/pdata/pmetric/metric_type.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package pmetric // import "go.opentelemetry.io/collector/pdata/pmetric" 5 | 6 | // MetricType specifies the type of data in a Metric. 7 | type MetricType int32 8 | 9 | const ( 10 | // MetricTypeEmpty means that metric type is unset. 11 | MetricTypeEmpty MetricType = iota 12 | MetricTypeGauge 13 | MetricTypeSum 14 | MetricTypeHistogram 15 | MetricTypeExponentialHistogram 16 | MetricTypeSummary 17 | ) 18 | 19 | // String returns the string representation of the MetricType. 20 | func (mdt MetricType) String() string { 21 | switch mdt { 22 | case MetricTypeEmpty: 23 | return "Empty" 24 | case MetricTypeGauge: 25 | return "Gauge" 26 | case MetricTypeSum: 27 | return "Sum" 28 | case MetricTypeHistogram: 29 | return "Histogram" 30 | case MetricTypeExponentialHistogram: 31 | return "ExponentialHistogram" 32 | case MetricTypeSummary: 33 | return "Summary" 34 | } 35 | return "" 36 | } 37 | -------------------------------------------------------------------------------- /otel-compressor/pdata/pmetric/metric_type_test.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package pmetric // import "go.opentelemetry.io/collector/pdata/pmetric" 5 | 6 | import ( 7 | "testing" 8 | 9 | "github.com/stretchr/testify/assert" 10 | ) 11 | 12 | func TestMetricTypeString(t *testing.T) { 13 | assert.Equal(t, "Empty", MetricTypeEmpty.String()) 14 | assert.Equal(t, "Gauge", MetricTypeGauge.String()) 15 | assert.Equal(t, "Sum", MetricTypeSum.String()) 16 | assert.Equal(t, "Histogram", MetricTypeHistogram.String()) 17 | assert.Equal(t, "ExponentialHistogram", MetricTypeExponentialHistogram.String()) 18 | assert.Equal(t, "Summary", MetricTypeSummary.String()) 19 | assert.Equal(t, "", (MetricTypeSummary + 1).String()) 20 | } 21 | -------------------------------------------------------------------------------- /otel-compressor/pdata/pmetric/number_data_point_value_type.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package pmetric // import "go.opentelemetry.io/collector/pdata/pmetric" 5 | 6 | // NumberDataPointValueType specifies the type of NumberDataPoint value. 7 | type NumberDataPointValueType int32 8 | 9 | const ( 10 | // NumberDataPointValueTypeEmpty means that data point value is unset. 11 | NumberDataPointValueTypeEmpty NumberDataPointValueType = iota 12 | NumberDataPointValueTypeInt 13 | NumberDataPointValueTypeDouble 14 | ) 15 | 16 | // String returns the string representation of the NumberDataPointValueType. 17 | func (nt NumberDataPointValueType) String() string { 18 | switch nt { 19 | case NumberDataPointValueTypeEmpty: 20 | return "Empty" 21 | case NumberDataPointValueTypeInt: 22 | return "Int" 23 | case NumberDataPointValueTypeDouble: 24 | return "Double" 25 | } 26 | return "" 27 | } 28 | -------------------------------------------------------------------------------- /otel-compressor/pdata/pmetric/number_data_point_value_type_test.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package pmetric // import "go.opentelemetry.io/collector/pdata/pmetric" 5 | 6 | import ( 7 | "testing" 8 | 9 | "github.com/stretchr/testify/assert" 10 | ) 11 | 12 | func TestNumberDataPointValueTypeString(t *testing.T) { 13 | assert.Equal(t, "Empty", NumberDataPointValueTypeEmpty.String()) 14 | assert.Equal(t, "Int", NumberDataPointValueTypeInt.String()) 15 | assert.Equal(t, "Double", NumberDataPointValueTypeDouble.String()) 16 | assert.Equal(t, "", (NumberDataPointValueTypeDouble + 1).String()) 17 | } 18 | -------------------------------------------------------------------------------- /otel-compressor/pdata/pmetric/package_test.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package pmetric 5 | 6 | import ( 7 | "testing" 8 | 9 | "go.uber.org/goleak" 10 | ) 11 | 12 | func TestMain(m *testing.M) { 13 | goleak.VerifyTestMain(m) 14 | } 15 | -------------------------------------------------------------------------------- /otel-compressor/pdata/pmetric/pb.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package pmetric // import "go.opentelemetry.io/collector/pdata/pmetric" 5 | 6 | import ( 7 | "go.opentelemetry.io/collector/pdata/internal" 8 | otlpmetrics "go.opentelemetry.io/collector/pdata/internal/data/protogen/metrics/v1" 9 | ) 10 | 11 | var _ MarshalSizer = (*ProtoMarshaler)(nil) 12 | 13 | type ProtoMarshaler struct{} 14 | 15 | func (e *ProtoMarshaler) MarshalMetrics(md Metrics) ([]byte, error) { 16 | pb := internal.MetricsToProto(internal.Metrics(md)) 17 | return pb.Marshal() 18 | } 19 | 20 | func (e *ProtoMarshaler) MetricsSize(md Metrics) int { 21 | pb := internal.MetricsToProto(internal.Metrics(md)) 22 | return pb.Size() 23 | } 24 | 25 | type ProtoUnmarshaler struct{} 26 | 27 | func (d *ProtoUnmarshaler) UnmarshalMetrics(buf []byte) (Metrics, error) { 28 | pb := otlpmetrics.MetricsData{} 29 | err := pb.Unmarshal(buf) 30 | return Metrics(internal.MetricsFromProto(pb)), err 31 | } 32 | -------------------------------------------------------------------------------- /otel-compressor/pdata/pmetric/pb_test.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package pmetric 5 | 6 | import ( 7 | "testing" 8 | "time" 9 | 10 | "github.com/stretchr/testify/assert" 11 | "github.com/stretchr/testify/require" 12 | 13 | "go.opentelemetry.io/collector/pdata/pcommon" 14 | ) 15 | 16 | func TestProtoMetricsUnmarshalerError(t *testing.T) { 17 | p := &ProtoUnmarshaler{} 18 | _, err := p.UnmarshalMetrics([]byte("+$%")) 19 | assert.Error(t, err) 20 | } 21 | 22 | func TestProtoSizer(t *testing.T) { 23 | marshaler := &ProtoMarshaler{} 24 | md := NewMetrics() 25 | md.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty().Metrics().AppendEmpty().SetName("foo") 26 | 27 | size := marshaler.MetricsSize(md) 28 | 29 | bytes, err := marshaler.MarshalMetrics(md) 30 | require.NoError(t, err) 31 | assert.Equal(t, len(bytes), size) 32 | } 33 | 34 | func TestProtoSizerEmptyMetrics(t *testing.T) { 35 | sizer := &ProtoMarshaler{} 36 | assert.Equal(t, 0, sizer.MetricsSize(NewMetrics())) 37 | } 38 | 39 | func BenchmarkMetricsToProto(b *testing.B) { 40 | marshaler := &ProtoMarshaler{} 41 | metrics := generateBenchmarkMetrics(128) 42 | b.ResetTimer() 43 | for n := 0; n < b.N; n++ { 44 | buf, err := marshaler.MarshalMetrics(metrics) 45 | require.NoError(b, err) 46 | assert.NotEqual(b, 0, len(buf)) 47 | } 48 | } 49 | 50 | func BenchmarkMetricsFromProto(b *testing.B) { 51 | marshaler := &ProtoMarshaler{} 52 | unmarshaler := &ProtoUnmarshaler{} 53 | baseMetrics := generateBenchmarkMetrics(128) 54 | buf, err := marshaler.MarshalMetrics(baseMetrics) 55 | require.NoError(b, err) 56 | assert.NotEqual(b, 0, len(buf)) 57 | b.ResetTimer() 58 | b.ReportAllocs() 59 | for n := 0; n < b.N; n++ { 60 | metrics, err := unmarshaler.UnmarshalMetrics(buf) 61 | require.NoError(b, err) 62 | assert.Equal(b, baseMetrics.ResourceMetrics().Len(), metrics.ResourceMetrics().Len()) 63 | } 64 | } 65 | 66 | func generateBenchmarkMetrics(metricsCount int) Metrics { 67 | now := time.Now() 68 | startTime := pcommon.NewTimestampFromTime(now.Add(-10 * time.Second)) 69 | endTime := pcommon.NewTimestampFromTime(now) 70 | 71 | md := NewMetrics() 72 | ilm := md.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty() 73 | ilm.Metrics().EnsureCapacity(metricsCount) 74 | for i := 0; i < metricsCount; i++ { 75 | im := ilm.Metrics().AppendEmpty() 76 | im.SetName("test_name") 77 | idp := im.SetEmptySum().DataPoints().AppendEmpty() 78 | idp.SetStartTimestamp(startTime) 79 | idp.SetTimestamp(endTime) 80 | idp.SetIntValue(123) 81 | } 82 | return md 83 | } 84 | -------------------------------------------------------------------------------- /otel-compressor/pdata/pmetric/pmetricotlp/package_test.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package pmetricotlp 5 | 6 | import ( 7 | "testing" 8 | 9 | "go.uber.org/goleak" 10 | ) 11 | 12 | func TestMain(m *testing.M) { 13 | goleak.VerifyTestMain(m) 14 | } 15 | -------------------------------------------------------------------------------- /otel-compressor/pdata/pmetric/pmetricotlp/request.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package pmetricotlp // import "go.opentelemetry.io/collector/pdata/pmetric/pmetricotlp" 5 | 6 | import ( 7 | "bytes" 8 | 9 | "go.opentelemetry.io/collector/pdata/internal" 10 | otlpcollectormetrics "go.opentelemetry.io/collector/pdata/internal/data/protogen/collector/metrics/v1" 11 | "go.opentelemetry.io/collector/pdata/internal/json" 12 | "go.opentelemetry.io/collector/pdata/pmetric" 13 | ) 14 | 15 | var jsonUnmarshaler = &pmetric.JSONUnmarshaler{} 16 | 17 | // ExportRequest represents the request for gRPC/HTTP client/server. 18 | // It's a wrapper for pmetric.Metrics data. 19 | type ExportRequest struct { 20 | orig *otlpcollectormetrics.ExportMetricsServiceRequest 21 | state *internal.State 22 | } 23 | 24 | // NewExportRequest returns an empty ExportRequest. 25 | func NewExportRequest() ExportRequest { 26 | state := internal.StateMutable 27 | return ExportRequest{ 28 | orig: &otlpcollectormetrics.ExportMetricsServiceRequest{}, 29 | state: &state, 30 | } 31 | } 32 | 33 | // NewExportRequestFromMetrics returns a ExportRequest from pmetric.Metrics. 34 | // Because ExportRequest is a wrapper for pmetric.Metrics, 35 | // any changes to the provided Metrics struct will be reflected in the ExportRequest and vice versa. 36 | func NewExportRequestFromMetrics(md pmetric.Metrics) ExportRequest { 37 | return ExportRequest{ 38 | orig: internal.GetOrigMetrics(internal.Metrics(md)), 39 | state: internal.GetMetricsState(internal.Metrics(md)), 40 | } 41 | } 42 | 43 | // MarshalProto marshals ExportRequest into proto bytes. 44 | func (ms ExportRequest) MarshalProto() ([]byte, error) { 45 | return ms.orig.Marshal() 46 | } 47 | 48 | // UnmarshalProto unmarshalls ExportRequest from proto bytes. 49 | func (ms ExportRequest) UnmarshalProto(data []byte) error { 50 | return ms.orig.Unmarshal(data) 51 | } 52 | 53 | // MarshalJSON marshals ExportRequest into JSON bytes. 54 | func (ms ExportRequest) MarshalJSON() ([]byte, error) { 55 | var buf bytes.Buffer 56 | if err := json.Marshal(&buf, ms.orig); err != nil { 57 | return nil, err 58 | } 59 | return buf.Bytes(), nil 60 | } 61 | 62 | // UnmarshalJSON unmarshalls ExportRequest from JSON bytes. 63 | func (ms ExportRequest) UnmarshalJSON(data []byte) error { 64 | md, err := jsonUnmarshaler.UnmarshalMetrics(data) 65 | if err != nil { 66 | return err 67 | } 68 | *ms.orig = *internal.GetOrigMetrics(internal.Metrics(md)) 69 | return nil 70 | } 71 | 72 | func (ms ExportRequest) Metrics() pmetric.Metrics { 73 | return pmetric.Metrics(internal.NewMetrics(ms.orig, ms.state)) 74 | } 75 | -------------------------------------------------------------------------------- /otel-compressor/pdata/pmetric/pmetricotlp/request_test.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package pmetricotlp 5 | 6 | import ( 7 | "encoding/json" 8 | "strings" 9 | "testing" 10 | 11 | "github.com/stretchr/testify/assert" 12 | ) 13 | 14 | var _ json.Unmarshaler = ExportRequest{} 15 | var _ json.Marshaler = ExportRequest{} 16 | 17 | var metricsRequestJSON = []byte(` 18 | { 19 | "resourceMetrics": [ 20 | { 21 | "resource": {}, 22 | "scopeMetrics": [ 23 | { 24 | "scope": {}, 25 | "metrics": [ 26 | { 27 | "name": "test_metric" 28 | } 29 | ] 30 | } 31 | ] 32 | } 33 | ] 34 | }`) 35 | 36 | func TestRequestToPData(t *testing.T) { 37 | tr := NewExportRequest() 38 | assert.Equal(t, tr.Metrics().MetricCount(), 0) 39 | tr.Metrics().ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty().Metrics().AppendEmpty() 40 | assert.Equal(t, tr.Metrics().MetricCount(), 1) 41 | } 42 | 43 | func TestRequestJSON(t *testing.T) { 44 | mr := NewExportRequest() 45 | assert.NoError(t, mr.UnmarshalJSON(metricsRequestJSON)) 46 | assert.Equal(t, "test_metric", mr.Metrics().ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics().At(0).Name()) 47 | 48 | got, err := mr.MarshalJSON() 49 | assert.NoError(t, err) 50 | assert.Equal(t, strings.Join(strings.Fields(string(metricsRequestJSON)), ""), string(got)) 51 | } 52 | -------------------------------------------------------------------------------- /otel-compressor/pdata/pmetric/pmetricotlp/response_test.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package pmetricotlp 5 | 6 | import ( 7 | "encoding/json" 8 | "testing" 9 | 10 | jsoniter "github.com/json-iterator/go" 11 | "github.com/stretchr/testify/assert" 12 | ) 13 | 14 | var _ json.Unmarshaler = ExportResponse{} 15 | var _ json.Marshaler = ExportResponse{} 16 | 17 | func TestExportResponseJSON(t *testing.T) { 18 | jsonStr := `{"partialSuccess": {"rejectedDataPoints":1, "errorMessage":"nothing"}}` 19 | val := NewExportResponse() 20 | assert.NoError(t, val.UnmarshalJSON([]byte(jsonStr))) 21 | expected := NewExportResponse() 22 | expected.PartialSuccess().SetRejectedDataPoints(1) 23 | expected.PartialSuccess().SetErrorMessage("nothing") 24 | assert.Equal(t, expected, val) 25 | } 26 | 27 | func TestUnmarshalJSONExportResponse(t *testing.T) { 28 | jsonStr := `{"extra":"", "partialSuccess": {}}` 29 | val := NewExportResponse() 30 | assert.NoError(t, val.UnmarshalJSON([]byte(jsonStr))) 31 | assert.Equal(t, NewExportResponse(), val) 32 | } 33 | 34 | func TestUnmarshalJsoniterExportPartialSuccess(t *testing.T) { 35 | jsonStr := `{"extra":""}` 36 | iter := jsoniter.ConfigFastest.BorrowIterator([]byte(jsonStr)) 37 | defer jsoniter.ConfigFastest.ReturnIterator(iter) 38 | val := NewExportPartialSuccess() 39 | val.unmarshalJsoniter(iter) 40 | assert.NoError(t, iter.Error) 41 | assert.Equal(t, NewExportPartialSuccess(), val) 42 | } 43 | -------------------------------------------------------------------------------- /otel-compressor/pdata/ptrace/encoding.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package ptrace // import "go.opentelemetry.io/collector/pdata/ptrace" 5 | 6 | // MarshalSizer is the interface that groups the basic Marshal and Size methods 7 | type MarshalSizer interface { 8 | Marshaler 9 | Sizer 10 | } 11 | 12 | // Marshaler marshals pdata.Traces into bytes. 13 | type Marshaler interface { 14 | // MarshalTraces the given pdata.Traces into bytes. 15 | // If the error is not nil, the returned bytes slice cannot be used. 16 | MarshalTraces(td Traces) ([]byte, error) 17 | } 18 | 19 | // Unmarshaler unmarshalls bytes into pdata.Traces. 20 | type Unmarshaler interface { 21 | // UnmarshalTraces the given bytes into pdata.Traces. 22 | // If the error is not nil, the returned pdata.Traces cannot be used. 23 | UnmarshalTraces(buf []byte) (Traces, error) 24 | } 25 | 26 | // Sizer is an optional interface implemented by the Marshaler, 27 | // that calculates the size of a marshaled Traces. 28 | type Sizer interface { 29 | // TracesSize returns the size in bytes of a marshaled Traces. 30 | TracesSize(td Traces) int 31 | } 32 | -------------------------------------------------------------------------------- /otel-compressor/pdata/ptrace/generated_scopespans_test.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | // Code generated by "pdata/internal/cmd/pdatagen/main.go". DO NOT EDIT. 5 | // To regenerate this file run "make genpdata". 6 | 7 | package ptrace 8 | 9 | import ( 10 | "testing" 11 | 12 | "github.com/stretchr/testify/assert" 13 | 14 | "go.opentelemetry.io/collector/pdata/internal" 15 | otlptrace "go.opentelemetry.io/collector/pdata/internal/data/protogen/trace/v1" 16 | "go.opentelemetry.io/collector/pdata/pcommon" 17 | ) 18 | 19 | func TestScopeSpans_MoveTo(t *testing.T) { 20 | ms := generateTestScopeSpans() 21 | dest := NewScopeSpans() 22 | ms.MoveTo(dest) 23 | assert.Equal(t, NewScopeSpans(), ms) 24 | assert.Equal(t, generateTestScopeSpans(), dest) 25 | sharedState := internal.StateReadOnly 26 | assert.Panics(t, func() { ms.MoveTo(newScopeSpans(&otlptrace.ScopeSpans{}, &sharedState)) }) 27 | assert.Panics(t, func() { newScopeSpans(&otlptrace.ScopeSpans{}, &sharedState).MoveTo(dest) }) 28 | } 29 | 30 | func TestScopeSpans_CopyTo(t *testing.T) { 31 | ms := NewScopeSpans() 32 | orig := NewScopeSpans() 33 | orig.CopyTo(ms) 34 | assert.Equal(t, orig, ms) 35 | orig = generateTestScopeSpans() 36 | orig.CopyTo(ms) 37 | assert.Equal(t, orig, ms) 38 | sharedState := internal.StateReadOnly 39 | assert.Panics(t, func() { ms.CopyTo(newScopeSpans(&otlptrace.ScopeSpans{}, &sharedState)) }) 40 | } 41 | 42 | func TestScopeSpans_Scope(t *testing.T) { 43 | ms := NewScopeSpans() 44 | internal.FillTestInstrumentationScope(internal.InstrumentationScope(ms.Scope())) 45 | assert.Equal(t, pcommon.InstrumentationScope(internal.GenerateTestInstrumentationScope()), ms.Scope()) 46 | } 47 | 48 | func TestScopeSpans_SchemaUrl(t *testing.T) { 49 | ms := NewScopeSpans() 50 | assert.Equal(t, "", ms.SchemaUrl()) 51 | ms.SetSchemaUrl("https://opentelemetry.io/schemas/1.5.0") 52 | assert.Equal(t, "https://opentelemetry.io/schemas/1.5.0", ms.SchemaUrl()) 53 | sharedState := internal.StateReadOnly 54 | assert.Panics(t, func() { 55 | newScopeSpans(&otlptrace.ScopeSpans{}, &sharedState).SetSchemaUrl("https://opentelemetry.io/schemas/1.5.0") 56 | }) 57 | } 58 | 59 | func TestScopeSpans_Spans(t *testing.T) { 60 | ms := NewScopeSpans() 61 | assert.Equal(t, NewSpanSlice(), ms.Spans()) 62 | fillTestSpanSlice(ms.Spans()) 63 | assert.Equal(t, generateTestSpanSlice(), ms.Spans()) 64 | } 65 | 66 | func generateTestScopeSpans() ScopeSpans { 67 | tv := NewScopeSpans() 68 | fillTestScopeSpans(tv) 69 | return tv 70 | } 71 | 72 | func fillTestScopeSpans(tv ScopeSpans) { 73 | internal.FillTestInstrumentationScope(internal.NewInstrumentationScope(&tv.orig.Scope, tv.state)) 74 | tv.orig.SchemaUrl = "https://opentelemetry.io/schemas/1.5.0" 75 | fillTestSpanSlice(newSpanSlice(&tv.orig.Spans, tv.state)) 76 | } 77 | -------------------------------------------------------------------------------- /otel-compressor/pdata/ptrace/generated_status.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | // Code generated by "pdata/internal/cmd/pdatagen/main.go". DO NOT EDIT. 5 | // To regenerate this file run "make genpdata". 6 | 7 | package ptrace 8 | 9 | import ( 10 | "go.opentelemetry.io/collector/pdata/internal" 11 | otlptrace "go.opentelemetry.io/collector/pdata/internal/data/protogen/trace/v1" 12 | ) 13 | 14 | // Status is an optional final status for this span. Semantically, when Status was not 15 | // set, that means the span ended without errors and to assume Status.Ok (code = 0). 16 | // 17 | // This is a reference type, if passed by value and callee modifies it the 18 | // caller will see the modification. 19 | // 20 | // Must use NewStatus function to create new instances. 21 | // Important: zero-initialized instance is not valid for use. 22 | type Status struct { 23 | orig *otlptrace.Status 24 | state *internal.State 25 | } 26 | 27 | func newStatus(orig *otlptrace.Status, state *internal.State) Status { 28 | return Status{orig: orig, state: state} 29 | } 30 | 31 | // NewStatus creates a new empty Status. 32 | // 33 | // This must be used only in testing code. Users should use "AppendEmpty" when part of a Slice, 34 | // OR directly access the member if this is embedded in another struct. 35 | func NewStatus() Status { 36 | state := internal.StateMutable 37 | return newStatus(&otlptrace.Status{}, &state) 38 | } 39 | 40 | // MoveTo moves all properties from the current struct overriding the destination and 41 | // resetting the current instance to its zero value 42 | func (ms Status) MoveTo(dest Status) { 43 | ms.state.AssertMutable() 44 | dest.state.AssertMutable() 45 | *dest.orig = *ms.orig 46 | *ms.orig = otlptrace.Status{} 47 | } 48 | 49 | // Code returns the code associated with this Status. 50 | func (ms Status) Code() StatusCode { 51 | return StatusCode(ms.orig.Code) 52 | } 53 | 54 | // SetCode replaces the code associated with this Status. 55 | func (ms Status) SetCode(v StatusCode) { 56 | ms.state.AssertMutable() 57 | ms.orig.Code = otlptrace.Status_StatusCode(v) 58 | } 59 | 60 | // Message returns the message associated with this Status. 61 | func (ms Status) Message() string { 62 | return ms.orig.Message 63 | } 64 | 65 | // SetMessage replaces the message associated with this Status. 66 | func (ms Status) SetMessage(v string) { 67 | ms.state.AssertMutable() 68 | ms.orig.Message = v 69 | } 70 | 71 | // CopyTo copies all properties from the current struct overriding the destination. 72 | func (ms Status) CopyTo(dest Status) { 73 | dest.state.AssertMutable() 74 | dest.SetCode(ms.Code()) 75 | dest.SetMessage(ms.Message()) 76 | } 77 | -------------------------------------------------------------------------------- /otel-compressor/pdata/ptrace/generated_status_test.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | // Code generated by "pdata/internal/cmd/pdatagen/main.go". DO NOT EDIT. 5 | // To regenerate this file run "make genpdata". 6 | 7 | package ptrace 8 | 9 | import ( 10 | "testing" 11 | 12 | "github.com/stretchr/testify/assert" 13 | 14 | "go.opentelemetry.io/collector/pdata/internal" 15 | otlptrace "go.opentelemetry.io/collector/pdata/internal/data/protogen/trace/v1" 16 | ) 17 | 18 | func TestStatus_MoveTo(t *testing.T) { 19 | ms := generateTestStatus() 20 | dest := NewStatus() 21 | ms.MoveTo(dest) 22 | assert.Equal(t, NewStatus(), ms) 23 | assert.Equal(t, generateTestStatus(), dest) 24 | sharedState := internal.StateReadOnly 25 | assert.Panics(t, func() { ms.MoveTo(newStatus(&otlptrace.Status{}, &sharedState)) }) 26 | assert.Panics(t, func() { newStatus(&otlptrace.Status{}, &sharedState).MoveTo(dest) }) 27 | } 28 | 29 | func TestStatus_CopyTo(t *testing.T) { 30 | ms := NewStatus() 31 | orig := NewStatus() 32 | orig.CopyTo(ms) 33 | assert.Equal(t, orig, ms) 34 | orig = generateTestStatus() 35 | orig.CopyTo(ms) 36 | assert.Equal(t, orig, ms) 37 | sharedState := internal.StateReadOnly 38 | assert.Panics(t, func() { ms.CopyTo(newStatus(&otlptrace.Status{}, &sharedState)) }) 39 | } 40 | 41 | func TestStatus_Code(t *testing.T) { 42 | ms := NewStatus() 43 | assert.Equal(t, StatusCode(0), ms.Code()) 44 | testValCode := StatusCode(1) 45 | ms.SetCode(testValCode) 46 | assert.Equal(t, testValCode, ms.Code()) 47 | } 48 | 49 | func TestStatus_Message(t *testing.T) { 50 | ms := NewStatus() 51 | assert.Equal(t, "", ms.Message()) 52 | ms.SetMessage("cancelled") 53 | assert.Equal(t, "cancelled", ms.Message()) 54 | sharedState := internal.StateReadOnly 55 | assert.Panics(t, func() { newStatus(&otlptrace.Status{}, &sharedState).SetMessage("cancelled") }) 56 | } 57 | 58 | func generateTestStatus() Status { 59 | tv := NewStatus() 60 | fillTestStatus(tv) 61 | return tv 62 | } 63 | 64 | func fillTestStatus(tv Status) { 65 | tv.orig.Code = 1 66 | tv.orig.Message = "cancelled" 67 | } 68 | -------------------------------------------------------------------------------- /otel-compressor/pdata/ptrace/package_test.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package ptrace 5 | 6 | import ( 7 | "testing" 8 | 9 | "go.uber.org/goleak" 10 | ) 11 | 12 | func TestMain(m *testing.M) { 13 | goleak.VerifyTestMain(m) 14 | } 15 | -------------------------------------------------------------------------------- /otel-compressor/pdata/ptrace/pb.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package ptrace // import "go.opentelemetry.io/collector/pdata/ptrace" 5 | 6 | import ( 7 | "go.opentelemetry.io/collector/pdata/internal" 8 | otlptrace "go.opentelemetry.io/collector/pdata/internal/data/protogen/trace/v1" 9 | ) 10 | 11 | var _ MarshalSizer = (*ProtoMarshaler)(nil) 12 | 13 | type ProtoMarshaler struct{} 14 | 15 | func (e *ProtoMarshaler) MarshalTraces(td Traces) ([]byte, error) { 16 | pb := internal.TracesToProto(internal.Traces(td)) 17 | return pb.Marshal() 18 | } 19 | 20 | func (e *ProtoMarshaler) TracesSize(td Traces) int { 21 | pb := internal.TracesToProto(internal.Traces(td)) 22 | return pb.Size() 23 | } 24 | 25 | type ProtoUnmarshaler struct{} 26 | 27 | func (d *ProtoUnmarshaler) UnmarshalTraces(buf []byte) (Traces, error) { 28 | pb := otlptrace.TracesData{} 29 | err := pb.Unmarshal(buf) 30 | return Traces(internal.TracesFromProto(pb)), err 31 | } 32 | -------------------------------------------------------------------------------- /otel-compressor/pdata/ptrace/pb_test.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package ptrace 5 | 6 | import ( 7 | "testing" 8 | "time" 9 | 10 | "github.com/stretchr/testify/assert" 11 | "github.com/stretchr/testify/require" 12 | 13 | "go.opentelemetry.io/collector/pdata/pcommon" 14 | ) 15 | 16 | func TestProtoTracesUnmarshalerError(t *testing.T) { 17 | p := &ProtoUnmarshaler{} 18 | _, err := p.UnmarshalTraces([]byte("+$%")) 19 | assert.Error(t, err) 20 | } 21 | 22 | func TestProtoSizer(t *testing.T) { 23 | marshaler := &ProtoMarshaler{} 24 | td := NewTraces() 25 | rms := td.ResourceSpans() 26 | rms.AppendEmpty().ScopeSpans().AppendEmpty().Spans().AppendEmpty().SetName("foo") 27 | 28 | size := marshaler.TracesSize(td) 29 | 30 | bytes, err := marshaler.MarshalTraces(td) 31 | require.NoError(t, err) 32 | assert.Equal(t, len(bytes), size) 33 | } 34 | 35 | func TestProtoSizerEmptyTraces(t *testing.T) { 36 | sizer := &ProtoMarshaler{} 37 | assert.Equal(t, 0, sizer.TracesSize(NewTraces())) 38 | } 39 | 40 | func BenchmarkTracesToProto(b *testing.B) { 41 | marshaler := &ProtoMarshaler{} 42 | traces := generateBenchmarkTraces(128) 43 | b.ResetTimer() 44 | for n := 0; n < b.N; n++ { 45 | buf, err := marshaler.MarshalTraces(traces) 46 | require.NoError(b, err) 47 | assert.NotEqual(b, 0, len(buf)) 48 | } 49 | } 50 | 51 | func BenchmarkTracesFromProto(b *testing.B) { 52 | marshaler := &ProtoMarshaler{} 53 | unmarshaler := &ProtoUnmarshaler{} 54 | baseTraces := generateBenchmarkTraces(128) 55 | buf, err := marshaler.MarshalTraces(baseTraces) 56 | require.NoError(b, err) 57 | assert.NotEqual(b, 0, len(buf)) 58 | b.ResetTimer() 59 | b.ReportAllocs() 60 | for n := 0; n < b.N; n++ { 61 | traces, err := unmarshaler.UnmarshalTraces(buf) 62 | require.NoError(b, err) 63 | assert.Equal(b, baseTraces.ResourceSpans().Len(), traces.ResourceSpans().Len()) 64 | } 65 | } 66 | 67 | func generateBenchmarkTraces(metricsCount int) Traces { 68 | now := time.Now() 69 | startTime := pcommon.NewTimestampFromTime(now.Add(-10 * time.Second)) 70 | endTime := pcommon.NewTimestampFromTime(now) 71 | 72 | md := NewTraces() 73 | ilm := md.ResourceSpans().AppendEmpty().ScopeSpans().AppendEmpty() 74 | ilm.Spans().EnsureCapacity(metricsCount) 75 | for i := 0; i < metricsCount; i++ { 76 | im := ilm.Spans().AppendEmpty() 77 | im.SetName("test_name") 78 | im.SetStartTimestamp(startTime) 79 | im.SetEndTimestamp(endTime) 80 | } 81 | return md 82 | } 83 | -------------------------------------------------------------------------------- /otel-compressor/pdata/ptrace/ptraceotlp/package_test.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package ptraceotlp 5 | 6 | import ( 7 | "testing" 8 | 9 | "go.uber.org/goleak" 10 | ) 11 | 12 | func TestMain(m *testing.M) { 13 | goleak.VerifyTestMain(m) 14 | } 15 | -------------------------------------------------------------------------------- /otel-compressor/pdata/ptrace/ptraceotlp/request_test.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package ptraceotlp 5 | 6 | import ( 7 | "testing" 8 | 9 | "github.com/stretchr/testify/assert" 10 | ) 11 | 12 | // var _ json.Unmarshaler = ExportRequest{} 13 | 14 | var tracesRequestJSON = []byte(` 15 | { 16 | "resourceSpans": [ 17 | { 18 | "resource": {}, 19 | "scopeSpans": [ 20 | { 21 | "scope": {}, 22 | "spans": [ 23 | { 24 | "traceId": "", 25 | "spanId":"", 26 | "parentSpanId":"", 27 | "name": "test_span", 28 | "status": {} 29 | } 30 | ] 31 | } 32 | ] 33 | } 34 | ] 35 | }`) 36 | 37 | func TestRequestToPData(t *testing.T) { 38 | tr := NewExportRequest() 39 | assert.Equal(t, tr.Traces().SpanCount(), 0) 40 | tr.Traces().ResourceSpans().AppendEmpty().ScopeSpans().AppendEmpty().Spans().AppendEmpty() 41 | assert.Equal(t, tr.Traces().SpanCount(), 1) 42 | } 43 | 44 | func TestRequestJSON(t *testing.T) { 45 | tr := NewExportRequest() 46 | assert.NoError(t, tr.UnmarshalJSON(tracesRequestJSON)) 47 | assert.Equal(t, "test_span", tr.Traces().ResourceSpans().At(0).ScopeSpans().At(0).Spans().At(0).Name()) 48 | 49 | // got, _, _, err := tr.MarshalWithTraceZip(100, 100, 100, 100, false) 50 | // assert.NoError(t, err) 51 | // assert.Equal(t, strings.Join(strings.Fields(string(tracesRequestJSON)), ""), string(got)) 52 | } 53 | -------------------------------------------------------------------------------- /otel-compressor/pdata/ptrace/ptraceotlp/response_test.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package ptraceotlp 5 | 6 | import ( 7 | "encoding/json" 8 | "testing" 9 | 10 | jsoniter "github.com/json-iterator/go" 11 | "github.com/stretchr/testify/assert" 12 | ) 13 | 14 | var _ json.Unmarshaler = ExportResponse{} 15 | var _ json.Marshaler = ExportResponse{} 16 | 17 | func TestExportResponseJSON(t *testing.T) { 18 | jsonStr := `{"partialSuccess": {"rejectedSpans":1, "errorMessage":"nothing"}}` 19 | val := NewExportResponse() 20 | assert.NoError(t, val.UnmarshalJSON([]byte(jsonStr))) 21 | expected := NewExportResponse() 22 | expected.PartialSuccess().SetRejectedSpans(1) 23 | expected.PartialSuccess().SetErrorMessage("nothing") 24 | assert.Equal(t, expected, val) 25 | } 26 | 27 | func TestUnmarshalJSONExportResponse(t *testing.T) { 28 | jsonStr := `{"extra":"", "partialSuccess": {}}` 29 | val := NewExportResponse() 30 | assert.NoError(t, val.UnmarshalJSON([]byte(jsonStr))) 31 | assert.Equal(t, NewExportResponse(), val) 32 | } 33 | 34 | func TestUnmarshalJsoniterExportPartialSuccess(t *testing.T) { 35 | jsonStr := `{"extra":""}` 36 | iter := jsoniter.ConfigFastest.BorrowIterator([]byte(jsonStr)) 37 | defer jsoniter.ConfigFastest.ReturnIterator(iter) 38 | val := NewExportPartialSuccess() 39 | val.unmarshalJsoniter(iter) 40 | assert.NoError(t, iter.Error) 41 | assert.Equal(t, NewExportPartialSuccess(), val) 42 | } 43 | -------------------------------------------------------------------------------- /otel-compressor/pdata/ptrace/span_kind.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package ptrace // import "go.opentelemetry.io/collector/pdata/ptrace" 5 | 6 | import ( 7 | otlptrace "go.opentelemetry.io/collector/pdata/internal/data/protogen/trace/v1" 8 | ) 9 | 10 | // SpanKind is the type of span. Can be used to specify additional relationships between spans 11 | // in addition to a parent/child relationship. 12 | type SpanKind int32 13 | 14 | const ( 15 | // SpanKindUnspecified represents that the SpanKind is unspecified, it MUST NOT be used. 16 | SpanKindUnspecified = SpanKind(otlptrace.Span_SPAN_KIND_UNSPECIFIED) 17 | // SpanKindInternal indicates that the span represents an internal operation within an application, 18 | // as opposed to an operation happening at the boundaries. Default value. 19 | SpanKindInternal = SpanKind(otlptrace.Span_SPAN_KIND_INTERNAL) 20 | // SpanKindServer indicates that the span covers server-side handling of an RPC or other 21 | // remote network request. 22 | SpanKindServer = SpanKind(otlptrace.Span_SPAN_KIND_SERVER) 23 | // SpanKindClient indicates that the span describes a request to some remote service. 24 | SpanKindClient = SpanKind(otlptrace.Span_SPAN_KIND_CLIENT) 25 | // SpanKindProducer indicates that the span describes a producer sending a message to a broker. 26 | // Unlike CLIENT and SERVER, there is often no direct critical path latency relationship 27 | // between producer and consumer spans. 28 | // A PRODUCER span ends when the message was accepted by the broker while the logical processing of 29 | // the message might span a much longer time. 30 | SpanKindProducer = SpanKind(otlptrace.Span_SPAN_KIND_PRODUCER) 31 | // SpanKindConsumer indicates that the span describes consumer receiving a message from a broker. 32 | // Like the PRODUCER kind, there is often no direct critical path latency relationship between 33 | // producer and consumer spans. 34 | SpanKindConsumer = SpanKind(otlptrace.Span_SPAN_KIND_CONSUMER) 35 | ) 36 | 37 | // String returns the string representation of the SpanKind. 38 | func (sk SpanKind) String() string { 39 | switch sk { 40 | case SpanKindUnspecified: 41 | return "Unspecified" 42 | case SpanKindInternal: 43 | return "Internal" 44 | case SpanKindServer: 45 | return "Server" 46 | case SpanKindClient: 47 | return "Client" 48 | case SpanKindProducer: 49 | return "Producer" 50 | case SpanKindConsumer: 51 | return "Consumer" 52 | } 53 | return "" 54 | } 55 | -------------------------------------------------------------------------------- /otel-compressor/pdata/ptrace/span_kind_test.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package ptrace // import "go.opentelemetry.io/collector/pdata/ptrace" 5 | 6 | import ( 7 | "testing" 8 | 9 | "github.com/stretchr/testify/assert" 10 | ) 11 | 12 | func TestSpanKindString(t *testing.T) { 13 | assert.EqualValues(t, "Unspecified", SpanKindUnspecified.String()) 14 | assert.EqualValues(t, "Internal", SpanKindInternal.String()) 15 | assert.EqualValues(t, "Server", SpanKindServer.String()) 16 | assert.EqualValues(t, "Client", SpanKindClient.String()) 17 | assert.EqualValues(t, "Producer", SpanKindProducer.String()) 18 | assert.EqualValues(t, "Consumer", SpanKindConsumer.String()) 19 | assert.EqualValues(t, "", SpanKind(100).String()) 20 | } 21 | -------------------------------------------------------------------------------- /otel-compressor/pdata/ptrace/status_code.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package ptrace // import "go.opentelemetry.io/collector/pdata/ptrace" 5 | 6 | import ( 7 | otlptrace "go.opentelemetry.io/collector/pdata/internal/data/protogen/trace/v1" 8 | ) 9 | 10 | // StatusCode mirrors the codes defined at 11 | // https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/api.md#set-status 12 | type StatusCode int32 13 | 14 | const ( 15 | StatusCodeUnset = StatusCode(otlptrace.Status_STATUS_CODE_UNSET) 16 | StatusCodeOk = StatusCode(otlptrace.Status_STATUS_CODE_OK) 17 | StatusCodeError = StatusCode(otlptrace.Status_STATUS_CODE_ERROR) 18 | ) 19 | 20 | // String returns the string representation of the StatusCode. 21 | func (sc StatusCode) String() string { 22 | switch sc { 23 | case StatusCodeUnset: 24 | return "Unset" 25 | case StatusCodeOk: 26 | return "Ok" 27 | case StatusCodeError: 28 | return "Error" 29 | } 30 | return "" 31 | } 32 | -------------------------------------------------------------------------------- /otel-compressor/pdata/ptrace/status_code_test.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package ptrace // import "go.opentelemetry.io/collector/pdata/ptrace" 5 | 6 | import ( 7 | "testing" 8 | 9 | "github.com/stretchr/testify/assert" 10 | ) 11 | 12 | func TestStatusCodeString(t *testing.T) { 13 | assert.EqualValues(t, "Unset", StatusCodeUnset.String()) 14 | assert.EqualValues(t, "Ok", StatusCodeOk.String()) 15 | assert.EqualValues(t, "Error", StatusCodeError.String()) 16 | assert.EqualValues(t, "", StatusCode(100).String()) 17 | } 18 | -------------------------------------------------------------------------------- /otel-compressor/pdata/ptrace/traces.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package ptrace // import "go.opentelemetry.io/collector/pdata/ptrace" 5 | 6 | import ( 7 | "go.opentelemetry.io/collector/pdata/internal" 8 | otlpcollectortrace "go.opentelemetry.io/collector/pdata/internal/data/protogen/collector/trace/v1" 9 | ) 10 | 11 | // Traces is the top-level struct that is propagated through the traces pipeline. 12 | // Use NewTraces to create new instance, zero-initialized instance is not valid for use. 13 | type Traces internal.Traces 14 | 15 | func newTraces(orig *otlpcollectortrace.ExportTraceServiceRequest) Traces { 16 | state := internal.StateMutable 17 | return Traces(internal.NewTraces(orig, &state)) 18 | } 19 | 20 | func (ms Traces) getOrig() *otlpcollectortrace.ExportTraceServiceRequest { 21 | return internal.GetOrigTraces(internal.Traces(ms)) 22 | } 23 | 24 | func (ms Traces) getState() *internal.State { 25 | return internal.GetTracesState(internal.Traces(ms)) 26 | } 27 | 28 | // NewTraces creates a new Traces struct. 29 | func NewTraces() Traces { 30 | return newTraces(&otlpcollectortrace.ExportTraceServiceRequest{}) 31 | } 32 | 33 | // IsReadOnly returns true if this Traces instance is read-only. 34 | func (ms Traces) IsReadOnly() bool { 35 | return *ms.getState() == internal.StateReadOnly 36 | } 37 | 38 | // CopyTo copies the Traces instance overriding the destination. 39 | func (ms Traces) CopyTo(dest Traces) { 40 | ms.ResourceSpans().CopyTo(dest.ResourceSpans()) 41 | } 42 | 43 | // SpanCount calculates the total number of spans. 44 | func (ms Traces) SpanCount() int { 45 | spanCount := 0 46 | rss := ms.ResourceSpans() 47 | for i := 0; i < rss.Len(); i++ { 48 | rs := rss.At(i) 49 | ilss := rs.ScopeSpans() 50 | for j := 0; j < ilss.Len(); j++ { 51 | spanCount += ilss.At(j).Spans().Len() 52 | } 53 | } 54 | return spanCount 55 | } 56 | 57 | // ResourceSpans returns the ResourceSpansSlice associated with this Metrics. 58 | func (ms Traces) ResourceSpans() ResourceSpansSlice { 59 | return newResourceSpansSlice(&ms.getOrig().ResourceSpans, internal.GetTracesState(internal.Traces(ms))) 60 | } 61 | 62 | // MarkReadOnly marks the Traces as shared so that no further modifications can be done on it. 63 | func (ms Traces) MarkReadOnly() { 64 | internal.SetTracesState(internal.Traces(ms), internal.StateReadOnly) 65 | } 66 | -------------------------------------------------------------------------------- /otel-compressor/prefix-compressed-exporter/internal/metadata/status.go: -------------------------------------------------------------------------------- 1 | package metadata 2 | 3 | import ( 4 | "go.opentelemetry.io/otel/metric" 5 | "go.opentelemetry.io/otel/trace" 6 | 7 | "go.opentelemetry.io/collector/component" 8 | ) 9 | 10 | var ( 11 | Type = component.MustNewType("prefix_compressed_exporter") 12 | scopeName = "angrychow/otel/prefix-compressed-exporter" 13 | ) 14 | 15 | const ( 16 | LogsStability = component.StabilityLevelBeta 17 | TracesStability = component.StabilityLevelStable 18 | MetricsStability = component.StabilityLevelStable 19 | ) 20 | 21 | func Meter(settings component.TelemetrySettings) metric.Meter { 22 | return settings.MeterProvider.Meter(scopeName) 23 | } 24 | 25 | func Tracer(settings component.TelemetrySettings) trace.Tracer { 26 | return settings.TracerProvider.Tracer(scopeName) 27 | } 28 | -------------------------------------------------------------------------------- /otel-compressor/prefix-compressed-receiver/internal/errors/errors.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package errors // import "go.opentelemetry.io/collector/receiver/otlpreceiver/internal/errors" 5 | 6 | import ( 7 | "google.golang.org/grpc/codes" 8 | "google.golang.org/grpc/status" 9 | 10 | "go.opentelemetry.io/collector/consumer/consumererror" 11 | ) 12 | 13 | func GetStatusFromError(err error) error { 14 | s, ok := status.FromError(err) 15 | if !ok { 16 | // Default to a retryable error 17 | // https://github.com/open-telemetry/opentelemetry-proto/blob/main/docs/specification.md#failures 18 | code := codes.Unavailable 19 | if consumererror.IsPermanent(err) { 20 | // If an error is permanent but doesn't have an attached gRPC status, assume it is server-side. 21 | code = codes.Internal 22 | } 23 | s = status.New(code, err.Error()) 24 | } 25 | return s.Err() 26 | } 27 | -------------------------------------------------------------------------------- /otel-compressor/prefix-compressed-receiver/internal/logs/otlp.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package logs // import "go.opentelemetry.io/collector/receiver/otlpreceiver/internal/logs" 5 | 6 | import ( 7 | "context" 8 | 9 | "angrychow/otel/prefix-compressed-receiver/internal/errors" 10 | 11 | "go.opentelemetry.io/collector/consumer" 12 | "go.opentelemetry.io/collector/pdata/plog/plogotlp" 13 | "go.opentelemetry.io/collector/receiver/receiverhelper" 14 | ) 15 | 16 | const dataFormatProtobuf = "protobuf" 17 | 18 | // Receiver is the type used to handle logs from OpenTelemetry exporters. 19 | type Receiver struct { 20 | plogotlp.UnimplementedGRPCServer 21 | nextConsumer consumer.Logs 22 | obsreport *receiverhelper.ObsReport 23 | } 24 | 25 | // New creates a new Receiver reference. 26 | func New(nextConsumer consumer.Logs, obsreport *receiverhelper.ObsReport) *Receiver { 27 | return &Receiver{ 28 | nextConsumer: nextConsumer, 29 | obsreport: obsreport, 30 | } 31 | } 32 | 33 | // Export implements the service Export logs func. 34 | func (r *Receiver) Export(ctx context.Context, req plogotlp.ExportRequest) (plogotlp.ExportResponse, error) { 35 | ld := req.Logs() 36 | numSpans := ld.LogRecordCount() 37 | if numSpans == 0 { 38 | return plogotlp.NewExportResponse(), nil 39 | } 40 | 41 | ctx = r.obsreport.StartLogsOp(ctx) 42 | err := r.nextConsumer.ConsumeLogs(ctx, ld) 43 | r.obsreport.EndLogsOp(ctx, dataFormatProtobuf, numSpans, err) 44 | 45 | // Use appropriate status codes for permanent/non-permanent errors 46 | // If we return the error straightaway, then the grpc implementation will set status code to Unknown 47 | // Refer: https://github.com/grpc/grpc-go/blob/v1.59.0/server.go#L1345 48 | // So, convert the error to appropriate grpc status and return the error 49 | // NonPermanent errors will be converted to codes.Unavailable (equivalent to HTTP 503) 50 | // Permanent errors will be converted to codes.InvalidArgument (equivalent to HTTP 400) 51 | if err != nil { 52 | return plogotlp.NewExportResponse(), errors.GetStatusFromError(err) 53 | } 54 | 55 | return plogotlp.NewExportResponse(), nil 56 | } 57 | -------------------------------------------------------------------------------- /otel-compressor/prefix-compressed-receiver/internal/metadata/status.go: -------------------------------------------------------------------------------- 1 | package metadata 2 | 3 | import ( 4 | "go.opentelemetry.io/otel/metric" 5 | "go.opentelemetry.io/otel/trace" 6 | 7 | "go.opentelemetry.io/collector/component" 8 | ) 9 | 10 | var ( 11 | Type = component.MustNewType("prefix_compressed_receiver") 12 | scopeName = "go.opentelemetry.io/collector/receiver/otlpreceiver" 13 | ) 14 | 15 | const ( 16 | LogsStability = component.StabilityLevelBeta 17 | TracesStability = component.StabilityLevelStable 18 | MetricsStability = component.StabilityLevelStable 19 | ) 20 | 21 | func Meter(settings component.TelemetrySettings) metric.Meter { 22 | return settings.MeterProvider.Meter(scopeName) 23 | } 24 | 25 | func Tracer(settings component.TelemetrySettings) trace.Tracer { 26 | return settings.TracerProvider.Tracer(scopeName) 27 | } 28 | -------------------------------------------------------------------------------- /otel-compressor/prefix-compressed-receiver/internal/metrics/otlp.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package metrics // import "go.opentelemetry.io/collector/receiver/otlpreceiver/internal/metrics" 5 | 6 | import ( 7 | "context" 8 | 9 | "angrychow/otel/prefix-compressed-receiver/internal/errors" 10 | 11 | "go.opentelemetry.io/collector/consumer" 12 | "go.opentelemetry.io/collector/pdata/pmetric/pmetricotlp" 13 | "go.opentelemetry.io/collector/receiver/receiverhelper" 14 | ) 15 | 16 | const dataFormatProtobuf = "protobuf" 17 | 18 | // Receiver is the type used to handle metrics from OpenTelemetry exporters. 19 | type Receiver struct { 20 | pmetricotlp.UnimplementedGRPCServer 21 | nextConsumer consumer.Metrics 22 | obsreport *receiverhelper.ObsReport 23 | } 24 | 25 | // New creates a new Receiver reference. 26 | func New(nextConsumer consumer.Metrics, obsreport *receiverhelper.ObsReport) *Receiver { 27 | return &Receiver{ 28 | nextConsumer: nextConsumer, 29 | obsreport: obsreport, 30 | } 31 | } 32 | 33 | // Export implements the service Export metrics func. 34 | func (r *Receiver) Export(ctx context.Context, req pmetricotlp.ExportRequest) (pmetricotlp.ExportResponse, error) { 35 | md := req.Metrics() 36 | dataPointCount := md.DataPointCount() 37 | if dataPointCount == 0 { 38 | return pmetricotlp.NewExportResponse(), nil 39 | } 40 | 41 | ctx = r.obsreport.StartMetricsOp(ctx) 42 | err := r.nextConsumer.ConsumeMetrics(ctx, md) 43 | r.obsreport.EndMetricsOp(ctx, dataFormatProtobuf, dataPointCount, err) 44 | 45 | // Use appropriate status codes for permanent/non-permanent errors 46 | // If we return the error straightaway, then the grpc implementation will set status code to Unknown 47 | // Refer: https://github.com/grpc/grpc-go/blob/v1.59.0/server.go#L1345 48 | // So, convert the error to appropriate grpc status and return the error 49 | // NonPermanent errors will be converted to codes.Unavailable (equivalent to HTTP 503) 50 | // Permanent errors will be converted to codes.InvalidArgument (equivalent to HTTP 400) 51 | if err != nil { 52 | return pmetricotlp.NewExportResponse(), errors.GetStatusFromError(err) 53 | } 54 | 55 | return pmetricotlp.NewExportResponse(), nil 56 | } 57 | -------------------------------------------------------------------------------- /otel-compressor/prefix-compressed-receiver/internal/trace/otlp.go: -------------------------------------------------------------------------------- 1 | // Copyright The OpenTelemetry Authors 2 | // SPDX-License-Identifier: Apache-2.0 3 | 4 | package trace // import "go.opentelemetry.io/collector/receiver/otlpreceiver/internal/trace" 5 | 6 | import ( 7 | "context" 8 | 9 | "angrychow/otel/prefix-compressed-receiver/internal/errors" 10 | 11 | "go.opentelemetry.io/collector/consumer" 12 | "go.opentelemetry.io/collector/pdata/ptrace/ptraceotlp" 13 | "go.opentelemetry.io/collector/receiver/receiverhelper" 14 | ) 15 | 16 | const dataFormatProtobuf = "protobuf" 17 | 18 | // Receiver is the type used to handle spans from OpenTelemetry exporters. 19 | type Receiver struct { 20 | ptraceotlp.UnimplementedGRPCServer 21 | nextConsumer consumer.Traces 22 | obsreport *receiverhelper.ObsReport 23 | } 24 | 25 | // New creates a new Receiver reference. 26 | func New(nextConsumer consumer.Traces, obsreport *receiverhelper.ObsReport) *Receiver { 27 | return &Receiver{ 28 | nextConsumer: nextConsumer, 29 | obsreport: obsreport, 30 | } 31 | } 32 | 33 | // Export implements the service Export traces func. 34 | func (r *Receiver) Export(ctx context.Context, req ptraceotlp.ExportRequest) (ptraceotlp.ExportResponse, error) { 35 | td := req.Traces() 36 | // We need to ensure that it propagates the receiver name as a tag 37 | numSpans := td.SpanCount() 38 | if numSpans == 0 { 39 | return ptraceotlp.NewExportResponse(), nil 40 | } 41 | 42 | ctx = r.obsreport.StartTracesOp(ctx) 43 | err := r.nextConsumer.ConsumeTraces(ctx, td) 44 | r.obsreport.EndTracesOp(ctx, dataFormatProtobuf, numSpans, err) 45 | 46 | // Use appropriate status codes for permanent/non-permanent errors 47 | // If we return the error straightaway, then the grpc implementation will set status code to Unknown 48 | // Refer: https://github.com/grpc/grpc-go/blob/v1.59.0/server.go#L1345 49 | // So, convert the error to appropriate grpc status and return the error 50 | // NonPermanent errors will be converted to codes.Unavailable (equivalent to HTTP 503) 51 | // Permanent errors will be converted to codes.InvalidArgument (equivalent to HTTP 400) 52 | if err != nil { 53 | return ptraceotlp.NewExportResponse(), errors.GetStatusFromError(err) 54 | } 55 | 56 | return ptraceotlp.NewExportResponse(), nil 57 | } 58 | -------------------------------------------------------------------------------- /otel-compressor/wrk/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "dependencies": { 3 | "node-fetch": "^3.3.2" 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /otel-compressor/wrk/readme.md: -------------------------------------------------------------------------------- 1 | # Send Files to OpenTelemetry Endpoint 2 | 3 | This script reads `.txt` files from a specified folder, merges their contents up to a specified size, and sends the merged data to an OpenTelemetry endpoint. It also calculates and logs throughput statistics during the process. 4 | 5 | ## Requirements 6 | 7 | - Node.js 8 | - Dependencies: `node-fetch` 9 | 10 | Install `node-fetch` using: 11 | ```bash 12 | npm install node-fetch 13 | ``` 14 | 15 | ## Usage 16 | 17 | Run the script with: 18 | ```bash 19 | node index.mjs 20 | ``` 21 | 22 | - ``: Path to the folder containing `.txt` files or `.json` files (default: `spans`). 23 | - ``: Number of files to merge in each batch (default: `1`). 24 | - ``: The OpenTelemetry endpoint to send data to (default: `http://127.0.0.1:4318/v1/traces`). 25 | - ``: The time interval between each request is in milliseconds (default value: 1000). 26 | 27 | Specifically, if you want to send the dataset we provided, you need to categorize all the span data by hostname and send them accordingly. You can find the hostname in the attributes of the resource_spans in each spans file. 28 | 29 | ## Notes 30 | 31 | 1. The script assumes that `.txt` or `.json` files contain valid JSON. 32 | 2. The OpenTelemetry endpoint URL can be specified as a parameter. 33 | 34 | ## Example Output 35 | 36 | When running the script, you will see logs like: 37 | ``` 38 | Total size sent: 1.23 MB. 39 | Current throughput: 512.34 KB/second. 40 | ``` 41 | 42 | These logs indicate the total data sent and the current throughput. 43 | -------------------------------------------------------------------------------- /static-compressor/go.mod: -------------------------------------------------------------------------------- 1 | module trie 2 | 3 | go 1.21 4 | -------------------------------------------------------------------------------- /static-compressor/readme.md: -------------------------------------------------------------------------------- 1 | ## TraceZip Algorithm Validate 2 | 3 | This repository is used to show the structure behind spans. 4 | 5 | For now, it is deliberatly designed to compress `alibaba-cluster-trace-data-v2022/CallGraph` data, but you can modify it and make it is able to compress any kind of spans data which is represented as CSV file. 6 | 7 | Even though the redundancy of the spans data in `alibaba-cluster-trace-data-v2022/CallGraph` is lower compared to the spans generated by OpenTelemetry, we found that static compression using TraceZip still yields good results. During static compression, we considered the internal redundancy of the strings within the `alibaba-cluster-trace-data-v2022/CallGraph` spans, and the large buffer size of TraceZip helps it identify global redundant strings. 8 | 9 | We noticed that in the Alibaba dataset, some of the span data formats do not match the CSV format (specifically, certain rows have fewer or more attribute values than the number specified in the header). We have directly ignored these rows. The number of such rows is very small. 10 | 11 | To Compress spans, running it by `go run . -path -dirname -chunk -j -merging` 12 | 13 | To Decompress spans, running it by `go run . -dirname -decompress` 14 | 15 | This program supports several command-line flags to configure its behavior. Below is a detailed explanation of each flag: 16 | 17 | | **Flag** | **Type** | **Default** | **Description** | 18 | |----------------|-----------|-------------|-----------------------------------------------------------------------------| 19 | | `-path` | `string` | `""` | Path of the file to be compressed (**Required for compression**). | 20 | | `-chunk` | `int` | `0` | Chunk size for processing data (`0` means processing the entire file at once). | 21 | | `-j` | `int` | `1` | Number of CPU cores to use. | 22 | | `-merging` | `bool` | `false` | Enables Merging Tree compression (`true` enables it). | 23 | | `-decompress` | `bool` | `false` | Enables decompression mode (**Used with `-dirname`**). | 24 | | `-dirname` | `string` | `"output"` | Directory name for storing compressed files (used for both compression and decompression). | 25 | | `-not_alibaba`| `bool` | `false` | Attribute sorting mode: | 26 | | | | | - `true`: Sorts attributes by optional value counts (ascending order). | 27 | | | | | - `false`: Uses a predefined attribute order. | -------------------------------------------------------------------------------- /train-ticket-workload/.gitignore: -------------------------------------------------------------------------------- 1 | __pycache__/ 2 | -------------------------------------------------------------------------------- /train-ticket-workload/query_scaffold/.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | pip-wheel-metadata/ 24 | share/python-wheels/ 25 | *.egg-info/ 26 | .installed.cfg 27 | *.egg 28 | MANIFEST 29 | 30 | # PyInstaller 31 | # Usually these files are written by a python script from a template 32 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 33 | *.manifest 34 | *.spec 35 | 36 | # Installer logs 37 | pip-log.txt 38 | pip-delete-this-directory.txt 39 | 40 | # Unit test / coverage reports 41 | htmlcov/ 42 | .tox/ 43 | .nox/ 44 | .coverage 45 | .coverage.* 46 | .cache 47 | nosetests.xml 48 | coverage.xml 49 | *.cover 50 | *.py,cover 51 | .hypothesis/ 52 | .pytest_cache/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | target/ 76 | 77 | # Jupyter Notebook 78 | .ipynb_checkpoints 79 | 80 | # IPython 81 | profile_default/ 82 | ipython_config.py 83 | 84 | # pyenv 85 | .python-version 86 | 87 | # pipenv 88 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 89 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 90 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 91 | # install all needed dependencies. 92 | #Pipfile.lock 93 | 94 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 95 | __pypackages__/ 96 | 97 | # Celery stuff 98 | celerybeat-schedule 99 | celerybeat.pid 100 | 101 | # SageMath parsed files 102 | *.sage.py 103 | 104 | # Environments 105 | .env 106 | .venv 107 | env/ 108 | venv/ 109 | ENV/ 110 | env.bak/ 111 | venv.bak/ 112 | 113 | # Spyder project settings 114 | .spyderproject 115 | .spyproject 116 | 117 | # Rope project settings 118 | .ropeproject 119 | 120 | # mkdocs documentation 121 | /site 122 | 123 | # mypy 124 | .mypy_cache/ 125 | .dmypy.json 126 | dmypy.json 127 | 128 | # Pyre type checker 129 | .pyre/ 130 | .idea/ -------------------------------------------------------------------------------- /train-ticket-workload/query_scaffold/README.md: -------------------------------------------------------------------------------- 1 | # train-ticket-auto-query 2 | 3 | Train Ticket Auto Query Python Scripts 4 | 5 | ## How to use 6 | 7 | ```python 8 | import logging 9 | from queries import Query 10 | from scenarios import query_and_preserve 11 | 12 | # login train-ticket and store the cookies 13 | q = Query(url) 14 | if not q.login(): 15 | logging.fatal('login failed') 16 | 17 | # execute scenario on current user 18 | query_and_preserve(q) 19 | 20 | # or execute query directly 21 | q.query_high_speed_ticket(type="quickest") 22 | ``` 23 | -------------------------------------------------------------------------------- /train-ticket-workload/query_scaffold/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OpsPAI/TraceZip/8b9fc5aa3364cce489f9baac6a1b750f21b24c8e/train-ticket-workload/query_scaffold/__init__.py -------------------------------------------------------------------------------- /train-ticket-workload/query_scaffold/query_admin_basic_config.py: -------------------------------------------------------------------------------- 1 | import logging 2 | import time 3 | 4 | from atomic_queries import _query_admin_basic_config 5 | 6 | logger = logging.getLogger("query_and_preserve") 7 | # The UUID of user fdse_microservice is that 8 | uuid = "4d2a46c7-71cb-4cf1-b5bb-b68406d9da6f" 9 | date = time.strftime("%Y-%m-%d", time.localtime()) 10 | 11 | base_address = "http://139.196.152.44:31000" 12 | 13 | 14 | def query_admin_basic_config(headers): 15 | _query_admin_basic_config(headers=headers) 16 | 17 | 18 | if __name__ == '__main__': 19 | cookie = "JSESSIONID=823B2652E3F5B64A1C94C924A05D80AF; YsbCaptcha=2E037F4AB09D49FA9EE3BE4E737EAFD2" 20 | Authorization = "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJmZHNlX21pY3Jvc2VydmljZSIsInJvbGVzIjpbIlJPTEVfVVNFUiJdLCJpZCI6IjRkMmE0NmM3LTcxY2ItNGNmMS1iNWJiLWI2ODQwNmQ5ZGE2ZiIsImlhdCI6MTYyODcwNzMxMCwiZXhwIjoxNjI4NzEwOTEwfQ.4TakdJtnTwHBViZtyI3EH7tZYdd--fJrRKHVoKQzQLY" 21 | headers = { 22 | 'Connection': 'close', 23 | "Cookie": f"{cookie}", 24 | "Authorization": f"Bearer {Authorization}", 25 | "Content-Type": "application/json" 26 | } 27 | 28 | start_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) 29 | 30 | for i in range(330): 31 | try: 32 | query_admin_basic_config(headers=headers) 33 | print("*****************************INDEX:" + str(i)) 34 | except Exception as e: 35 | print(e) 36 | 37 | end_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) 38 | 39 | print(f"start:{start_time} end:{end_time}") 40 | -------------------------------------------------------------------------------- /train-ticket-workload/query_scaffold/query_admin_basic_price.py: -------------------------------------------------------------------------------- 1 | import logging 2 | import time 3 | 4 | from atomic_queries import _query_admin_basic_price 5 | 6 | logger = logging.getLogger("query_and_preserve") 7 | # The UUID of user fdse_microservice is that 8 | uuid = "4d2a46c7-71cb-4cf1-b5bb-b68406d9da6f" 9 | date = time.strftime("%Y-%m-%d", time.localtime()) 10 | 11 | base_address = "http://139.196.152.44:31000" 12 | 13 | 14 | def query_admin_basic_price(headers): 15 | _query_admin_basic_price(headers=headers) 16 | 17 | 18 | if __name__ == '__main__': 19 | cookie = "JSESSIONID=823B2652E3F5B64A1C94C924A05D80AF; YsbCaptcha=2E037F4AB09D49FA9EE3BE4E737EAFD2" 20 | Authorization = "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJmZHNlX21pY3Jvc2VydmljZSIsInJvbGVzIjpbIlJPTEVfVVNFUiJdLCJpZCI6IjRkMmE0NmM3LTcxY2ItNGNmMS1iNWJiLWI2ODQwNmQ5ZGE2ZiIsImlhdCI6MTYyODcwNzMxMCwiZXhwIjoxNjI4NzEwOTEwfQ.4TakdJtnTwHBViZtyI3EH7tZYdd--fJrRKHVoKQzQLY" 21 | headers = { 22 | 'Connection': 'close', 23 | "Cookie": f"{cookie}", 24 | "Authorization": f"Bearer {Authorization}", 25 | "Content-Type": "application/json" 26 | } 27 | 28 | start_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) 29 | 30 | for i in range(330): 31 | try: 32 | query_admin_basic_price(headers=headers) 33 | print("*****************************INDEX:" + str(i)) 34 | except Exception as e: 35 | print(e) 36 | end_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) 37 | 38 | print(f"start:{start_time} end:{end_time}") 39 | -------------------------------------------------------------------------------- /train-ticket-workload/query_scaffold/query_advanced_ticket.py: -------------------------------------------------------------------------------- 1 | from atomic_queries import _query_advanced_ticket, _login 2 | 3 | import logging 4 | import random 5 | import time 6 | 7 | logger = logging.getLogger("query_advanced_ticket") 8 | # The UUID of user fdse_microservice is that 9 | uuid = "4d2a46c7-71cb-4cf1-b5bb-b68406d9da6f" 10 | date = time.strftime("%Y-%m-%d", time.localtime()) 11 | 12 | base_address = "http://10.176.122.1:32677" 13 | 14 | 15 | 16 | if __name__ == '__main__': 17 | _, token = _login() 18 | headers = { 19 | "Cookie": "JSESSIONID=823B2652E3F5B64A1C94C924A05D80AF; YsbCaptcha=2E037F4AB09D49FA9EE3BE4E737EAFD2", 20 | "Authorization": "Bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJmZHNlX21pY3Jvc2VydmljZSIsInJvbGVzIjpbIlJPTEVfVVNFUiJdLCJpZCI6IjRkMmE0NmM3LTcxY2ItNGNmMS1iNWJiLWI2ODQwNmQ5ZGE2ZiIsImlhdCI6MTYyNzE5OTA0NCwiZXhwIjoxNjI3MjAyNjQ0fQ.3IIwwz7AwqHtOFDeXfih25i6_7nQBPL_K7BFxuyFiKQ", 21 | "Content-Type": "application/json" 22 | } 23 | headers["Authorization"] = "Bearer " + token 24 | 25 | start_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) 26 | 27 | place_pairs = [("Shang Hai", "Su Zhou"), 28 | ("Su Zhou", "Shang Hai"), 29 | ("Nan Jing", "Shang Hai")] 30 | type = "quickest" 31 | for i in range(200): 32 | place_pair = random.choice(place_pairs) 33 | print(f"search {type} between {place_pair[0]} to {place_pair[1]}") 34 | try: 35 | trip_ids = _query_advanced_ticket(place_pair=place_pair, headers=headers, time=date, type=type) 36 | print(f"get {len(trip_ids)} routes.") 37 | print("*****************************INDEX:" + str(i)) 38 | except Exception as e: 39 | print(e) 40 | 41 | end_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) 42 | 43 | print(f"start:{start_time} end:{end_time}") -------------------------------------------------------------------------------- /train-ticket-workload/query_scaffold/query_and_cancel.py: -------------------------------------------------------------------------------- 1 | from atomic_queries import _query_orders, _cancel_one_order 2 | from utils import random_form_list 3 | 4 | 5 | def query_one_and_cancel(headers, uuid="4d2a46c7-71cb-4cf1-b5bb-b68406d9da6f"): 6 | """ 7 | 查询order并取消order 8 | :param uuid: 9 | :param headers: 10 | :return: 11 | """ 12 | pairs = _query_orders(headers=headers, types=tuple([0, 1])) 13 | pairs2 = _query_orders(headers=headers, types=tuple([0, 1]), query_other=True) 14 | 15 | if not pairs and not pairs2: 16 | return 17 | 18 | pairs = pairs + pairs2 19 | 20 | # (orderId, tripId) pair 21 | pair = random_form_list(pairs) 22 | 23 | order_id =_cancel_one_order(order_id=pair[0], uuid=uuid, headers=headers) 24 | if not order_id: 25 | return 26 | 27 | print(f"{order_id} queried and canceled") 28 | 29 | 30 | if __name__ == '__main__': 31 | headers = { 32 | "Cookie": "JSESSIONID=DBE6EC845809D4BFEA66D76BA600995F; YsbCaptcha=63EEEE0E2D564384A7C0052999F3AEA6", 33 | "Authorization": "Bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJmZHNlX21pY3Jvc2VydmljZSIsInJvbGVzIjpbIlJPTEVfVVNFUiJdLCJpZCI6IjRkMmE0NmM3LTcxY2ItNGNmMS1iNWJiLWI2ODQwNmQ5ZGE2ZiIsImlhdCI6MTYyNjM0OTcxOSwiZXhwIjoxNjI2MzUzMzE5fQ.nUTB1SI_gikEm8z8M6EQeyPuQx5zKevo40Y2rqf1EN4", 34 | "Content-Type": "application/json" 35 | } 36 | uuid = "4d2a46c7-71cb-4cf1-b5bb-b68406d9da6f" 37 | 38 | query_one_and_cancel(headers=headers, 39 | uuid=uuid,) 40 | -------------------------------------------------------------------------------- /train-ticket-workload/query_scaffold/query_and_collect_ticket.py: -------------------------------------------------------------------------------- 1 | from atomic_queries import _query_orders, _collect_one_order 2 | from utils import random_form_list 3 | 4 | 5 | def query_and_collect_ticket(headers): 6 | 7 | pairs = _query_orders(headers=headers, types=tuple([1])) 8 | pairs2 = _query_orders(headers=headers, types=tuple([1]), query_other=True) 9 | 10 | if not pairs and not pairs2: 11 | return 12 | 13 | pairs = pairs + pairs2 14 | 15 | # (orderId, tripId) 16 | pair = random_form_list(pairs) 17 | 18 | order_id = _collect_one_order(order_id=pair[0], headers=headers) 19 | if not order_id: 20 | return 21 | 22 | print(f"{order_id} queried and collected") 23 | 24 | 25 | if __name__ == '__main__': 26 | headers = { 27 | "Cookie": "JSESSIONID=DBE6EC845809D4BFEA66D76BA600995F; YsbCaptcha=63EEEE0E2D564384A7C0052999F3AEA6", 28 | "Authorization": "Bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJmZHNlX21pY3Jvc2VydmljZSIsInJvbGVzIjpbIlJPTEVfVVNFUiJdLCJpZCI6IjRkMmE0NmM3LTcxY2ItNGNmMS1iNWJiLWI2ODQwNmQ5ZGE2ZiIsImlhdCI6MTYyNjM0OTcxOSwiZXhwIjoxNjI2MzUzMzE5fQ.nUTB1SI_gikEm8z8M6EQeyPuQx5zKevo40Y2rqf1EN4", 29 | "Content-Type": "application/json" 30 | } 31 | query_and_collect_ticket(headers=headers) 32 | -------------------------------------------------------------------------------- /train-ticket-workload/query_scaffold/query_and_enter_station.py: -------------------------------------------------------------------------------- 1 | from atomic_queries import _query_orders, _enter_station 2 | from utils import random_form_list 3 | 4 | 5 | def query_and_enter_station(headers): 6 | pairs = _query_orders(headers=headers, types=tuple([2])) 7 | pairs2 = _query_orders(headers=headers, types=tuple([2]), query_other=True) 8 | 9 | if not pairs and not pairs2: 10 | return 11 | 12 | pairs = pairs + pairs2 13 | 14 | # (orderId, tripId) 15 | pair = random_form_list(pairs) 16 | 17 | order_id = _enter_station(order_id=pair[0], headers=headers) 18 | if not order_id: 19 | return 20 | 21 | print(f"{order_id} queried and entered station") 22 | 23 | 24 | if __name__ == '__main__': 25 | headers = { 26 | "Cookie": "JSESSIONID=DBE6EC845809D4BFEA66D76BA600995F; YsbCaptcha=63EEEE0E2D564384A7C0052999F3AEA6", 27 | "Authorization": "Bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJmZHNlX21pY3Jvc2VydmljZSIsInJvbGVzIjpbIlJPTEVfVVNFUiJdLCJpZCI6IjRkMmE0NmM3LTcxY2ItNGNmMS1iNWJiLWI2ODQwNmQ5ZGE2ZiIsImlhdCI6MTYyNjM0OTcxOSwiZXhwIjoxNjI2MzUzMzE5fQ.nUTB1SI_gikEm8z8M6EQeyPuQx5zKevo40Y2rqf1EN4", 28 | "Content-Type": "application/json" 29 | } 30 | query_and_enter_station(headers=headers) 31 | -------------------------------------------------------------------------------- /train-ticket-workload/query_scaffold/query_and_put_consign.py: -------------------------------------------------------------------------------- 1 | import time 2 | 3 | from atomic_queries import _query_orders_all_info, _put_consign 4 | from utils import random_form_list 5 | 6 | 7 | def query_one_and_put_consign(headers, pairs): 8 | """ 9 | 查询order并put consign 10 | :param uuid: 11 | :param headers: 12 | :return: 13 | """ 14 | 15 | pair = random_form_list(pairs) 16 | 17 | order_id = _put_consign(result=pair, headers=headers) 18 | if not order_id: 19 | return 20 | 21 | print(f"{order_id} queried and put consign") 22 | 23 | 24 | if __name__ == '__main__': 25 | cookie = "JSESSIONID=823B2652E3F5B64A1C94C924A05D80AF; YsbCaptcha=2E037F4AB09D49FA9EE3BE4E737EAFD2" 26 | Authorization = "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJmZHNlX21pY3Jvc2VydmljZSIsInJvbGVzIjpbIlJPTEVfVVNFUiJdLCJpZCI6IjRkMmE0NmM3LTcxY2ItNGNmMS1iNWJiLWI2ODQwNmQ5ZGE2ZiIsImlhdCI6MTYyODcwNTc0MiwiZXhwIjoxNjI4NzA5MzQyfQ.VHlvCNvaDW41rO55XNV1nniKotW6ip1TFfHaDqyDO3s" 27 | headers = { 28 | 'Connection': 'close', 29 | "Cookie": f"{cookie}", 30 | "Authorization": f"Bearer {Authorization}", 31 | "Content-Type": "application/json" 32 | } 33 | uuid = "4d2a46c7-71cb-4cf1-b5bb-b68406d9da6f" 34 | 35 | start_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) 36 | 37 | pairs = _query_orders_all_info(headers=headers) 38 | pairs2 = _query_orders_all_info(headers=headers, query_other=True) 39 | 40 | pairs = pairs + pairs2 41 | 42 | for i in range(330): 43 | try: 44 | query_one_and_put_consign(headers=headers, pairs=pairs) 45 | print("*****************************INDEX:" + str(i)) 46 | except Exception as e: 47 | print(e) 48 | 49 | end_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) 50 | 51 | print(f"start:{start_time} end:{end_time}") 52 | -------------------------------------------------------------------------------- /train-ticket-workload/query_scaffold/query_and_rebook.py: -------------------------------------------------------------------------------- 1 | from atomic_queries import _query_orders, _collect_one_order, _login, _rebook_ticket 2 | from utils import random_form_list 3 | import time 4 | 5 | base_address = "http://10.176.122.1:32677" 6 | 7 | 8 | def query_and_rebook(headers): 9 | 10 | pairs = _query_orders(headers=headers, types=tuple([1])) 11 | #pairs2 = _query_orders(headers=headers, types=tuple([1]), query_other=True) 12 | 13 | # if not pairs and not pairs2: 14 | # return 15 | 16 | # pairs = pairs + pairs2 17 | 18 | # (orderId, tripId) 19 | # pair = random_form_list(pairs) 20 | new_trip_id = "D1345" 21 | new_date = time.strftime("%Y-%m-%d", time.localtime()) 22 | new_seat_type = "3" 23 | 24 | for pair in pairs: 25 | #print(pair) 26 | _rebook_ticket(old_order_id=pair[0], old_trip_id=pair[1], new_trip_id=new_trip_id, new_date=new_date, new_seat_type=new_seat_type, headers=headers) 27 | 28 | 29 | 30 | 31 | 32 | if __name__ == '__main__': 33 | _, token = _login() 34 | 35 | headers = { 36 | "Cookie": "JSESSIONID=DBE6EC845809D4BFEA66D76BA600995F; YsbCaptcha=63EEEE0E2D564384A7C0052999F3AEA6", 37 | "Authorization": "Bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJmZHNlX21pY3Jvc2VydmljZSIsInJvbGVzIjpbIlJPTEVfVVNFUiJdLCJpZCI6IjRkMmE0NmM3LTcxY2ItNGNmMS1iNWJiLWI2ODQwNmQ5ZGE2ZiIsImlhdCI6MTcyMjAxMjkyNCwiZXhwIjoxNzIyMDE2NTI0fQ._uHvbMld79NLO8nZRfXrKIfOYQ3-5-nn9bh0fOx-vaY", 38 | "Content-Type": "application/json" 39 | } 40 | headers["Authorization"] = "Bearer " + token 41 | 42 | start_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) 43 | 44 | query_and_rebook(headers=headers) 45 | 46 | end_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) 47 | 48 | print(f"start:{start_time} end:{end_time}") -------------------------------------------------------------------------------- /train-ticket-workload/query_scaffold/query_food.py: -------------------------------------------------------------------------------- 1 | from atomic_queries import _query_high_speed_ticket, _query_normal_ticket, _query_assurances, _query_food, \ 2 | _query_contacts 3 | from utils import random_boolean, random_phone, random_str, random_form_list 4 | 5 | import logging 6 | import random 7 | import requests 8 | import time 9 | 10 | logger = logging.getLogger("query_and_preserve") 11 | # The UUID of user fdse_microservice is that 12 | uuid = "4d2a46c7-71cb-4cf1-b5bb-b68406d9da6f" 13 | date = time.strftime("%Y-%m-%d", time.localtime()) 14 | 15 | base_address = "http://139.196.152.44:31000" 16 | 17 | 18 | def query_food(headers): 19 | _query_food(headers=headers) 20 | 21 | 22 | if __name__ == '__main__': 23 | cookie = "JSESSIONID=823B2652E3F5B64A1C94C924A05D80AF; YsbCaptcha=2E037F4AB09D49FA9EE3BE4E737EAFD2" 24 | Authorization = "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJmZHNlX21pY3Jvc2VydmljZSIsInJvbGVzIjpbIlJPTEVfVVNFUiJdLCJpZCI6IjRkMmE0NmM3LTcxY2ItNGNmMS1iNWJiLWI2ODQwNmQ5ZGE2ZiIsImlhdCI6MTYyODcwNTc0MiwiZXhwIjoxNjI4NzA5MzQyfQ.VHlvCNvaDW41rO55XNV1nniKotW6ip1TFfHaDqyDO3s" 25 | headers = { 26 | 'Connection': 'close', 27 | "Cookie": f"{cookie}", 28 | "Authorization": f"Bearer {Authorization}", 29 | "Content-Type": "application/json" 30 | } 31 | 32 | start_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) 33 | 34 | for i in range(320): 35 | try: 36 | query_food(headers=headers) 37 | print("*****************************INDEX:" + str(i)) 38 | except Exception as e: 39 | print(e) 40 | 41 | end_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) 42 | 43 | print(f"start:{start_time} end:{end_time}") 44 | -------------------------------------------------------------------------------- /train-ticket-workload/query_scaffold/query_order_and_pay.py: -------------------------------------------------------------------------------- 1 | import time 2 | 3 | from atomic_queries import _query_orders, _pay_one_order 4 | from utils import random_form_list 5 | 6 | 7 | def query_order_and_pay(headers, pairs): 8 | """ 9 | 查询Order并付款未付款Order 10 | :return: 11 | """ 12 | 13 | # (orderId, tripId) pair 14 | pair = random_form_list(pairs) 15 | 16 | order_id = _pay_one_order(pair[0], pair[1], headers=headers) 17 | if not order_id: 18 | return 19 | 20 | print(f"{order_id} queried and paid") 21 | 22 | 23 | if __name__ == '__main__': 24 | cookie = "JSESSIONID=823B2652E3F5B64A1C94C924A05D80AF; YsbCaptcha=2E037F4AB09D49FA9EE3BE4E737EAFD2" 25 | Authorization = "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJhZG1pbiIsInJvbGVzIjpbIlJPTEVfQURNSU4iXSwiaWQiOiI4NDExZmQxYS1hODg3LTRjYTYtODkxOC0zOGU4ODQwZWYyNGEiLCJpYXQiOjE2MjkzNzIyMjAsImV4cCI6MTYyOTM3NTgyMH0.3f26iWVJTemfMwIPgg5OSgD706JL3ELG2Y9UtRsh0Hs" 26 | headers = { 27 | 'Connection': 'close', 28 | "Cookie": f"{cookie}", 29 | "Authorization": f"Bearer {Authorization}", 30 | "Content-Type": "application/json" 31 | } 32 | 33 | start_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) 34 | 35 | pairs = _query_orders(headers=headers, types=tuple([0, 1])) 36 | pairs2 = _query_orders(headers=headers, types=tuple([0, 1]), query_other=True) 37 | 38 | pairs = pairs + pairs2 39 | 40 | for i in range(330): 41 | try: 42 | query_order_and_pay(headers=headers, pairs=pairs) 43 | print("*****************************INDEX:" + str(i)) 44 | except Exception as e: 45 | print(e) 46 | 47 | end_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) 48 | 49 | print(f"start:{start_time} end:{end_time}") 50 | -------------------------------------------------------------------------------- /train-ticket-workload/query_scaffold/query_route.py: -------------------------------------------------------------------------------- 1 | from atomic_queries import _query_route 2 | 3 | if __name__ == '__main__': 4 | 5 | headers = { 6 | "Cookie": "JSESSIONID=CAF07ABCB2031807D1C6043730C69F17; YsbCaptcha=ABF26F4AE563405894B1540057F62E7B", 7 | "Authorization": "Bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJmZHNlX21pY3Jvc2VydmljZSIsInJvbGVzIjpbIlJPTEVfVVNFUiJdLCJpZCI6IjRkMmE0NmM3LTcxY2ItNGNmMS1iNWJiLWI2ODQwNmQ5ZGE2ZiIsImlhdCI6MTYyNjM0NDgyNSwiZXhwIjoxNjI2MzQ4NDI1fQ.4eOMmQDhnq-Hjj1DuiH8duT6rXkP0QfeTnaXwvYGKD4", 8 | "Content-Type": "application/json" 9 | } 10 | 11 | _query_route(headers=headers) 12 | -------------------------------------------------------------------------------- /train-ticket-workload/query_scaffold/query_travel_left.py: -------------------------------------------------------------------------------- 1 | from atomic_queries import _query_high_speed_ticket, _query_normal_ticket 2 | from utils import random_boolean 3 | 4 | import logging 5 | import time 6 | 7 | logger = logging.getLogger("query_and_preserve") 8 | # The UUID of user fdse_microservice is that 9 | uuid = "4d2a46c7-71cb-4cf1-b5bb-b68406d9da6f" 10 | date = time.strftime("%Y-%m-%d", time.localtime()) 11 | 12 | base_address = "http://139.196.152.44:31000" 13 | 14 | 15 | def query_travel_left(headers): 16 | """ 17 | 1. 查票(随机高铁或普通) 18 | 2. 查保险、Food、Contacts 19 | 3. 随机选择Contacts、保险、是否买食物、是否托运 20 | 4. 买票 21 | :return: 22 | """ 23 | start = "" 24 | end = "" 25 | trip_ids = [] 26 | PRESERVE_URL = "" 27 | 28 | high_speed = False 29 | if high_speed: 30 | start = "Shang Hai" 31 | end = "Su Zhou" 32 | high_speed_place_pair = (start, end) 33 | trip_ids = _query_high_speed_ticket(place_pair=high_speed_place_pair, headers=headers, time=date) 34 | else: 35 | start = "Shang Hai" 36 | end = "Nan Jing" 37 | other_place_pair = (start, end) 38 | trip_ids = _query_normal_ticket(place_pair=other_place_pair, headers=headers, time=date) 39 | 40 | 41 | if __name__ == '__main__': 42 | cookie = "JSESSIONID=823B2652E3F5B64A1C94C924A05D80AF; YsbCaptcha=2E037F4AB09D49FA9EE3BE4E737EAFD2" 43 | Authorization = "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJmZHNlX21pY3Jvc2VydmljZSIsInJvbGVzIjpbIlJPTEVfVVNFUiJdLCJpZCI6IjRkMmE0NmM3LTcxY2ItNGNmMS1iNWJiLWI2ODQwNmQ5ZGE2ZiIsImlhdCI6MTYyODcwNjQ5NSwiZXhwIjoxNjI4NzEwMDk1fQ.1G93WmGzGG11uc9dY4AAuJxiGlmjl12UXSAngNwPSXk" 44 | headers = { 45 | 'Connection': 'close', 46 | "Cookie": f"{cookie}", 47 | "Authorization": f"Bearer {Authorization}", 48 | "Content-Type": "application/json" 49 | } 50 | 51 | start_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) 52 | print(f"start:{start_time}") 53 | 54 | for i in range(30): 55 | try: 56 | query_travel_left(headers=headers) 57 | print("*****************************INDEX:" + str(i)) 58 | except Exception as e: 59 | print(e) 60 | end_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) 61 | 62 | print(f"start:{start_time} end:{end_time}") 63 | -------------------------------------------------------------------------------- /train-ticket-workload/query_scaffold/query_travel_left_parallel.py: -------------------------------------------------------------------------------- 1 | from atomic_queries import _query_high_speed_ticket, _query_normal_ticket, _query_high_speed_ticket_parallel 2 | from utils import random_boolean 3 | 4 | import logging 5 | import time 6 | 7 | logger = logging.getLogger("query_and_preserve") 8 | # The UUID of user fdse_microservice is that 9 | uuid = "4d2a46c7-71cb-4cf1-b5bb-b68406d9da6f" 10 | date = time.strftime("%Y-%m-%d", time.localtime()) 11 | 12 | base_address = "http://139.196.152.44:31000" 13 | 14 | 15 | def query_travel_left_parallel(headers): 16 | """ 17 | 1. 查票(随机高铁或普通) 18 | 2. 查保险、Food、Contacts 19 | 3. 随机选择Contacts、保险、是否买食物、是否托运 20 | 4. 买票 21 | :return: 22 | """ 23 | start = "" 24 | end = "" 25 | trip_ids = [] 26 | PRESERVE_URL = "" 27 | 28 | start = "Su Zhou" 29 | end = "Shang Hai" 30 | high_speed_place_pair = (start, end) 31 | trip_ids = _query_high_speed_ticket_parallel(place_pair=high_speed_place_pair, headers=headers, time=date) 32 | 33 | 34 | if __name__ == '__main__': 35 | cookie = "JSESSIONID=823B2652E3F5B64A1C94C924A05D80AF; YsbCaptcha=2E037F4AB09D49FA9EE3BE4E737EAFD2" 36 | Authorization = "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJmZHNlX21pY3Jvc2VydmljZSIsInJvbGVzIjpbIlJPTEVfVVNFUiJdLCJpZCI6IjRkMmE0NmM3LTcxY2ItNGNmMS1iNWJiLWI2ODQwNmQ5ZGE2ZiIsImlhdCI6MTYyOTM2ODk1NiwiZXhwIjoxNjI5MzcyNTU2fQ.K206pVlC2JgeATGYjQksPJt3DUNsfbVH4pgx9b54zwg" 37 | headers = { 38 | 'Connection': 'close', 39 | "Cookie": f"{cookie}", 40 | "Authorization": f"Bearer {Authorization}", 41 | "Content-Type": "application/json" 42 | } 43 | 44 | start_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) 45 | print(f"start:{start_time}") 46 | 47 | for i in range(320): 48 | try: 49 | query_travel_left_parallel(headers=headers) 50 | print("*****************************INDEX:" + str(i)) 51 | except Exception as e: 52 | print(e) 53 | end_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) 54 | 55 | print(f"start:{start_time} end:{end_time}") 56 | -------------------------------------------------------------------------------- /train-ticket-workload/query_scaffold/utils.py: -------------------------------------------------------------------------------- 1 | import random 2 | from typing import List 3 | import string 4 | 5 | 6 | def random_boolean() -> bool: 7 | return random.choice([True, False]) 8 | 9 | 10 | def random_from_list(l: List): 11 | return random.choice(l) 12 | 13 | 14 | def random_from_weighted(d: dict): 15 | """ 16 | :param d: 带相对权重的字典,eg. {'a': 100, 'b': 50} 17 | :return: 返回随机选择的key 18 | """ 19 | total = sum(d.values()) # 权重求和 20 | ra = random.uniform(0, total) # 在0与权重和之前获取一个随机数 21 | curr_sum = 0 22 | ret = None 23 | 24 | keys = d.keys() 25 | for k in keys: 26 | curr_sum += d[k] # 在遍历中,累加当前权重值 27 | if ra <= curr_sum: # 当随机数<=当前权重和时,返回权重key 28 | ret = k 29 | break 30 | 31 | return ret 32 | 33 | 34 | def random_str(): 35 | ''.join(random.choices(string.ascii_letters, k=random.randint(4, 10))) 36 | 37 | 38 | def random_phone(): 39 | ''.join(random.choices(string.digits, k=random.randint(8, 15))) 40 | -------------------------------------------------------------------------------- /train-ticket-workload/readme.md: -------------------------------------------------------------------------------- 1 | before running, please install `requests` 2 | 3 | run by `python3 test.py` 4 | 5 | you should disabled verification (always return true no matter what code is) of verification-code micro-service in train-ticket benchmark 6 | 7 | we only write `main` function in `test.py`, the rest of codes comes from train-ticket-auto-query, which is delivered as a part of train-ticket benchmark. We delivered our scripts to show how we generate workload 8 | --------------------------------------------------------------------------------