cpcloud commented on a change in pull request #10260:
URL: https://github.com/apache/arrow/pull/10260#discussion_r736886675



##########
File path: cpp/src/arrow/util/tracing_internal.cc
##########
@@ -0,0 +1,252 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include "arrow/util/tracing_internal.h"
+
+#include <iostream>
+#include <sstream>
+#include <thread>
+
+#ifdef _MSC_VER
+#pragma warning(push)
+#pragma warning(disable : 4522)
+#endif
+#ifdef ARROW_WITH_OPENTELEMETRY
+#include <opentelemetry/sdk/trace/batch_span_processor.h>
+#include <opentelemetry/sdk/trace/recordable.h>
+#include <opentelemetry/sdk/trace/span_data.h>
+#include <opentelemetry/sdk/trace/tracer_provider.h>
+#include <opentelemetry/trace/noop.h>
+#include <opentelemetry/trace/provider.h>
+#endif
+#ifdef _MSC_VER
+#pragma warning(pop)
+#endif
+
+#include "arrow/util/config.h"
+#include "arrow/util/io_util.h"
+#include "arrow/util/logging.h"
+#include "arrow/util/make_unique.h"
+#ifdef ARROW_JSON
+#include "arrow/json/rapidjson_defs.h"
+#include "rapidjson/ostreamwrapper.h"
+#include "rapidjson/writer.h"
+#endif
+
+namespace arrow {
+namespace internal {
+namespace tracing {
+
+namespace nostd = opentelemetry::nostd;
+namespace otel = opentelemetry;
+
+constexpr char kTracingBackendEnvVar[] = "ARROW_TRACING_BACKEND";
+
+namespace {
+
+#ifdef ARROW_WITH_OPENTELEMETRY
+namespace sdktrace = opentelemetry::sdk::trace;
+#ifdef ARROW_JSON
+struct OwnedAttributeValueVisitor {
+  OwnedAttributeValueVisitor(
+      arrow::rapidjson::Writer<arrow::rapidjson::OStreamWrapper>& writer_)
+      : writer(writer_) {}
+
+  void operator()(const std::string& arg) { writer.String(arg); }
+
+  void operator()(const int32_t& arg) { writer.Int(arg); }
+
+  void operator()(const uint32_t& arg) { writer.Uint(arg); }
+
+  void operator()(const int64_t& arg) { writer.Int64(arg); }
+
+  void operator()(const uint64_t& arg) { writer.Uint64(arg); }
+
+  template <typename T>
+  void operator()(T&& arg) {
+    writer.Null();
+  }
+
+  arrow::rapidjson::Writer<arrow::rapidjson::OStreamWrapper>& writer;
+};
+
+/// Export spans as newline-delimited JSON.
+class OStreamJsonSpanExporter : public sdktrace::SpanExporter {
+ public:
+  explicit OStreamJsonSpanExporter(std::ostream& sout = std::cerr) noexcept
+      : sout_(sout), shutdown_(false) {}
+  std::unique_ptr<sdktrace::Recordable> MakeRecordable() noexcept override {
+    return std::unique_ptr<sdktrace::Recordable>(new sdktrace::SpanData);
+  }
+  otel::sdk::common::ExportResult Export(
+      const nostd::span<std::unique_ptr<sdktrace::Recordable>>& spans) 
noexcept override {
+    if (shutdown_) return otel::sdk::common::ExportResult::kFailure;
+
+    for (auto& recordable : spans) {
+      arrow::rapidjson::Writer<arrow::rapidjson::OStreamWrapper> writer(sout_);
+      auto span = std::unique_ptr<sdktrace::SpanData>(
+          static_cast<sdktrace::SpanData*>(recordable.release()));
+      if (!span) continue;
+      char trace_id[32] = {0};
+      char span_id[16] = {0};
+      char parent_span_id[16] = {0};
+      span->GetTraceId().ToLowerBase16(trace_id);
+      span->GetSpanId().ToLowerBase16(span_id);
+      span->GetParentSpanId().ToLowerBase16(parent_span_id);
+
+      writer.StartObject();
+      writer.Key("name");
+      writer.String(span->GetName().data(), span->GetName().length());
+      writer.Key("trace_id");
+      writer.String(trace_id, 32);
+      writer.Key("span_id");
+      writer.String(span_id, 16);
+      writer.Key("parent_span_id");
+      writer.String(parent_span_id, 16);
+      writer.Key("start");
+      writer.Int64(span->GetStartTime().time_since_epoch().count());
+      writer.Key("duration");
+      writer.Int64(span->GetDuration().count());
+      writer.Key("description");
+      writer.String(span->GetDescription().data(), 
span->GetDescription().length());
+      writer.Key("kind");
+      writer.Int(static_cast<int>(span->GetSpanKind()));
+      writer.Key("status");
+      // TODO: this is expensive
+      writer.String(statuses_[static_cast<int>(span->GetStatus())]);
+      writer.Key("args");
+      writer.StartObject();
+      OwnedAttributeValueVisitor visitor(writer);
+      for (const auto& pair : span->GetAttributes()) {
+        writer.Key(pair.first.data(), pair.first.length());
+        nostd::visit(visitor, pair.second);
+      }
+      writer.EndObject();
+      writer.EndObject();
+      sout_.Put('\n');
+    }
+    sout_.Flush();
+    return otel::sdk::common::ExportResult::kSuccess;
+  }
+  bool Shutdown(std::chrono::microseconds) noexcept override {
+    shutdown_ = true;
+    return true;
+  }
+
+ private:
+  arrow::rapidjson::OStreamWrapper sout_;
+  bool shutdown_;
+  std::map<int, std::string> statuses_{{0, "Unset"}, {1, "Ok"}, {2, "Error"}};
+};
+#endif
+
+class ThreadIdSpanProcessor : public sdktrace::BatchSpanProcessor {
+ public:
+  using sdktrace::BatchSpanProcessor::BatchSpanProcessor;
+  void OnEnd(std::unique_ptr<sdktrace::Recordable>&& span) noexcept override {
+    std::stringstream thread_id;
+    thread_id << std::this_thread::get_id();
+    span->SetAttribute("thread_id", thread_id.str());
+    sdktrace::BatchSpanProcessor::OnEnd(std::move(span));
+  }
+};
+
+std::unique_ptr<sdktrace::SpanExporter> InitializeExporter() {
+  auto maybe_env_var = arrow::internal::GetEnvVar(kTracingBackendEnvVar);
+  if (maybe_env_var.ok()) {
+    auto env_var = maybe_env_var.ValueOrDie();
+    if (env_var == "json") {
+#ifdef ARROW_JSON
+      return std::unique_ptr<sdktrace::SpanExporter>(
+          new OStreamJsonSpanExporter(std::cerr));
+#else
+      ARROW_LOG(WARNING) << "Requested " << kTracingBackendEnvVar
+                         << "=json but Arrow was built without ARROW_JSON";
+#endif
+    } else if (!env_var.empty()) {
+      ARROW_LOG(WARNING) << "Requested unknown backend " << 
kTracingBackendEnvVar << "="
+                         << env_var;
+    }
+  }
+  return std::unique_ptr<sdktrace::SpanExporter>();
+}
+
+nostd::shared_ptr<sdktrace::TracerProvider> InitializeSdkTracerProvider() {
+  auto exporter = InitializeExporter();
+  if (exporter) {
+    sdktrace::BatchSpanProcessorOptions options;
+    options.max_queue_size = 16384;
+    options.schedule_delay_millis = std::chrono::milliseconds(500);
+    options.max_export_batch_size = 16384;
+    auto processor = std::unique_ptr<sdktrace::SpanProcessor>(

Review comment:
       I guess this can't be `make_unique`?

##########
File path: cpp/src/arrow/util/tracing_internal.cc
##########
@@ -0,0 +1,252 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include "arrow/util/tracing_internal.h"
+
+#include <iostream>
+#include <sstream>
+#include <thread>
+
+#ifdef _MSC_VER
+#pragma warning(push)
+#pragma warning(disable : 4522)
+#endif
+#ifdef ARROW_WITH_OPENTELEMETRY
+#include <opentelemetry/sdk/trace/batch_span_processor.h>
+#include <opentelemetry/sdk/trace/recordable.h>
+#include <opentelemetry/sdk/trace/span_data.h>
+#include <opentelemetry/sdk/trace/tracer_provider.h>
+#include <opentelemetry/trace/noop.h>
+#include <opentelemetry/trace/provider.h>
+#endif
+#ifdef _MSC_VER
+#pragma warning(pop)
+#endif
+
+#include "arrow/util/config.h"
+#include "arrow/util/io_util.h"
+#include "arrow/util/logging.h"
+#include "arrow/util/make_unique.h"
+#ifdef ARROW_JSON
+#include "arrow/json/rapidjson_defs.h"
+#include "rapidjson/ostreamwrapper.h"
+#include "rapidjson/writer.h"
+#endif
+
+namespace arrow {
+namespace internal {
+namespace tracing {
+
+namespace nostd = opentelemetry::nostd;
+namespace otel = opentelemetry;
+
+constexpr char kTracingBackendEnvVar[] = "ARROW_TRACING_BACKEND";
+
+namespace {
+
+#ifdef ARROW_WITH_OPENTELEMETRY
+namespace sdktrace = opentelemetry::sdk::trace;
+#ifdef ARROW_JSON
+struct OwnedAttributeValueVisitor {
+  OwnedAttributeValueVisitor(
+      arrow::rapidjson::Writer<arrow::rapidjson::OStreamWrapper>& writer_)
+      : writer(writer_) {}
+
+  void operator()(const std::string& arg) { writer.String(arg); }
+
+  void operator()(const int32_t& arg) { writer.Int(arg); }
+
+  void operator()(const uint32_t& arg) { writer.Uint(arg); }
+
+  void operator()(const int64_t& arg) { writer.Int64(arg); }
+
+  void operator()(const uint64_t& arg) { writer.Uint64(arg); }
+
+  template <typename T>
+  void operator()(T&& arg) {
+    writer.Null();
+  }
+
+  arrow::rapidjson::Writer<arrow::rapidjson::OStreamWrapper>& writer;
+};
+
+/// Export spans as newline-delimited JSON.
+class OStreamJsonSpanExporter : public sdktrace::SpanExporter {

Review comment:
       Even if you don't go with the OTLP exporter (I assume the dependencies 
are annoying), you can use the 
`opentelemetry::exporter::trace::OStreamSpanExporter` which doesn't output 
JSON, but if you're not doing anything structured, it should suffice. The 
messages look like this:
   
   ```
   {
     name          : library
     trace_id      : 2a93c0ffd8d36fce84c28b3d08f845c9
     span_id       : 109ea4dea0acdab5
     tracestate    :
     parent_span_id: 0000000000000000
     start         : 1635282336724427669
     duration      : 75913
     description   :
     span kind     : Internal
     status        : Unset
     attributes    :
     events        :
     links         :
     resources     :
           service.name: unknown_service
           telemetry.sdk.version: 1.0.1
           telemetry.sdk.name: opentelemetry
           telemetry.sdk.language: cpp
     instr-lib     : foo_library
   }
   ```
   
    For any structure, the collector can be used.

##########
File path: cpp/src/arrow/util/tracing_internal.cc
##########
@@ -0,0 +1,252 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include "arrow/util/tracing_internal.h"
+
+#include <iostream>
+#include <sstream>
+#include <thread>
+
+#ifdef _MSC_VER
+#pragma warning(push)
+#pragma warning(disable : 4522)
+#endif
+#ifdef ARROW_WITH_OPENTELEMETRY
+#include <opentelemetry/sdk/trace/batch_span_processor.h>
+#include <opentelemetry/sdk/trace/recordable.h>
+#include <opentelemetry/sdk/trace/span_data.h>
+#include <opentelemetry/sdk/trace/tracer_provider.h>
+#include <opentelemetry/trace/noop.h>
+#include <opentelemetry/trace/provider.h>
+#endif
+#ifdef _MSC_VER
+#pragma warning(pop)
+#endif
+
+#include "arrow/util/config.h"
+#include "arrow/util/io_util.h"
+#include "arrow/util/logging.h"
+#include "arrow/util/make_unique.h"
+#ifdef ARROW_JSON
+#include "arrow/json/rapidjson_defs.h"
+#include "rapidjson/ostreamwrapper.h"
+#include "rapidjson/writer.h"
+#endif
+
+namespace arrow {
+namespace internal {
+namespace tracing {
+
+namespace nostd = opentelemetry::nostd;
+namespace otel = opentelemetry;
+
+constexpr char kTracingBackendEnvVar[] = "ARROW_TRACING_BACKEND";
+
+namespace {
+
+#ifdef ARROW_WITH_OPENTELEMETRY
+namespace sdktrace = opentelemetry::sdk::trace;
+#ifdef ARROW_JSON
+struct OwnedAttributeValueVisitor {
+  OwnedAttributeValueVisitor(
+      arrow::rapidjson::Writer<arrow::rapidjson::OStreamWrapper>& writer_)
+      : writer(writer_) {}
+
+  void operator()(const std::string& arg) { writer.String(arg); }
+
+  void operator()(const int32_t& arg) { writer.Int(arg); }
+
+  void operator()(const uint32_t& arg) { writer.Uint(arg); }
+
+  void operator()(const int64_t& arg) { writer.Int64(arg); }
+
+  void operator()(const uint64_t& arg) { writer.Uint64(arg); }
+
+  template <typename T>
+  void operator()(T&& arg) {
+    writer.Null();
+  }
+
+  arrow::rapidjson::Writer<arrow::rapidjson::OStreamWrapper>& writer;
+};
+
+/// Export spans as newline-delimited JSON.
+class OStreamJsonSpanExporter : public sdktrace::SpanExporter {
+ public:
+  explicit OStreamJsonSpanExporter(std::ostream& sout = std::cerr) noexcept
+      : sout_(sout), shutdown_(false) {}
+  std::unique_ptr<sdktrace::Recordable> MakeRecordable() noexcept override {
+    return std::unique_ptr<sdktrace::Recordable>(new sdktrace::SpanData);
+  }
+  otel::sdk::common::ExportResult Export(
+      const nostd::span<std::unique_ptr<sdktrace::Recordable>>& spans) 
noexcept override {
+    if (shutdown_) return otel::sdk::common::ExportResult::kFailure;
+
+    for (auto& recordable : spans) {
+      arrow::rapidjson::Writer<arrow::rapidjson::OStreamWrapper> writer(sout_);
+      auto span = std::unique_ptr<sdktrace::SpanData>(
+          static_cast<sdktrace::SpanData*>(recordable.release()));
+      if (!span) continue;
+      char trace_id[32] = {0};
+      char span_id[16] = {0};
+      char parent_span_id[16] = {0};
+      span->GetTraceId().ToLowerBase16(trace_id);
+      span->GetSpanId().ToLowerBase16(span_id);
+      span->GetParentSpanId().ToLowerBase16(parent_span_id);
+
+      writer.StartObject();
+      writer.Key("name");
+      writer.String(span->GetName().data(), span->GetName().length());
+      writer.Key("trace_id");
+      writer.String(trace_id, 32);
+      writer.Key("span_id");
+      writer.String(span_id, 16);
+      writer.Key("parent_span_id");
+      writer.String(parent_span_id, 16);
+      writer.Key("start");
+      writer.Int64(span->GetStartTime().time_since_epoch().count());
+      writer.Key("duration");
+      writer.Int64(span->GetDuration().count());
+      writer.Key("description");
+      writer.String(span->GetDescription().data(), 
span->GetDescription().length());
+      writer.Key("kind");
+      writer.Int(static_cast<int>(span->GetSpanKind()));
+      writer.Key("status");
+      // TODO: this is expensive
+      writer.String(statuses_[static_cast<int>(span->GetStatus())]);
+      writer.Key("args");
+      writer.StartObject();
+      OwnedAttributeValueVisitor visitor(writer);
+      for (const auto& pair : span->GetAttributes()) {
+        writer.Key(pair.first.data(), pair.first.length());
+        nostd::visit(visitor, pair.second);
+      }
+      writer.EndObject();
+      writer.EndObject();
+      sout_.Put('\n');
+    }
+    sout_.Flush();
+    return otel::sdk::common::ExportResult::kSuccess;
+  }
+  bool Shutdown(std::chrono::microseconds) noexcept override {
+    shutdown_ = true;
+    return true;
+  }
+
+ private:
+  arrow::rapidjson::OStreamWrapper sout_;
+  bool shutdown_;
+  std::map<int, std::string> statuses_{{0, "Unset"}, {1, "Ok"}, {2, "Error"}};
+};
+#endif
+
+class ThreadIdSpanProcessor : public sdktrace::BatchSpanProcessor {
+ public:
+  using sdktrace::BatchSpanProcessor::BatchSpanProcessor;
+  void OnEnd(std::unique_ptr<sdktrace::Recordable>&& span) noexcept override {
+    std::stringstream thread_id;
+    thread_id << std::this_thread::get_id();
+    span->SetAttribute("thread_id", thread_id.str());
+    sdktrace::BatchSpanProcessor::OnEnd(std::move(span));
+  }
+};
+
+std::unique_ptr<sdktrace::SpanExporter> InitializeExporter() {
+  auto maybe_env_var = arrow::internal::GetEnvVar(kTracingBackendEnvVar);
+  if (maybe_env_var.ok()) {
+    auto env_var = maybe_env_var.ValueOrDie();
+    if (env_var == "json") {
+#ifdef ARROW_JSON
+      return std::unique_ptr<sdktrace::SpanExporter>(
+          new OStreamJsonSpanExporter(std::cerr));
+#else
+      ARROW_LOG(WARNING) << "Requested " << kTracingBackendEnvVar
+                         << "=json but Arrow was built without ARROW_JSON";
+#endif
+    } else if (!env_var.empty()) {
+      ARROW_LOG(WARNING) << "Requested unknown backend " << 
kTracingBackendEnvVar << "="
+                         << env_var;
+    }
+  }
+  return std::unique_ptr<sdktrace::SpanExporter>();
+}
+
+nostd::shared_ptr<sdktrace::TracerProvider> InitializeSdkTracerProvider() {
+  auto exporter = InitializeExporter();
+  if (exporter) {
+    sdktrace::BatchSpanProcessorOptions options;
+    options.max_queue_size = 16384;
+    options.schedule_delay_millis = std::chrono::milliseconds(500);
+    options.max_export_batch_size = 16384;
+    auto processor = std::unique_ptr<sdktrace::SpanProcessor>(
+        new ThreadIdSpanProcessor(std::move(exporter), options));
+    return nostd::shared_ptr<sdktrace::TracerProvider>(

Review comment:
       Similarly, guessing this can't be `make_shared`?

##########
File path: cpp/src/arrow/util/tracing_internal.cc
##########
@@ -0,0 +1,252 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include "arrow/util/tracing_internal.h"
+
+#include <iostream>
+#include <sstream>
+#include <thread>
+
+#ifdef _MSC_VER
+#pragma warning(push)
+#pragma warning(disable : 4522)
+#endif
+#ifdef ARROW_WITH_OPENTELEMETRY
+#include <opentelemetry/sdk/trace/batch_span_processor.h>
+#include <opentelemetry/sdk/trace/recordable.h>
+#include <opentelemetry/sdk/trace/span_data.h>
+#include <opentelemetry/sdk/trace/tracer_provider.h>
+#include <opentelemetry/trace/noop.h>
+#include <opentelemetry/trace/provider.h>
+#endif
+#ifdef _MSC_VER
+#pragma warning(pop)
+#endif
+
+#include "arrow/util/config.h"
+#include "arrow/util/io_util.h"
+#include "arrow/util/logging.h"
+#include "arrow/util/make_unique.h"
+#ifdef ARROW_JSON
+#include "arrow/json/rapidjson_defs.h"
+#include "rapidjson/ostreamwrapper.h"
+#include "rapidjson/writer.h"
+#endif
+
+namespace arrow {
+namespace internal {
+namespace tracing {
+
+namespace nostd = opentelemetry::nostd;
+namespace otel = opentelemetry;
+
+constexpr char kTracingBackendEnvVar[] = "ARROW_TRACING_BACKEND";
+
+namespace {
+
+#ifdef ARROW_WITH_OPENTELEMETRY
+namespace sdktrace = opentelemetry::sdk::trace;
+#ifdef ARROW_JSON
+struct OwnedAttributeValueVisitor {
+  OwnedAttributeValueVisitor(
+      arrow::rapidjson::Writer<arrow::rapidjson::OStreamWrapper>& writer_)
+      : writer(writer_) {}
+
+  void operator()(const std::string& arg) { writer.String(arg); }
+
+  void operator()(const int32_t& arg) { writer.Int(arg); }
+
+  void operator()(const uint32_t& arg) { writer.Uint(arg); }
+
+  void operator()(const int64_t& arg) { writer.Int64(arg); }
+
+  void operator()(const uint64_t& arg) { writer.Uint64(arg); }
+
+  template <typename T>
+  void operator()(T&& arg) {
+    writer.Null();
+  }
+
+  arrow::rapidjson::Writer<arrow::rapidjson::OStreamWrapper>& writer;
+};
+
+/// Export spans as newline-delimited JSON.
+class OStreamJsonSpanExporter : public sdktrace::SpanExporter {

Review comment:
       I'm able to run these examples 
(https://github.com/open-telemetry/opentelemetry-cpp/tree/main/examples/otlp) 
with a tiny collector setup producing JSON to stdout. I don't think this 
exporter is necessary.
   
   Additionally, I think we'd lose a lot of the benefits of opentelemetry by 
outputting JSON that isn't compatible with any otel consumer. If you run the 
examples you'll see that the JSON differs what's being written here in a number 
of meaningful ways.
   
   I think it's enough to add the initialization (sans this exporter) and the 
async generator wrapper.
   
   To reproduce the examples:
   
   1. Build the library from source (the examples should build by default)
   2. change `examples/otlp/opentelemetry-collector-config/config.dev.yaml` to 
this:
   ```yaml
   exporters:
     file:
       path: /dev/stdout
   receivers:
     otlp:
       protocols:
         grpc:
           endpoint: 0.0.0.0:4317
         http:
           endpoint: "0.0.0.0:4318"
           cors_allowed_origins:
           - '*'
   service:
     pipelines:
       traces:
         receivers:
         - otlp
         exporters:
         - file
   ```
   3. Start the collector container from the `opentelemetry-cpp` clone:
   ```
   docker run --rm -it -p 4317:4317 -p 4318:4318 -v $(pwd)/examples/otlp:/cfg 
otel/opentelemetry-collector:latest 
--config=/cfg/opentelemetry-collector-config/config.dev.yaml
   ```
   
   If you now run `build/examples/otlp/example_otlp_grpc` or 
`build/examples/otlp/example_otlp_http` you should see a line of JSON coming 
out from the place you launch the container for each invocation.
   
   The example can be simplified further by removing one of the `grpc` or 
`http` receivers.

##########
File path: cpp/src/arrow/util/tracing_internal.cc
##########
@@ -0,0 +1,252 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include "arrow/util/tracing_internal.h"
+
+#include <iostream>
+#include <sstream>
+#include <thread>
+
+#ifdef _MSC_VER
+#pragma warning(push)
+#pragma warning(disable : 4522)
+#endif
+#ifdef ARROW_WITH_OPENTELEMETRY
+#include <opentelemetry/sdk/trace/batch_span_processor.h>
+#include <opentelemetry/sdk/trace/recordable.h>
+#include <opentelemetry/sdk/trace/span_data.h>
+#include <opentelemetry/sdk/trace/tracer_provider.h>
+#include <opentelemetry/trace/noop.h>
+#include <opentelemetry/trace/provider.h>
+#endif
+#ifdef _MSC_VER
+#pragma warning(pop)
+#endif
+
+#include "arrow/util/config.h"
+#include "arrow/util/io_util.h"
+#include "arrow/util/logging.h"
+#include "arrow/util/make_unique.h"
+#ifdef ARROW_JSON
+#include "arrow/json/rapidjson_defs.h"
+#include "rapidjson/ostreamwrapper.h"
+#include "rapidjson/writer.h"
+#endif
+
+namespace arrow {
+namespace internal {
+namespace tracing {
+
+namespace nostd = opentelemetry::nostd;
+namespace otel = opentelemetry;
+
+constexpr char kTracingBackendEnvVar[] = "ARROW_TRACING_BACKEND";
+
+namespace {
+
+#ifdef ARROW_WITH_OPENTELEMETRY
+namespace sdktrace = opentelemetry::sdk::trace;
+#ifdef ARROW_JSON
+struct OwnedAttributeValueVisitor {
+  OwnedAttributeValueVisitor(
+      arrow::rapidjson::Writer<arrow::rapidjson::OStreamWrapper>& writer_)
+      : writer(writer_) {}
+
+  void operator()(const std::string& arg) { writer.String(arg); }
+
+  void operator()(const int32_t& arg) { writer.Int(arg); }
+
+  void operator()(const uint32_t& arg) { writer.Uint(arg); }
+
+  void operator()(const int64_t& arg) { writer.Int64(arg); }
+
+  void operator()(const uint64_t& arg) { writer.Uint64(arg); }
+
+  template <typename T>
+  void operator()(T&& arg) {
+    writer.Null();
+  }
+
+  arrow::rapidjson::Writer<arrow::rapidjson::OStreamWrapper>& writer;
+};
+
+/// Export spans as newline-delimited JSON.
+class OStreamJsonSpanExporter : public sdktrace::SpanExporter {
+ public:
+  explicit OStreamJsonSpanExporter(std::ostream& sout = std::cerr) noexcept
+      : sout_(sout), shutdown_(false) {}
+  std::unique_ptr<sdktrace::Recordable> MakeRecordable() noexcept override {
+    return std::unique_ptr<sdktrace::Recordable>(new sdktrace::SpanData);
+  }
+  otel::sdk::common::ExportResult Export(
+      const nostd::span<std::unique_ptr<sdktrace::Recordable>>& spans) 
noexcept override {
+    if (shutdown_) return otel::sdk::common::ExportResult::kFailure;
+
+    for (auto& recordable : spans) {
+      arrow::rapidjson::Writer<arrow::rapidjson::OStreamWrapper> writer(sout_);
+      auto span = std::unique_ptr<sdktrace::SpanData>(
+          static_cast<sdktrace::SpanData*>(recordable.release()));
+      if (!span) continue;
+      char trace_id[32] = {0};
+      char span_id[16] = {0};
+      char parent_span_id[16] = {0};
+      span->GetTraceId().ToLowerBase16(trace_id);
+      span->GetSpanId().ToLowerBase16(span_id);
+      span->GetParentSpanId().ToLowerBase16(parent_span_id);
+
+      writer.StartObject();
+      writer.Key("name");
+      writer.String(span->GetName().data(), span->GetName().length());
+      writer.Key("trace_id");
+      writer.String(trace_id, 32);
+      writer.Key("span_id");
+      writer.String(span_id, 16);
+      writer.Key("parent_span_id");
+      writer.String(parent_span_id, 16);
+      writer.Key("start");
+      writer.Int64(span->GetStartTime().time_since_epoch().count());
+      writer.Key("duration");
+      writer.Int64(span->GetDuration().count());
+      writer.Key("description");
+      writer.String(span->GetDescription().data(), 
span->GetDescription().length());
+      writer.Key("kind");
+      writer.Int(static_cast<int>(span->GetSpanKind()));
+      writer.Key("status");
+      // TODO: this is expensive
+      writer.String(statuses_[static_cast<int>(span->GetStatus())]);
+      writer.Key("args");
+      writer.StartObject();
+      OwnedAttributeValueVisitor visitor(writer);
+      for (const auto& pair : span->GetAttributes()) {
+        writer.Key(pair.first.data(), pair.first.length());
+        nostd::visit(visitor, pair.second);
+      }
+      writer.EndObject();
+      writer.EndObject();
+      sout_.Put('\n');
+    }
+    sout_.Flush();
+    return otel::sdk::common::ExportResult::kSuccess;
+  }
+  bool Shutdown(std::chrono::microseconds) noexcept override {
+    shutdown_ = true;
+    return true;
+  }
+
+ private:
+  arrow::rapidjson::OStreamWrapper sout_;
+  bool shutdown_;
+  std::map<int, std::string> statuses_{{0, "Unset"}, {1, "Ok"}, {2, "Error"}};
+};
+#endif
+
+class ThreadIdSpanProcessor : public sdktrace::BatchSpanProcessor {

Review comment:
       Is this the thread of the current span, or the thread of the thing doing 
the processing (these could in theory be separate threads)

##########
File path: cpp/src/arrow/util/tracing_internal.cc
##########
@@ -0,0 +1,252 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include "arrow/util/tracing_internal.h"
+
+#include <iostream>
+#include <sstream>
+#include <thread>
+
+#ifdef _MSC_VER
+#pragma warning(push)
+#pragma warning(disable : 4522)
+#endif
+#ifdef ARROW_WITH_OPENTELEMETRY
+#include <opentelemetry/sdk/trace/batch_span_processor.h>
+#include <opentelemetry/sdk/trace/recordable.h>
+#include <opentelemetry/sdk/trace/span_data.h>
+#include <opentelemetry/sdk/trace/tracer_provider.h>
+#include <opentelemetry/trace/noop.h>
+#include <opentelemetry/trace/provider.h>
+#endif
+#ifdef _MSC_VER
+#pragma warning(pop)
+#endif
+
+#include "arrow/util/config.h"
+#include "arrow/util/io_util.h"
+#include "arrow/util/logging.h"
+#include "arrow/util/make_unique.h"
+#ifdef ARROW_JSON
+#include "arrow/json/rapidjson_defs.h"
+#include "rapidjson/ostreamwrapper.h"
+#include "rapidjson/writer.h"
+#endif
+
+namespace arrow {
+namespace internal {
+namespace tracing {
+
+namespace nostd = opentelemetry::nostd;
+namespace otel = opentelemetry;
+
+constexpr char kTracingBackendEnvVar[] = "ARROW_TRACING_BACKEND";
+
+namespace {
+
+#ifdef ARROW_WITH_OPENTELEMETRY
+namespace sdktrace = opentelemetry::sdk::trace;
+#ifdef ARROW_JSON
+struct OwnedAttributeValueVisitor {
+  OwnedAttributeValueVisitor(
+      arrow::rapidjson::Writer<arrow::rapidjson::OStreamWrapper>& writer_)
+      : writer(writer_) {}
+
+  void operator()(const std::string& arg) { writer.String(arg); }
+
+  void operator()(const int32_t& arg) { writer.Int(arg); }
+
+  void operator()(const uint32_t& arg) { writer.Uint(arg); }
+
+  void operator()(const int64_t& arg) { writer.Int64(arg); }
+
+  void operator()(const uint64_t& arg) { writer.Uint64(arg); }
+
+  template <typename T>
+  void operator()(T&& arg) {
+    writer.Null();
+  }
+
+  arrow::rapidjson::Writer<arrow::rapidjson::OStreamWrapper>& writer;
+};
+
+/// Export spans as newline-delimited JSON.
+class OStreamJsonSpanExporter : public sdktrace::SpanExporter {

Review comment:
       Is this actually necessary? Typically a collector handles turning the 
otel data into `$MY_FAVORITE_FORMAT`.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to