From 734c6277be2a0bb902141700da90c8c3cdc41a81 Mon Sep 17 00:00:00 2001 From: Nadia Ciobanu Date: Fri, 19 Jun 2020 00:25:51 +0000 Subject: [PATCH 01/41] Add simple example to exporter dir for experimentation --- exporters/otlp/BUILD | 37 +++++++--------- exporters/otlp/BUILD_old | 30 +++++++++++++ exporters/otlp/foo_library/foo_library.cc | 33 ++++++++++++++ exporters/otlp/foo_library/foo_library.h | 3 ++ exporters/otlp/main.cc | 29 +++++++++++++ exporters/otlp/otlp_exporter.h | 0 exporters/otlp/stdout_exporter.h | 52 +++++++++++++++++++++++ 7 files changed, 163 insertions(+), 21 deletions(-) create mode 100644 exporters/otlp/BUILD_old create mode 100644 exporters/otlp/foo_library/foo_library.cc create mode 100644 exporters/otlp/foo_library/foo_library.h create mode 100644 exporters/otlp/main.cc create mode 100644 exporters/otlp/otlp_exporter.h create mode 100644 exporters/otlp/stdout_exporter.h diff --git a/exporters/otlp/BUILD b/exporters/otlp/BUILD index 09b1516ab5..34583e2eac 100644 --- a/exporters/otlp/BUILD +++ b/exporters/otlp/BUILD @@ -1,30 +1,25 @@ -# Copyright 2020, OpenTelemetry Authors -# -# Licensed 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. - -package(default_visibility = ["//visibility:public"]) - cc_library( - name = "recordable", + name = "foo_library", srcs = [ - "recordable.cc", + "foo_library/foo_library.cc", ], hdrs = [ - "recordable.h", + "foo_library/foo_library.h", + ], + deps = [ + "//api", + ], +) + +cc_binary( + name = "example_simple", + srcs = [ + "main.cc", + "stdout_exporter.h", ], - include_prefix = "exporters/otlp", deps = [ + ":foo_library", + "//api", "//sdk/src/trace", - "@com_github_opentelemetry_proto//:trace_proto_cc", ], ) diff --git a/exporters/otlp/BUILD_old b/exporters/otlp/BUILD_old new file mode 100644 index 0000000000..09b1516ab5 --- /dev/null +++ b/exporters/otlp/BUILD_old @@ -0,0 +1,30 @@ +# Copyright 2020, OpenTelemetry Authors +# +# Licensed 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. + +package(default_visibility = ["//visibility:public"]) + +cc_library( + name = "recordable", + srcs = [ + "recordable.cc", + ], + hdrs = [ + "recordable.h", + ], + include_prefix = "exporters/otlp", + deps = [ + "//sdk/src/trace", + "@com_github_opentelemetry_proto//:trace_proto_cc", + ], +) diff --git a/exporters/otlp/foo_library/foo_library.cc b/exporters/otlp/foo_library/foo_library.cc new file mode 100644 index 0000000000..0990b85ced --- /dev/null +++ b/exporters/otlp/foo_library/foo_library.cc @@ -0,0 +1,33 @@ +#include "opentelemetry/trace/provider.h" + +namespace trace = opentelemetry::trace; +namespace nostd = opentelemetry::nostd; + +namespace +{ +nostd::shared_ptr get_tracer() +{ + auto provider = trace::Provider::GetTracerProvider(); + return provider->GetTracer("foo_library"); +} + +void f1() +{ + auto span = get_tracer()->StartSpan("f1"); +} + +void f2() +{ + auto span = get_tracer()->StartSpan("f2"); + + f1(); + f1(); +} +} // namespace + +void foo_library() +{ + auto span = get_tracer()->StartSpan("library"); + + f2(); +} diff --git a/exporters/otlp/foo_library/foo_library.h b/exporters/otlp/foo_library/foo_library.h new file mode 100644 index 0000000000..7ac75c0e50 --- /dev/null +++ b/exporters/otlp/foo_library/foo_library.h @@ -0,0 +1,3 @@ +#pragma once + +void foo_library(); diff --git a/exporters/otlp/main.cc b/exporters/otlp/main.cc new file mode 100644 index 0000000000..af03d2e6db --- /dev/null +++ b/exporters/otlp/main.cc @@ -0,0 +1,29 @@ +#include "opentelemetry/sdk/trace/simple_processor.h" +#include "opentelemetry/sdk/trace/tracer_provider.h" +#include "opentelemetry/trace/provider.h" + +// Using an exporter that simply dumps span data to stdout. +#include "stdout_exporter.h" + +#include "foo_library/foo_library.h" + +namespace +{ +void initTracer() +{ + auto exporter = std::unique_ptr(new StdoutExporter); + auto processor = std::shared_ptr( + new sdktrace::SimpleSpanProcessor(std::move(exporter))); + auto provider = nostd::shared_ptr(new sdktrace::TracerProvider(processor)); + trace::Provider::SetTracerProvider(provider); +} +} // namespace + +int main() +{ + // Removing this line will leave OT initialized with the default noop + // tracer, thus being effectively deactivated. + initTracer(); + + foo_library(); +} diff --git a/exporters/otlp/otlp_exporter.h b/exporters/otlp/otlp_exporter.h new file mode 100644 index 0000000000..e69de29bb2 diff --git a/exporters/otlp/stdout_exporter.h b/exporters/otlp/stdout_exporter.h new file mode 100644 index 0000000000..6a58c5b8d6 --- /dev/null +++ b/exporters/otlp/stdout_exporter.h @@ -0,0 +1,52 @@ +#pragma once + +#include "opentelemetry/sdk/trace/exporter.h" +#include "opentelemetry/sdk/trace/span_data.h" + +#include + +namespace trace = opentelemetry::trace; +namespace nostd = opentelemetry::nostd; +namespace sdktrace = opentelemetry::sdk::trace; + +class StdoutExporter final : public sdktrace::SpanExporter +{ + std::unique_ptr MakeRecordable() noexcept + { + return std::unique_ptr(new sdktrace::SpanData); + } + + sdktrace::ExportResult Export( + const nostd::span> &spans) noexcept + { + for (auto &recordable : spans) + { + auto span = std::unique_ptr( + static_cast(recordable.release())); + + if (span != nullptr) + { + 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); + + std::cout << "{" + << "\n name : " << span->GetName() + << "\n trace_id : " << std::string(trace_id, 32) + << "\n span_id : " << std::string(span_id, 16) + << "\n parent_span_id: " << std::string(parent_span_id, 16) + << "\n start : " << span->GetStartTime().time_since_epoch().count() + << "\n duration : " << span->GetDuration().count() << "\n}" + << "\n"; + } + } + + return sdktrace::ExportResult::kSuccess; + } + + void Shutdown(std::chrono::microseconds timeout = std::chrono::microseconds(0)) noexcept {} +}; From 23dc12ed2cb5dc963f551caa97c169578ee58187 Mon Sep 17 00:00:00 2001 From: Nadia Ciobanu Date: Fri, 19 Jun 2020 18:41:55 +0000 Subject: [PATCH 02/41] Add trace service protobuf dependency --- WORKSPACE | 40 ++++++++++++++++++++++ bazel/opentelemetry_proto.BUILD | 26 ++++++++++++++ exporters/otlp/BUILD | 39 ++++++++++++++++++++- exporters/otlp/BUILD_old | 37 +++++++++----------- exporters/otlp/main.cc | 6 ++-- exporters/otlp/otlp_exporter.h | 60 +++++++++++++++++++++++++++++++++ 6 files changed, 184 insertions(+), 24 deletions(-) diff --git a/WORKSPACE b/WORKSPACE index 1dd2ea37fc..4e32a9daa3 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -37,6 +37,46 @@ new_local_repository( path = "third_party/opentelemetry-proto", ) + +##### gRPC Rules for Bazel +##### See https://github.com/grpc/grpc/blob/master/src/cpp/README.md#make + +# http_archive( +# name = "com_github_grpc_grpc", +# strip_prefix = "grpc-master", +# urls = ["https://github.com/grpc/grpc/archive/master.tar.gz"], +# ) + +# load("@com_github_grpc_grpc//bazel:grpc_deps.bzl", "grpc_deps") + +# grpc_deps() + +# # grpc_deps() cannot load() its deps, this WORKSPACE has to do it. +# # See also: https://github.com/bazelbuild/bazel/issues/1943 +# load( +# "@build_bazel_rules_apple//apple:repositories.bzl", +# "apple_rules_dependencies", +# ) + +# apple_rules_dependencies() + +# load( +# "@build_bazel_apple_support//lib:repositories.bzl", +# "apple_support_dependencies", +# ) + +# apple_support_dependencies() + +# load("@upb//bazel:repository_defs.bzl", "bazel_version_repository") + +# bazel_version_repository(name = "upb_bazel_version") + + +# Not mentioned in official docs... mentioned here https://github.com/grpc/grpc/issues/20511 +#load("@com_github_grpc_grpc//bazel:grpc_extra_deps.bzl", "grpc_extra_deps") +#grpc_extra_deps() + + # GoogleTest framework. # Only needed for tests, not to build the OpenTelemetry library. http_archive( diff --git a/bazel/opentelemetry_proto.BUILD b/bazel/opentelemetry_proto.BUILD index ddb9915bf2..90661c39c9 100644 --- a/bazel/opentelemetry_proto.BUILD +++ b/bazel/opentelemetry_proto.BUILD @@ -16,6 +16,8 @@ package(default_visibility = ["//visibility:public"]) load("@rules_proto//proto:defs.bzl", "proto_library") +#load("@com_github_grpc_grpc//bazel:cc_grpc_library.bzl", "cc_grpc_library") + proto_library( name = "common_proto", srcs = [ @@ -58,3 +60,27 @@ cc_proto_library( name = "trace_proto_cc", deps = [":trace_proto"], ) + + + +proto_library( + name = "trace_service_proto", + srcs = [ + "opentelemetry/proto/collector/trace/v1/trace_service.proto", + ], + deps = [ + ":trace_proto", + ], +) + +cc_proto_library( + name = "trace_service_proto_cc", + deps = [":trace_service_proto"], +) + +# cc_grpc_library( +# name = "trace_service_grpc_cc", +# srcs = [":trace_service_proto"], +# grpc_only = True, +# deps = [":trace_service_proto_cc"], +# ) diff --git a/exporters/otlp/BUILD b/exporters/otlp/BUILD index 34583e2eac..09cc7d4604 100644 --- a/exporters/otlp/BUILD +++ b/exporters/otlp/BUILD @@ -1,3 +1,34 @@ +# Copyright 2020, OpenTelemetry Authors +# +# Licensed 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. + +package(default_visibility = ["//visibility:public"]) + +cc_library( + name = "recordable", + srcs = [ + "recordable.cc", + ], + hdrs = [ + "recordable.h", + ], + include_prefix = "exporters/otlp", + deps = [ + "//sdk/src/trace", + "@com_github_opentelemetry_proto//:trace_proto_cc", + ], +) + cc_library( name = "foo_library", srcs = [ @@ -15,11 +46,17 @@ cc_binary( name = "example_simple", srcs = [ "main.cc", - "stdout_exporter.h", + #"stdout_exporter.h", + 'otlp_exporter.h', ], deps = [ ":foo_library", "//api", "//sdk/src/trace", + "@com_github_opentelemetry_proto//:trace_proto_cc", + "@com_github_opentelemetry_proto//:trace_service_proto_cc", + #"@com_github_opentelemetry_proto//:trace_service_grpc_cc", + #"@com_github_grpc_grpc//:grpc++", ], ) + diff --git a/exporters/otlp/BUILD_old b/exporters/otlp/BUILD_old index 09b1516ab5..34583e2eac 100644 --- a/exporters/otlp/BUILD_old +++ b/exporters/otlp/BUILD_old @@ -1,30 +1,25 @@ -# Copyright 2020, OpenTelemetry Authors -# -# Licensed 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. - -package(default_visibility = ["//visibility:public"]) - cc_library( - name = "recordable", + name = "foo_library", srcs = [ - "recordable.cc", + "foo_library/foo_library.cc", ], hdrs = [ - "recordable.h", + "foo_library/foo_library.h", + ], + deps = [ + "//api", + ], +) + +cc_binary( + name = "example_simple", + srcs = [ + "main.cc", + "stdout_exporter.h", ], - include_prefix = "exporters/otlp", deps = [ + ":foo_library", + "//api", "//sdk/src/trace", - "@com_github_opentelemetry_proto//:trace_proto_cc", ], ) diff --git a/exporters/otlp/main.cc b/exporters/otlp/main.cc index af03d2e6db..0f489d87c2 100644 --- a/exporters/otlp/main.cc +++ b/exporters/otlp/main.cc @@ -3,7 +3,8 @@ #include "opentelemetry/trace/provider.h" // Using an exporter that simply dumps span data to stdout. -#include "stdout_exporter.h" +//#include "stdout_exporter.h" +#include "otlp_exporter.h" #include "foo_library/foo_library.h" @@ -11,7 +12,8 @@ namespace { void initTracer() { - auto exporter = std::unique_ptr(new StdoutExporter); + //auto exporter = std::unique_ptr(new StdoutExporter); + auto exporter = std::unique_ptr(new OtlpExporter); auto processor = std::shared_ptr( new sdktrace::SimpleSpanProcessor(std::move(exporter))); auto provider = nostd::shared_ptr(new sdktrace::TracerProvider(processor)); diff --git a/exporters/otlp/otlp_exporter.h b/exporters/otlp/otlp_exporter.h index e69de29bb2..9798fe35cb 100644 --- a/exporters/otlp/otlp_exporter.h +++ b/exporters/otlp/otlp_exporter.h @@ -0,0 +1,60 @@ +#pragma once + +#include "opentelemetry/sdk/trace/exporter.h" +#include "opentelemetry/sdk/trace/span_data.h" + +#include "opentelemetry/proto/collector/trace/v1/trace_service.pb.h" +//#include "opentelemetry/proto/collector/trace/v1/trace_service.grpc.pb.h" + +#include + +namespace trace = opentelemetry::trace; +namespace nostd = opentelemetry::nostd; +namespace sdktrace = opentelemetry::sdk::trace; + +class OtlpExporter final : public sdktrace::SpanExporter +{ + std::unique_ptr MakeRecordable() noexcept + { + return std::unique_ptr(new sdktrace::SpanData); + } + + sdktrace::ExportResult Export( + const nostd::span> &spans) noexcept + { + for (auto &recordable : spans) + { + auto span = std::unique_ptr( + static_cast(recordable.release())); + + if (span != nullptr) + { + 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); + + std::cout << "{" + << "\n name : " << span->GetName() + << "\n trace_id : " << std::string(trace_id, 32) + << "\n span_id : " << std::string(span_id, 16) + << "\n parent_span_id: " << std::string(parent_span_id, 16) + << "\n start : " << span->GetStartTime().time_since_epoch().count() + << "\n duration : " << span->GetDuration().count() << "\n}" + << "\n"; + } + } + + return sdktrace::ExportResult::kSuccess; + } + + void Shutdown(std::chrono::microseconds timeout = std::chrono::microseconds(0)) noexcept {} + + + //std::unique_ptr< + //opentelemetry::proto::collector::trace::v1::TraceService::StubInterface> + //trace_service_stub; +}; From 609bd1dc292bacd261c0a7ed1e3b7f25cea0d26b Mon Sep 17 00:00:00 2001 From: Nadia Ciobanu Date: Fri, 19 Jun 2020 21:53:49 +0000 Subject: [PATCH 03/41] Try adding skylib, reformatting --- WORKSPACE | 9 ++++++--- bazel/opentelemetry_proto.BUILD | 2 +- exporters/otlp/BUILD | 2 ++ exporters/otlp/BUILD_old | 25 ------------------------- 4 files changed, 9 insertions(+), 29 deletions(-) delete mode 100644 exporters/otlp/BUILD_old diff --git a/WORKSPACE b/WORKSPACE index 4e32a9daa3..4532c2740c 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -71,10 +71,13 @@ new_local_repository( # bazel_version_repository(name = "upb_bazel_version") +# Add skylib? +#load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace") +#bazel_skylib_workspace() -# Not mentioned in official docs... mentioned here https://github.com/grpc/grpc/issues/20511 -#load("@com_github_grpc_grpc//bazel:grpc_extra_deps.bzl", "grpc_extra_deps") -#grpc_extra_deps() +# # Not mentioned in official docs... mentioned here https://github.com/grpc/grpc/issues/20511 +# load("@com_github_grpc_grpc//bazel:grpc_extra_deps.bzl", "grpc_extra_deps") +# grpc_extra_deps() # GoogleTest framework. diff --git a/bazel/opentelemetry_proto.BUILD b/bazel/opentelemetry_proto.BUILD index 90661c39c9..a5ee02977c 100644 --- a/bazel/opentelemetry_proto.BUILD +++ b/bazel/opentelemetry_proto.BUILD @@ -16,7 +16,7 @@ package(default_visibility = ["//visibility:public"]) load("@rules_proto//proto:defs.bzl", "proto_library") -#load("@com_github_grpc_grpc//bazel:cc_grpc_library.bzl", "cc_grpc_library") +# load("@com_github_grpc_grpc//bazel:cc_grpc_library.bzl", "cc_grpc_library") proto_library( name = "common_proto", diff --git a/exporters/otlp/BUILD b/exporters/otlp/BUILD index 09cc7d4604..fbbc245610 100644 --- a/exporters/otlp/BUILD +++ b/exporters/otlp/BUILD @@ -55,6 +55,8 @@ cc_binary( "//sdk/src/trace", "@com_github_opentelemetry_proto//:trace_proto_cc", "@com_github_opentelemetry_proto//:trace_service_proto_cc", + + # For gRPC #"@com_github_opentelemetry_proto//:trace_service_grpc_cc", #"@com_github_grpc_grpc//:grpc++", ], diff --git a/exporters/otlp/BUILD_old b/exporters/otlp/BUILD_old deleted file mode 100644 index 34583e2eac..0000000000 --- a/exporters/otlp/BUILD_old +++ /dev/null @@ -1,25 +0,0 @@ -cc_library( - name = "foo_library", - srcs = [ - "foo_library/foo_library.cc", - ], - hdrs = [ - "foo_library/foo_library.h", - ], - deps = [ - "//api", - ], -) - -cc_binary( - name = "example_simple", - srcs = [ - "main.cc", - "stdout_exporter.h", - ], - deps = [ - ":foo_library", - "//api", - "//sdk/src/trace", - ], -) From 04261325f5e5140f3fdbfd81b237e473fcd25bd5 Mon Sep 17 00:00:00 2001 From: Nadia Ciobanu Date: Mon, 22 Jun 2020 18:25:10 +0000 Subject: [PATCH 04/41] Fix build by adding gRPC load to top of WORKSPACE --- WORKSPACE | 76 ++++++++++++++------------------- bazel/opentelemetry_proto.BUILD | 14 +++--- exporters/otlp/BUILD | 6 +-- exporters/otlp/otlp_exporter.h | 2 +- 4 files changed, 44 insertions(+), 54 deletions(-) diff --git a/WORKSPACE b/WORKSPACE index 4532c2740c..2f48918241 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -16,6 +16,39 @@ workspace(name = "io_opentelemetry_cpp") load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") +# Load gRPC dependency +# Note that this dependency needs to be loaded first due to +# https://github.com/bazelbuild/bazel/issues/6664 +http_archive( + name = "com_github_grpc_grpc", + strip_prefix = "grpc-master", + urls = ["https://github.com/grpc/grpc/archive/master.tar.gz"], +) + +load("@com_github_grpc_grpc//bazel:grpc_deps.bzl", "grpc_deps") + +grpc_deps() + +# grpc_deps() cannot load() its deps, this WORKSPACE has to do it. +# See also: https://github.com/bazelbuild/bazel/issues/1943 +load( + "@build_bazel_rules_apple//apple:repositories.bzl", + "apple_rules_dependencies", +) + +apple_rules_dependencies() + +load( + "@build_bazel_apple_support//lib:repositories.bzl", + "apple_support_dependencies", +) + +apple_support_dependencies() + +load("@upb//bazel:repository_defs.bzl", "bazel_version_repository") + +bazel_version_repository(name = "upb_bazel_version") + # Uses older protobuf version because of # https://github.com/protocolbuffers/protobuf/issues/7179 http_archive( @@ -37,49 +70,6 @@ new_local_repository( path = "third_party/opentelemetry-proto", ) - -##### gRPC Rules for Bazel -##### See https://github.com/grpc/grpc/blob/master/src/cpp/README.md#make - -# http_archive( -# name = "com_github_grpc_grpc", -# strip_prefix = "grpc-master", -# urls = ["https://github.com/grpc/grpc/archive/master.tar.gz"], -# ) - -# load("@com_github_grpc_grpc//bazel:grpc_deps.bzl", "grpc_deps") - -# grpc_deps() - -# # grpc_deps() cannot load() its deps, this WORKSPACE has to do it. -# # See also: https://github.com/bazelbuild/bazel/issues/1943 -# load( -# "@build_bazel_rules_apple//apple:repositories.bzl", -# "apple_rules_dependencies", -# ) - -# apple_rules_dependencies() - -# load( -# "@build_bazel_apple_support//lib:repositories.bzl", -# "apple_support_dependencies", -# ) - -# apple_support_dependencies() - -# load("@upb//bazel:repository_defs.bzl", "bazel_version_repository") - -# bazel_version_repository(name = "upb_bazel_version") - -# Add skylib? -#load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace") -#bazel_skylib_workspace() - -# # Not mentioned in official docs... mentioned here https://github.com/grpc/grpc/issues/20511 -# load("@com_github_grpc_grpc//bazel:grpc_extra_deps.bzl", "grpc_extra_deps") -# grpc_extra_deps() - - # GoogleTest framework. # Only needed for tests, not to build the OpenTelemetry library. http_archive( diff --git a/bazel/opentelemetry_proto.BUILD b/bazel/opentelemetry_proto.BUILD index a5ee02977c..d8e95b4310 100644 --- a/bazel/opentelemetry_proto.BUILD +++ b/bazel/opentelemetry_proto.BUILD @@ -16,7 +16,7 @@ package(default_visibility = ["//visibility:public"]) load("@rules_proto//proto:defs.bzl", "proto_library") -# load("@com_github_grpc_grpc//bazel:cc_grpc_library.bzl", "cc_grpc_library") +load("@com_github_grpc_grpc//bazel:cc_grpc_library.bzl", "cc_grpc_library") proto_library( name = "common_proto", @@ -78,9 +78,9 @@ cc_proto_library( deps = [":trace_service_proto"], ) -# cc_grpc_library( -# name = "trace_service_grpc_cc", -# srcs = [":trace_service_proto"], -# grpc_only = True, -# deps = [":trace_service_proto_cc"], -# ) +cc_grpc_library( + name = "trace_service_grpc_cc", + srcs = [":trace_service_proto"], + grpc_only = True, + deps = [":trace_service_proto_cc"], +) diff --git a/exporters/otlp/BUILD b/exporters/otlp/BUILD index fbbc245610..8a308c47c4 100644 --- a/exporters/otlp/BUILD +++ b/exporters/otlp/BUILD @@ -43,7 +43,7 @@ cc_library( ) cc_binary( - name = "example_simple", + name = "otlp_exporter", srcs = [ "main.cc", #"stdout_exporter.h", @@ -57,8 +57,8 @@ cc_binary( "@com_github_opentelemetry_proto//:trace_service_proto_cc", # For gRPC - #"@com_github_opentelemetry_proto//:trace_service_grpc_cc", - #"@com_github_grpc_grpc//:grpc++", + "@com_github_opentelemetry_proto//:trace_service_grpc_cc", + "@com_github_grpc_grpc//:grpc++", ], ) diff --git a/exporters/otlp/otlp_exporter.h b/exporters/otlp/otlp_exporter.h index 9798fe35cb..412a9a1f2c 100644 --- a/exporters/otlp/otlp_exporter.h +++ b/exporters/otlp/otlp_exporter.h @@ -4,7 +4,7 @@ #include "opentelemetry/sdk/trace/span_data.h" #include "opentelemetry/proto/collector/trace/v1/trace_service.pb.h" -//#include "opentelemetry/proto/collector/trace/v1/trace_service.grpc.pb.h" +#include "opentelemetry/proto/collector/trace/v1/trace_service.grpc.pb.h" #include From 009776e4f62b84735ee596dd9b9668554ab0cc3f Mon Sep 17 00:00:00 2001 From: Nadia Ciobanu Date: Mon, 22 Jun 2020 21:10:52 +0000 Subject: [PATCH 05/41] Add recordable to exporter --- exporters/otlp/BUILD | 1 + exporters/otlp/otlp_exporter.h | 8 +++++--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/exporters/otlp/BUILD b/exporters/otlp/BUILD index 8a308c47c4..958f8e9601 100644 --- a/exporters/otlp/BUILD +++ b/exporters/otlp/BUILD @@ -51,6 +51,7 @@ cc_binary( ], deps = [ ":foo_library", + ":recordable", "//api", "//sdk/src/trace", "@com_github_opentelemetry_proto//:trace_proto_cc", diff --git a/exporters/otlp/otlp_exporter.h b/exporters/otlp/otlp_exporter.h index 412a9a1f2c..276dd2c0aa 100644 --- a/exporters/otlp/otlp_exporter.h +++ b/exporters/otlp/otlp_exporter.h @@ -6,6 +6,8 @@ #include "opentelemetry/proto/collector/trace/v1/trace_service.pb.h" #include "opentelemetry/proto/collector/trace/v1/trace_service.grpc.pb.h" +#include "recordable.h" + #include namespace trace = opentelemetry::trace; @@ -54,7 +56,7 @@ class OtlpExporter final : public sdktrace::SpanExporter void Shutdown(std::chrono::microseconds timeout = std::chrono::microseconds(0)) noexcept {} - //std::unique_ptr< - //opentelemetry::proto::collector::trace::v1::TraceService::StubInterface> - //trace_service_stub; + std::unique_ptr< + opentelemetry::proto::collector::trace::v1::TraceService::StubInterface> + trace_service_stub; }; From 99e3672f846fc76a4f7252629dde6dbba0a364f2 Mon Sep 17 00:00:00 2001 From: Nadia Ciobanu Date: Mon, 22 Jun 2020 22:11:46 +0000 Subject: [PATCH 06/41] Add basic recordable functions --- exporters/otlp/recordable.cc | 5 +++++ exporters/otlp/recordable.h | 9 ++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/exporters/otlp/recordable.cc b/exporters/otlp/recordable.cc index d38f284577..ee44f947d5 100644 --- a/exporters/otlp/recordable.cc +++ b/exporters/otlp/recordable.cc @@ -20,6 +20,11 @@ void Recordable::SetName(nostd::string_view name) noexcept { span_.set_name(name.data(), name.size()); } + +void Recordable::SetStartTime(opentelemetry::core::SystemTimestamp start_time) noexcept +{ + span_.set_start_time_unixnano(start_time.time_since_epoch()); +} } // namespace otlp } // namespace exporter OPENTELEMETRY_END_NAMESPACE diff --git a/exporters/otlp/recordable.h b/exporters/otlp/recordable.h index 5ea1edf497..ace4e05f35 100644 --- a/exporters/otlp/recordable.h +++ b/exporters/otlp/recordable.h @@ -14,13 +14,20 @@ class Recordable final : public sdk::trace::Recordable public: const proto::trace::v1::Span &span() const noexcept { return span_; } - // sdk::trace::Recordable + void SetIds(trace::TraceId trace_id, + trace::SpanId span_id, + trace::SpanId parent_span_id) noexcept override; + void AddEvent(nostd::string_view name, core::SystemTimestamp timestamp) noexcept override; void SetStatus(trace::CanonicalCode code, nostd::string_view description) noexcept override; void SetName(nostd::string_view name) noexcept override; + void SetStartTime(opentelemetry::core::SystemTimestamp start_time) noexcept override; + + void SetDuration(std::chrono::nanoseconds duration) noexcept override; + private: proto::trace::v1::Span span_; }; From e326783fe7d505b65fa0c5c18a3cdddda0b1babc Mon Sep 17 00:00:00 2001 From: Nadia Ciobanu Date: Tue, 23 Jun 2020 01:02:33 +0000 Subject: [PATCH 07/41] Add start time --- exporters/otlp/recordable.cc | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/exporters/otlp/recordable.cc b/exporters/otlp/recordable.cc index ee44f947d5..0094cf6d68 100644 --- a/exporters/otlp/recordable.cc +++ b/exporters/otlp/recordable.cc @@ -5,6 +5,12 @@ namespace exporter { namespace otlp { +// void Recordable::SetIds(trace::TraceId trace_id, +// trace::SpanId span_id, +// trace::SpanId parent_span_id) noexcept { +// span_.set_trace_id(trace_id); +// } + void Recordable::AddEvent(nostd::string_view name, core::SystemTimestamp timestamp) noexcept { (void)name; @@ -23,8 +29,14 @@ void Recordable::SetName(nostd::string_view name) noexcept void Recordable::SetStartTime(opentelemetry::core::SystemTimestamp start_time) noexcept { - span_.set_start_time_unixnano(start_time.time_since_epoch()); + std::chrono::nanoseconds nano_time = start_time.time_since_epoch(); + uint64_t nano_unix_time = std::chrono::duration_cast(nano_time).count(); + span_.set_start_time_unixnano(nano_unix_time); } + +// void Recordable::SetDuration(std::chrono::nanoseconds duration) noexcept { + +// } } // namespace otlp } // namespace exporter OPENTELEMETRY_END_NAMESPACE From bb8b215791773a92d971597b8eff895a82c0ce9f Mon Sep 17 00:00:00 2001 From: Nadia Ciobanu Date: Tue, 23 Jun 2020 19:02:46 +0000 Subject: [PATCH 08/41] Add duration and ids --- exporters/otlp/recordable.cc | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/exporters/otlp/recordable.cc b/exporters/otlp/recordable.cc index 0094cf6d68..cbe9636241 100644 --- a/exporters/otlp/recordable.cc +++ b/exporters/otlp/recordable.cc @@ -5,11 +5,21 @@ namespace exporter { namespace otlp { -// void Recordable::SetIds(trace::TraceId trace_id, -// trace::SpanId span_id, -// trace::SpanId parent_span_id) noexcept { -// span_.set_trace_id(trace_id); -// } +void Recordable::SetIds(trace::TraceId trace_id, + trace::SpanId span_id, + trace::SpanId parent_span_id) noexcept { + char trace_id_buff[32] = {0}; + char span_id_buff[16] = {0}; + char parent_span_id_buff[16] = {0}; + + trace_id.ToLowerBase16(trace_id_buff); + span_id.ToLowerBase16(span_id_buff); + parent_span_id.ToLowerBase16(parent_span_id_buff); + + span_.set_trace_id(trace_id_buff); + span_.set_span_id(span_id_buff); + span_.set_parent_span_id(parent_span_id_buff); +} void Recordable::AddEvent(nostd::string_view name, core::SystemTimestamp timestamp) noexcept { @@ -30,13 +40,16 @@ void Recordable::SetName(nostd::string_view name) noexcept void Recordable::SetStartTime(opentelemetry::core::SystemTimestamp start_time) noexcept { std::chrono::nanoseconds nano_time = start_time.time_since_epoch(); - uint64_t nano_unix_time = std::chrono::duration_cast(nano_time).count(); + uint64_t nano_unix_time = nano_time.count(); span_.set_start_time_unixnano(nano_unix_time); } -// void Recordable::SetDuration(std::chrono::nanoseconds duration) noexcept { +void Recordable::SetDuration(std::chrono::nanoseconds duration) noexcept { + uint64_t unix_duration = duration.count(); -// } + uint64_t unix_end_time = span_.start_time_unixnano() + unix_duration; + span_.set_end_time_unixnano(unix_end_time); +} } // namespace otlp } // namespace exporter OPENTELEMETRY_END_NAMESPACE From 257d03a4867aa9e41794f0552b23e0f7cbac5979 Mon Sep 17 00:00:00 2001 From: Nadia Ciobanu Date: Tue, 23 Jun 2020 22:56:47 +0000 Subject: [PATCH 09/41] Add recordable test --- exporters/otlp/BUILD | 9 ++++++ exporters/otlp/recordable.cc | 4 +-- exporters/otlp/recordable_test.cc | 51 +++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 exporters/otlp/recordable_test.cc diff --git a/exporters/otlp/BUILD b/exporters/otlp/BUILD index 09b1516ab5..fd1f048889 100644 --- a/exporters/otlp/BUILD +++ b/exporters/otlp/BUILD @@ -28,3 +28,12 @@ cc_library( "@com_github_opentelemetry_proto//:trace_proto_cc", ], ) + +cc_test( + name = "recordable_test", + srcs = ["recordable_test.cc"], + deps = [ + ":recordable", + "@com_google_googletest//:gtest_main", + ], +) diff --git a/exporters/otlp/recordable.cc b/exporters/otlp/recordable.cc index cbe9636241..23739791b1 100644 --- a/exporters/otlp/recordable.cc +++ b/exporters/otlp/recordable.cc @@ -44,9 +44,9 @@ void Recordable::SetStartTime(opentelemetry::core::SystemTimestamp start_time) n span_.set_start_time_unixnano(nano_unix_time); } -void Recordable::SetDuration(std::chrono::nanoseconds duration) noexcept { +void Recordable::SetDuration(std::chrono::nanoseconds duration) noexcept +{ uint64_t unix_duration = duration.count(); - uint64_t unix_end_time = span_.start_time_unixnano() + unix_duration; span_.set_end_time_unixnano(unix_end_time); } diff --git a/exporters/otlp/recordable_test.cc b/exporters/otlp/recordable_test.cc new file mode 100644 index 0000000000..e4131a55a0 --- /dev/null +++ b/exporters/otlp/recordable_test.cc @@ -0,0 +1,51 @@ +#include "exporters/otlp/recordable.h" + +#include + +using namespace opentelemetry::exporter::otlp; + +OPENTELEMETRY_BEGIN_NAMESPACE + +TEST(RecordableTest, OtlpRecordable) +{ + Recordable rec; + + // For SetName + nostd::string_view name = "TestSpan"; + + // For SetStartTime + std::chrono::system_clock::time_point start_time = std::chrono::system_clock::now(); + core::SystemTimestamp start_timestamp(start_time); + + uint64_t unix_start = std::chrono::duration_cast( + start_time.time_since_epoch()).count(); + + // For SetDuration + std::chrono::nanoseconds duration(10); + uint64_t unix_end = unix_start + duration.count(); + + // For SetIds + // trace::TraceId trace_id(); + // trace::SpanId span_id(); + // trace::SpanId parent_span_id(); + + // nostd::span span_trace_id(0, 16); + // nostd::span span_span_id(0, 8); + // nostd::span span_parent_span_id(0, 8); + + // trace::TraceId trace_id(span_trace_id); + // trace::SpanId span_id(span_span_id); + // trace::SpanId parent_span_id(span_parent_span_id); + + rec.SetName(name); + rec.SetStartTime(start_timestamp); + rec.SetDuration(duration); + //rec.SetIds(trace_id, span_id, parent_span_id); + + ASSERT_EQ(rec.span().name(), name); + ASSERT_EQ(rec.span().start_time_unixnano(), unix_start); + ASSERT_EQ(rec.span().end_time_unixnano(), unix_end); + + //ASSERT_EQ(rec.span().trace_id(), trace_id); +} +OPENTELEMETRY_END_NAMESPACE \ No newline at end of file From 249b30895b7275cb5cac4bf99e6490a118ab7627 Mon Sep 17 00:00:00 2001 From: Nadia Ciobanu Date: Wed, 24 Jun 2020 00:33:19 +0000 Subject: [PATCH 10/41] Move SetIds to its own test --- exporters/otlp/recordable.cc | 3 +- exporters/otlp/recordable_test.cc | 58 ++++++++++++++++++++++--------- 2 files changed, 43 insertions(+), 18 deletions(-) diff --git a/exporters/otlp/recordable.cc b/exporters/otlp/recordable.cc index 23739791b1..6821e255e7 100644 --- a/exporters/otlp/recordable.cc +++ b/exporters/otlp/recordable.cc @@ -7,7 +7,8 @@ namespace otlp { void Recordable::SetIds(trace::TraceId trace_id, trace::SpanId span_id, - trace::SpanId parent_span_id) noexcept { + trace::SpanId parent_span_id) noexcept +{ char trace_id_buff[32] = {0}; char span_id_buff[16] = {0}; char parent_span_id_buff[16] = {0}; diff --git a/exporters/otlp/recordable_test.cc b/exporters/otlp/recordable_test.cc index e4131a55a0..30247ce062 100644 --- a/exporters/otlp/recordable_test.cc +++ b/exporters/otlp/recordable_test.cc @@ -6,7 +6,47 @@ using namespace opentelemetry::exporter::otlp; OPENTELEMETRY_BEGIN_NAMESPACE -TEST(RecordableTest, OtlpRecordable) +TEST(Recordable, SetIds) +{ + Recordable rec; + + // trace::TraceId trace_id(); + // trace::SpanId span_id(); + // trace::SpanId parent_span_id(); + + // nostd::span span_trace_id(0, 16); + // nostd::span span_span_id(0, 8); + // nostd::span span_parent_span_id(0, 8); + + // trace::TraceId trace_id(span_trace_id); + // trace::SpanId span_id(span_span_id); + // trace::SpanId parent_span_id(span_parent_span_id); + + // constexpr uint8_t buf[] = {1, 2, 3, 4, 5, 6, 7, 8}; + // trace::TraceId trace_id(buf); + // trace::SpanId span_id(buf); + // trace::SpanId parent_span_id(buf); + + opentelemetry::trace::TraceId trace_id; + opentelemetry::trace::SpanId span_id; + opentelemetry::trace::SpanId parent_span_id; + + char trace_id_buff[32] = {0}; + char span_id_buff[16] = {0}; + char parent_span_id_buff[16] = {0}; + + trace_id.ToLowerBase16(trace_id_buff); + span_id.ToLowerBase16(span_id_buff); + parent_span_id.ToLowerBase16(parent_span_id_buff); + + rec.SetIds(trace_id, span_id, parent_span_id); + + ASSERT_EQ(rec.span().trace_id(), trace_id_buff); + ASSERT_EQ(rec.span().span_id(), span_id_buff); + ASSERT_EQ(rec.span().parent_span_id(), parent_span_id_buff); +} + +TEST(Recordable, OtlpRecordable) { Recordable rec; @@ -24,28 +64,12 @@ TEST(RecordableTest, OtlpRecordable) std::chrono::nanoseconds duration(10); uint64_t unix_end = unix_start + duration.count(); - // For SetIds - // trace::TraceId trace_id(); - // trace::SpanId span_id(); - // trace::SpanId parent_span_id(); - - // nostd::span span_trace_id(0, 16); - // nostd::span span_span_id(0, 8); - // nostd::span span_parent_span_id(0, 8); - - // trace::TraceId trace_id(span_trace_id); - // trace::SpanId span_id(span_span_id); - // trace::SpanId parent_span_id(span_parent_span_id); - rec.SetName(name); rec.SetStartTime(start_timestamp); rec.SetDuration(duration); - //rec.SetIds(trace_id, span_id, parent_span_id); ASSERT_EQ(rec.span().name(), name); ASSERT_EQ(rec.span().start_time_unixnano(), unix_start); ASSERT_EQ(rec.span().end_time_unixnano(), unix_end); - - //ASSERT_EQ(rec.span().trace_id(), trace_id); } OPENTELEMETRY_END_NAMESPACE \ No newline at end of file From b85b49a77d0388ae0f71f16c442cced070b5f714 Mon Sep 17 00:00:00 2001 From: Nadia Ciobanu Date: Wed, 24 Jun 2020 18:21:42 +0000 Subject: [PATCH 11/41] Fix SetIds by not converting to hex --- exporters/otlp/recordable.cc | 13 +++++++------ exporters/otlp/recordable_test.cc | 30 ++++-------------------------- 2 files changed, 11 insertions(+), 32 deletions(-) diff --git a/exporters/otlp/recordable.cc b/exporters/otlp/recordable.cc index 6821e255e7..33ce6a0404 100644 --- a/exporters/otlp/recordable.cc +++ b/exporters/otlp/recordable.cc @@ -9,13 +9,14 @@ void Recordable::SetIds(trace::TraceId trace_id, trace::SpanId span_id, trace::SpanId parent_span_id) noexcept { - char trace_id_buff[32] = {0}; - char span_id_buff[16] = {0}; - char parent_span_id_buff[16] = {0}; + const uint8_t* trace_id_data = trace_id.Id().data(); + const char* trace_id_buff = (char*)trace_id_data; - trace_id.ToLowerBase16(trace_id_buff); - span_id.ToLowerBase16(span_id_buff); - parent_span_id.ToLowerBase16(parent_span_id_buff); + const uint8_t* span_id_data = span_id.Id().data(); + const char* span_id_buff = (char*)span_id_data; + + const uint8_t* parent_span_id_data = parent_span_id.Id().data(); + const char* parent_span_id_buff = (char*)parent_span_id_data; span_.set_trace_id(trace_id_buff); span_.set_span_id(span_id_buff); diff --git a/exporters/otlp/recordable_test.cc b/exporters/otlp/recordable_test.cc index 30247ce062..589aa71571 100644 --- a/exporters/otlp/recordable_test.cc +++ b/exporters/otlp/recordable_test.cc @@ -10,40 +10,18 @@ TEST(Recordable, SetIds) { Recordable rec; - // trace::TraceId trace_id(); - // trace::SpanId span_id(); - // trace::SpanId parent_span_id(); - - // nostd::span span_trace_id(0, 16); - // nostd::span span_span_id(0, 8); - // nostd::span span_parent_span_id(0, 8); - - // trace::TraceId trace_id(span_trace_id); - // trace::SpanId span_id(span_span_id); - // trace::SpanId parent_span_id(span_parent_span_id); - - // constexpr uint8_t buf[] = {1, 2, 3, 4, 5, 6, 7, 8}; - // trace::TraceId trace_id(buf); - // trace::SpanId span_id(buf); - // trace::SpanId parent_span_id(buf); - opentelemetry::trace::TraceId trace_id; opentelemetry::trace::SpanId span_id; opentelemetry::trace::SpanId parent_span_id; - char trace_id_buff[32] = {0}; - char span_id_buff[16] = {0}; - char parent_span_id_buff[16] = {0}; - - trace_id.ToLowerBase16(trace_id_buff); - span_id.ToLowerBase16(span_id_buff); - parent_span_id.ToLowerBase16(parent_span_id_buff); - rec.SetIds(trace_id, span_id, parent_span_id); + char trace_id_buff[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; + char span_id_buff[8] = {0,0,0,0,0,0,0,0}; + ASSERT_EQ(rec.span().trace_id(), trace_id_buff); ASSERT_EQ(rec.span().span_id(), span_id_buff); - ASSERT_EQ(rec.span().parent_span_id(), parent_span_id_buff); + ASSERT_EQ(rec.span().parent_span_id(), span_id_buff); } TEST(Recordable, OtlpRecordable) From 78cf8e0f8d0b5af6cfbe77c985978ba3c9eeb4ec Mon Sep 17 00:00:00 2001 From: Nadia Ciobanu Date: Wed, 24 Jun 2020 19:02:30 +0000 Subject: [PATCH 12/41] Reformat tests --- exporters/otlp/recordable.cc | 7 ++-- exporters/otlp/recordable_test.cc | 57 ++++++++++++++++++------------- 2 files changed, 37 insertions(+), 27 deletions(-) diff --git a/exporters/otlp/recordable.cc b/exporters/otlp/recordable.cc index 33ce6a0404..b695faf6bd 100644 --- a/exporters/otlp/recordable.cc +++ b/exporters/otlp/recordable.cc @@ -10,12 +10,11 @@ void Recordable::SetIds(trace::TraceId trace_id, trace::SpanId parent_span_id) noexcept { const uint8_t* trace_id_data = trace_id.Id().data(); - const char* trace_id_buff = (char*)trace_id_data; - const uint8_t* span_id_data = span_id.Id().data(); - const char* span_id_buff = (char*)span_id_data; - const uint8_t* parent_span_id_data = parent_span_id.Id().data(); + + const char* trace_id_buff = (char*)trace_id_data; + const char* span_id_buff = (char*)span_id_data; const char* parent_span_id_buff = (char*)parent_span_id_data; span_.set_trace_id(trace_id_buff); diff --git a/exporters/otlp/recordable_test.cc b/exporters/otlp/recordable_test.cc index 589aa71571..3d14635be9 100644 --- a/exporters/otlp/recordable_test.cc +++ b/exporters/otlp/recordable_test.cc @@ -4,50 +4,61 @@ using namespace opentelemetry::exporter::otlp; -OPENTELEMETRY_BEGIN_NAMESPACE - TEST(Recordable, SetIds) { Recordable rec; - opentelemetry::trace::TraceId trace_id; - opentelemetry::trace::SpanId span_id; - opentelemetry::trace::SpanId parent_span_id; + const uint8_t trace_id_buff[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}; + const uint8_t span_id_buff[8] = {0,0,0,0,0,0,0,2}; + const uint8_t parent_span_id_buff[8] = {0,0,0,0,0,0,0,3}; - rec.SetIds(trace_id, span_id, parent_span_id); + opentelemetry::nostd::spantrace_id_span(trace_id_buff, 16); + opentelemetry::nostd::span span_id_span(span_id_buff, 8); + opentelemetry::nostd::span parent_span_id_span(parent_span_id_buff, 8); - char trace_id_buff[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; - char span_id_buff[8] = {0,0,0,0,0,0,0,0}; + opentelemetry::trace::TraceId trace_id(trace_id_span); + opentelemetry::trace::SpanId span_id(span_id_span); + opentelemetry::trace::SpanId parent_span_id(parent_span_id_span); - ASSERT_EQ(rec.span().trace_id(), trace_id_buff); - ASSERT_EQ(rec.span().span_id(), span_id_buff); - ASSERT_EQ(rec.span().parent_span_id(), span_id_buff); + rec.SetIds(trace_id, span_id, parent_span_id); + + ASSERT_EQ(rec.span().trace_id(), (char*)trace_id_buff); + ASSERT_EQ(rec.span().span_id(), (char*)span_id_buff); + ASSERT_EQ(rec.span().parent_span_id(), (char*)parent_span_id_buff); } -TEST(Recordable, OtlpRecordable) +TEST(Recordable, SetName) { Recordable rec; + opentelemetry::nostd::string_view name = "TestSpan"; + rec.SetName(name); + ASSERT_EQ(rec.span().name(), name); +} - // For SetName - nostd::string_view name = "TestSpan"; - - // For SetStartTime +TEST(Recordable, SetStartTime) +{ + Recordable rec; std::chrono::system_clock::time_point start_time = std::chrono::system_clock::now(); - core::SystemTimestamp start_timestamp(start_time); + opentelemetry::core::SystemTimestamp start_timestamp(start_time); uint64_t unix_start = std::chrono::duration_cast( start_time.time_since_epoch()).count(); - // For SetDuration + rec.SetStartTime(start_timestamp); + ASSERT_EQ(rec.span().start_time_unixnano(), unix_start); +} + +TEST(Recordable, SetDuration) +{ + Recordable rec; + // Start time is 0 + opentelemetry::core::SystemTimestamp start_timestamp; + std::chrono::nanoseconds duration(10); - uint64_t unix_end = unix_start + duration.count(); + uint64_t unix_end = duration.count(); - rec.SetName(name); rec.SetStartTime(start_timestamp); rec.SetDuration(duration); - ASSERT_EQ(rec.span().name(), name); - ASSERT_EQ(rec.span().start_time_unixnano(), unix_start); ASSERT_EQ(rec.span().end_time_unixnano(), unix_end); } -OPENTELEMETRY_END_NAMESPACE \ No newline at end of file From a53e87166723bb783f30850d3c7128346d264118 Mon Sep 17 00:00:00 2001 From: Nadia Ciobanu Date: Wed, 24 Jun 2020 19:40:35 +0000 Subject: [PATCH 13/41] Fix Recordable instantiation in test --- exporters/otlp/recordable_test.cc | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/exporters/otlp/recordable_test.cc b/exporters/otlp/recordable_test.cc index 3d14635be9..893820d523 100644 --- a/exporters/otlp/recordable_test.cc +++ b/exporters/otlp/recordable_test.cc @@ -6,7 +6,7 @@ using namespace opentelemetry::exporter::otlp; TEST(Recordable, SetIds) { - Recordable rec; + Recordable* rec = new Recordable(); const uint8_t trace_id_buff[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}; const uint8_t span_id_buff[8] = {0,0,0,0,0,0,0,2}; @@ -20,45 +20,45 @@ TEST(Recordable, SetIds) opentelemetry::trace::SpanId span_id(span_id_span); opentelemetry::trace::SpanId parent_span_id(parent_span_id_span); - rec.SetIds(trace_id, span_id, parent_span_id); + rec->SetIds(trace_id, span_id, parent_span_id); - ASSERT_EQ(rec.span().trace_id(), (char*)trace_id_buff); - ASSERT_EQ(rec.span().span_id(), (char*)span_id_buff); - ASSERT_EQ(rec.span().parent_span_id(), (char*)parent_span_id_buff); + ASSERT_EQ(rec->span().trace_id(), (char*)trace_id_buff); + ASSERT_EQ(rec->span().span_id(), (char*)span_id_buff); + ASSERT_EQ(rec->span().parent_span_id(), (char*)parent_span_id_buff); } TEST(Recordable, SetName) { - Recordable rec; + Recordable* rec = new Recordable(); opentelemetry::nostd::string_view name = "TestSpan"; - rec.SetName(name); - ASSERT_EQ(rec.span().name(), name); + rec->SetName(name); + ASSERT_EQ(rec->span().name(), name); } TEST(Recordable, SetStartTime) { - Recordable rec; + Recordable* rec = new Recordable(); std::chrono::system_clock::time_point start_time = std::chrono::system_clock::now(); opentelemetry::core::SystemTimestamp start_timestamp(start_time); uint64_t unix_start = std::chrono::duration_cast( start_time.time_since_epoch()).count(); - rec.SetStartTime(start_timestamp); - ASSERT_EQ(rec.span().start_time_unixnano(), unix_start); + rec->SetStartTime(start_timestamp); + ASSERT_EQ(rec->span().start_time_unixnano(), unix_start); } TEST(Recordable, SetDuration) { - Recordable rec; + Recordable* rec = new Recordable(); // Start time is 0 opentelemetry::core::SystemTimestamp start_timestamp; std::chrono::nanoseconds duration(10); uint64_t unix_end = duration.count(); - rec.SetStartTime(start_timestamp); - rec.SetDuration(duration); + rec->SetStartTime(start_timestamp); + rec->SetDuration(duration); - ASSERT_EQ(rec.span().end_time_unixnano(), unix_end); + ASSERT_EQ(rec->span().end_time_unixnano(), unix_end); } From ce61e404345e81590d6cddd73e968693f71dc58c Mon Sep 17 00:00:00 2001 From: Nadia Ciobanu Date: Wed, 24 Jun 2020 23:40:31 +0000 Subject: [PATCH 14/41] Update code for new function (SetAttribute) --- exporters/otlp/recordable.cc | 7 +++++++ exporters/otlp/recordable.h | 3 +++ 2 files changed, 10 insertions(+) diff --git a/exporters/otlp/recordable.cc b/exporters/otlp/recordable.cc index b695faf6bd..2ba6de6398 100644 --- a/exporters/otlp/recordable.cc +++ b/exporters/otlp/recordable.cc @@ -22,6 +22,13 @@ void Recordable::SetIds(trace::TraceId trace_id, span_.set_parent_span_id(parent_span_id_buff); } +void SetAttribute(nostd::string_view key, + const opentelemetry::common::AttributeValue &&value) noexcept +{ + (void)key; + (void)value; +} + void Recordable::AddEvent(nostd::string_view name, core::SystemTimestamp timestamp) noexcept { (void)name; diff --git a/exporters/otlp/recordable.h b/exporters/otlp/recordable.h index ace4e05f35..8490bc9141 100644 --- a/exporters/otlp/recordable.h +++ b/exporters/otlp/recordable.h @@ -18,6 +18,9 @@ class Recordable final : public sdk::trace::Recordable trace::SpanId span_id, trace::SpanId parent_span_id) noexcept override; + void SetAttribute(nostd::string_view key, + const opentelemetry::common::AttributeValue &&value) noexcept override; + void AddEvent(nostd::string_view name, core::SystemTimestamp timestamp) noexcept override; void SetStatus(trace::CanonicalCode code, nostd::string_view description) noexcept override; From 11ce576c1af66cbd631746fce057865ce00ab679 Mon Sep 17 00:00:00 2001 From: Nadia Ciobanu Date: Wed, 24 Jun 2020 23:50:03 +0000 Subject: [PATCH 15/41] Fix undefined reference error --- exporters/otlp/recordable.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/exporters/otlp/recordable.cc b/exporters/otlp/recordable.cc index 2ba6de6398..6ddc66d003 100644 --- a/exporters/otlp/recordable.cc +++ b/exporters/otlp/recordable.cc @@ -22,8 +22,8 @@ void Recordable::SetIds(trace::TraceId trace_id, span_.set_parent_span_id(parent_span_id_buff); } -void SetAttribute(nostd::string_view key, - const opentelemetry::common::AttributeValue &&value) noexcept +void Recordable::SetAttribute(nostd::string_view key, + const opentelemetry::common::AttributeValue &&value) noexcept { (void)key; (void)value; From bc98b812b1350b706ccf3c9f787b73ffb37a30ad Mon Sep 17 00:00:00 2001 From: Nadia Ciobanu Date: Thu, 25 Jun 2020 00:05:24 +0000 Subject: [PATCH 16/41] Fix memory leak --- exporters/otlp/recordable_test.cc | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/exporters/otlp/recordable_test.cc b/exporters/otlp/recordable_test.cc index 893820d523..3d14635be9 100644 --- a/exporters/otlp/recordable_test.cc +++ b/exporters/otlp/recordable_test.cc @@ -6,7 +6,7 @@ using namespace opentelemetry::exporter::otlp; TEST(Recordable, SetIds) { - Recordable* rec = new Recordable(); + Recordable rec; const uint8_t trace_id_buff[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}; const uint8_t span_id_buff[8] = {0,0,0,0,0,0,0,2}; @@ -20,45 +20,45 @@ TEST(Recordable, SetIds) opentelemetry::trace::SpanId span_id(span_id_span); opentelemetry::trace::SpanId parent_span_id(parent_span_id_span); - rec->SetIds(trace_id, span_id, parent_span_id); + rec.SetIds(trace_id, span_id, parent_span_id); - ASSERT_EQ(rec->span().trace_id(), (char*)trace_id_buff); - ASSERT_EQ(rec->span().span_id(), (char*)span_id_buff); - ASSERT_EQ(rec->span().parent_span_id(), (char*)parent_span_id_buff); + ASSERT_EQ(rec.span().trace_id(), (char*)trace_id_buff); + ASSERT_EQ(rec.span().span_id(), (char*)span_id_buff); + ASSERT_EQ(rec.span().parent_span_id(), (char*)parent_span_id_buff); } TEST(Recordable, SetName) { - Recordable* rec = new Recordable(); + Recordable rec; opentelemetry::nostd::string_view name = "TestSpan"; - rec->SetName(name); - ASSERT_EQ(rec->span().name(), name); + rec.SetName(name); + ASSERT_EQ(rec.span().name(), name); } TEST(Recordable, SetStartTime) { - Recordable* rec = new Recordable(); + Recordable rec; std::chrono::system_clock::time_point start_time = std::chrono::system_clock::now(); opentelemetry::core::SystemTimestamp start_timestamp(start_time); uint64_t unix_start = std::chrono::duration_cast( start_time.time_since_epoch()).count(); - rec->SetStartTime(start_timestamp); - ASSERT_EQ(rec->span().start_time_unixnano(), unix_start); + rec.SetStartTime(start_timestamp); + ASSERT_EQ(rec.span().start_time_unixnano(), unix_start); } TEST(Recordable, SetDuration) { - Recordable* rec = new Recordable(); + Recordable rec; // Start time is 0 opentelemetry::core::SystemTimestamp start_timestamp; std::chrono::nanoseconds duration(10); uint64_t unix_end = duration.count(); - rec->SetStartTime(start_timestamp); - rec->SetDuration(duration); + rec.SetStartTime(start_timestamp); + rec.SetDuration(duration); - ASSERT_EQ(rec->span().end_time_unixnano(), unix_end); + ASSERT_EQ(rec.span().end_time_unixnano(), unix_end); } From a61c01f66f41f5292df15938715eccb01b752443 Mon Sep 17 00:00:00 2001 From: Nadia Ciobanu Date: Thu, 25 Jun 2020 17:05:10 +0000 Subject: [PATCH 17/41] Address review comments --- exporters/otlp/recordable.cc | 17 ++---- exporters/otlp/recordable_test.cc | 89 +++++++++++++++++-------------- 2 files changed, 54 insertions(+), 52 deletions(-) diff --git a/exporters/otlp/recordable.cc b/exporters/otlp/recordable.cc index 6ddc66d003..4af62db8c6 100644 --- a/exporters/otlp/recordable.cc +++ b/exporters/otlp/recordable.cc @@ -9,17 +9,10 @@ void Recordable::SetIds(trace::TraceId trace_id, trace::SpanId span_id, trace::SpanId parent_span_id) noexcept { - const uint8_t* trace_id_data = trace_id.Id().data(); - const uint8_t* span_id_data = span_id.Id().data(); - const uint8_t* parent_span_id_data = parent_span_id.Id().data(); - - const char* trace_id_buff = (char*)trace_id_data; - const char* span_id_buff = (char*)span_id_data; - const char* parent_span_id_buff = (char*)parent_span_id_data; - - span_.set_trace_id(trace_id_buff); - span_.set_span_id(span_id_buff); - span_.set_parent_span_id(parent_span_id_buff); + span_.set_trace_id(reinterpret_cast(trace_id.Id().data()), trace::TraceId::kSize); + span_.set_span_id(reinterpret_cast(span_id.Id().data()), trace::SpanId::kSize); + span_.set_parent_span_id(reinterpret_cast(parent_span_id.Id().data()), + trace::SpanId::kSize); } void Recordable::SetAttribute(nostd::string_view key, @@ -48,7 +41,7 @@ void Recordable::SetName(nostd::string_view name) noexcept void Recordable::SetStartTime(opentelemetry::core::SystemTimestamp start_time) noexcept { std::chrono::nanoseconds nano_time = start_time.time_since_epoch(); - uint64_t nano_unix_time = nano_time.count(); + uint64_t nano_unix_time = nano_time.count(); span_.set_start_time_unixnano(nano_unix_time); } diff --git a/exporters/otlp/recordable_test.cc b/exporters/otlp/recordable_test.cc index 3d14635be9..0318bd0587 100644 --- a/exporters/otlp/recordable_test.cc +++ b/exporters/otlp/recordable_test.cc @@ -2,63 +2,72 @@ #include -using namespace opentelemetry::exporter::otlp; - +OPENTELEMETRY_BEGIN_NAMESPACE +namespace exporter +{ +namespace otlp +{ TEST(Recordable, SetIds) { - Recordable rec; - - const uint8_t trace_id_buff[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}; - const uint8_t span_id_buff[8] = {0,0,0,0,0,0,0,2}; - const uint8_t parent_span_id_buff[8] = {0,0,0,0,0,0,0,3}; - - opentelemetry::nostd::spantrace_id_span(trace_id_buff, 16); - opentelemetry::nostd::span span_id_span(span_id_buff, 8); - opentelemetry::nostd::span parent_span_id_span(parent_span_id_buff, 8); - - opentelemetry::trace::TraceId trace_id(trace_id_span); - opentelemetry::trace::SpanId span_id(span_id_span); - opentelemetry::trace::SpanId parent_span_id(parent_span_id_span); - - rec.SetIds(trace_id, span_id, parent_span_id); - - ASSERT_EQ(rec.span().trace_id(), (char*)trace_id_buff); - ASSERT_EQ(rec.span().span_id(), (char*)span_id_buff); - ASSERT_EQ(rec.span().parent_span_id(), (char*)parent_span_id_buff); + Recordable rec; + + const uint8_t trace_id_buff[trace::TraceId::kSize] = {0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 1}; + const uint8_t span_id_buff[trace::SpanId::kSize] = {0, 0, 0, 0, 0, 0, 0, 2}; + const uint8_t parent_span_id_buff[trace::SpanId::kSize] = {0, 0, 0, 0, 0, 0, 0, 3}; + + const trace::TraceId trace_id( + nostd::span(trace_id_buff, trace::TraceId::kSize)); + const trace::SpanId span_id( + nostd::span(span_id_buff, trace::SpanId::kSize)); + const trace::SpanId parent_span_id( + nostd::span(parent_span_id_buff, trace::SpanId::kSize)); + + rec.SetIds(trace_id, span_id, parent_span_id); + + EXPECT_EQ(rec.span().trace_id(), + std::string(reinterpret_cast(trace_id_buff), trace::TraceId::kSize)); + EXPECT_EQ(rec.span().span_id(), + std::string(reinterpret_cast(span_id_buff), trace::SpanId::kSize)); + EXPECT_EQ(rec.span().parent_span_id(), + std::string(reinterpret_cast(parent_span_id_buff), trace::SpanId::kSize)); } TEST(Recordable, SetName) { - Recordable rec; - opentelemetry::nostd::string_view name = "TestSpan"; - rec.SetName(name); - ASSERT_EQ(rec.span().name(), name); + Recordable rec; + nostd::string_view name = "TestSpan"; + rec.SetName(name); + ASSERT_EQ(rec.span().name(), name); } TEST(Recordable, SetStartTime) { - Recordable rec; - std::chrono::system_clock::time_point start_time = std::chrono::system_clock::now(); - opentelemetry::core::SystemTimestamp start_timestamp(start_time); + Recordable rec; + std::chrono::system_clock::time_point start_time = std::chrono::system_clock::now(); + core::SystemTimestamp start_timestamp(start_time); - uint64_t unix_start = std::chrono::duration_cast( - start_time.time_since_epoch()).count(); + uint64_t unix_start = + std::chrono::duration_cast(start_time.time_since_epoch()).count(); - rec.SetStartTime(start_timestamp); - ASSERT_EQ(rec.span().start_time_unixnano(), unix_start); + rec.SetStartTime(start_timestamp); + ASSERT_EQ(rec.span().start_time_unixnano(), unix_start); } TEST(Recordable, SetDuration) { - Recordable rec; - // Start time is 0 - opentelemetry::core::SystemTimestamp start_timestamp; + Recordable rec; + // Start time is 0 + core::SystemTimestamp start_timestamp; - std::chrono::nanoseconds duration(10); - uint64_t unix_end = duration.count(); + std::chrono::nanoseconds duration(10); + uint64_t unix_end = duration.count(); - rec.SetStartTime(start_timestamp); - rec.SetDuration(duration); + rec.SetStartTime(start_timestamp); + rec.SetDuration(duration); - ASSERT_EQ(rec.span().end_time_unixnano(), unix_end); + ASSERT_EQ(rec.span().end_time_unixnano(), unix_end); } +} // namespace otlp +} // namespace exporter +OPENTELEMETRY_END_NAMESPACE From 69c5c3204ea3f53274d2ae274852ddb05dd079f5 Mon Sep 17 00:00:00 2001 From: Nadia Ciobanu Date: Thu, 25 Jun 2020 20:39:47 +0000 Subject: [PATCH 18/41] Return OTLP Recordable --- exporters/otlp/otlp_exporter.h | 54 +++++++++++++++++--------------- exporters/otlp/stdout_exporter.h | 52 ------------------------------ 2 files changed, 28 insertions(+), 78 deletions(-) delete mode 100644 exporters/otlp/stdout_exporter.h diff --git a/exporters/otlp/otlp_exporter.h b/exporters/otlp/otlp_exporter.h index 276dd2c0aa..202fc501f6 100644 --- a/exporters/otlp/otlp_exporter.h +++ b/exporters/otlp/otlp_exporter.h @@ -18,38 +18,40 @@ class OtlpExporter final : public sdktrace::SpanExporter { std::unique_ptr MakeRecordable() noexcept { - return std::unique_ptr(new sdktrace::SpanData); + //return std::unique_ptr(new sdktrace::SpanData); + return std::unique_ptr(); } sdktrace::ExportResult Export( const nostd::span> &spans) noexcept { - for (auto &recordable : spans) - { - auto span = std::unique_ptr( - static_cast(recordable.release())); - - if (span != nullptr) - { - 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); - - std::cout << "{" - << "\n name : " << span->GetName() - << "\n trace_id : " << std::string(trace_id, 32) - << "\n span_id : " << std::string(span_id, 16) - << "\n parent_span_id: " << std::string(parent_span_id, 16) - << "\n start : " << span->GetStartTime().time_since_epoch().count() - << "\n duration : " << span->GetDuration().count() << "\n}" - << "\n"; - } - } + // for (auto &recordable : spans) + // { + // auto span = std::unique_ptr( + // static_cast(recordable.release())); + // if (span != nullptr) + // { + // 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); + + // std::cout << "{" + // << "\n name : " << span->GetName() + // << "\n trace_id : " << std::string(trace_id, 32) + // << "\n span_id : " << std::string(span_id, 16) + // << "\n parent_span_id: " << std::string(parent_span_id, 16) + // << "\n start : " << span->GetStartTime().time_since_epoch().count() + // << "\n duration : " << span->GetDuration().count() << "\n}" + // << "\n"; + // } + // } + + std::cout << "Hi" << std::endl; return sdktrace::ExportResult::kSuccess; } diff --git a/exporters/otlp/stdout_exporter.h b/exporters/otlp/stdout_exporter.h deleted file mode 100644 index 6a58c5b8d6..0000000000 --- a/exporters/otlp/stdout_exporter.h +++ /dev/null @@ -1,52 +0,0 @@ -#pragma once - -#include "opentelemetry/sdk/trace/exporter.h" -#include "opentelemetry/sdk/trace/span_data.h" - -#include - -namespace trace = opentelemetry::trace; -namespace nostd = opentelemetry::nostd; -namespace sdktrace = opentelemetry::sdk::trace; - -class StdoutExporter final : public sdktrace::SpanExporter -{ - std::unique_ptr MakeRecordable() noexcept - { - return std::unique_ptr(new sdktrace::SpanData); - } - - sdktrace::ExportResult Export( - const nostd::span> &spans) noexcept - { - for (auto &recordable : spans) - { - auto span = std::unique_ptr( - static_cast(recordable.release())); - - if (span != nullptr) - { - 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); - - std::cout << "{" - << "\n name : " << span->GetName() - << "\n trace_id : " << std::string(trace_id, 32) - << "\n span_id : " << std::string(span_id, 16) - << "\n parent_span_id: " << std::string(parent_span_id, 16) - << "\n start : " << span->GetStartTime().time_since_epoch().count() - << "\n duration : " << span->GetDuration().count() << "\n}" - << "\n"; - } - } - - return sdktrace::ExportResult::kSuccess; - } - - void Shutdown(std::chrono::microseconds timeout = std::chrono::microseconds(0)) noexcept {} -}; From de7c586b75ae5d8552b15b31332ca4e261fdd443 Mon Sep 17 00:00:00 2001 From: Nadia Ciobanu Date: Fri, 26 Jun 2020 00:22:26 +0000 Subject: [PATCH 19/41] Return OTLP Recordable in MakeRecordable --- exporters/otlp/otlp_exporter.h | 46 ++++++++++++---------------------- 1 file changed, 16 insertions(+), 30 deletions(-) diff --git a/exporters/otlp/otlp_exporter.h b/exporters/otlp/otlp_exporter.h index 202fc501f6..01a702a76a 100644 --- a/exporters/otlp/otlp_exporter.h +++ b/exporters/otlp/otlp_exporter.h @@ -13,52 +13,38 @@ namespace trace = opentelemetry::trace; namespace nostd = opentelemetry::nostd; namespace sdktrace = opentelemetry::sdk::trace; +namespace otlpexporter = opentelemetry::exporter::otlp; class OtlpExporter final : public sdktrace::SpanExporter { std::unique_ptr MakeRecordable() noexcept { - //return std::unique_ptr(new sdktrace::SpanData); - return std::unique_ptr(); + return std::unique_ptr(new opentelemetry::exporter::otlp::Recordable); } sdktrace::ExportResult Export( const nostd::span> &spans) noexcept { - // for (auto &recordable : spans) - // { - // auto span = std::unique_ptr( - // static_cast(recordable.release())); + // grpc::ClientContext context; - // if (span != nullptr) - // { - // char trace_id[32] = {0}; - // char span_id[16] = {0}; - // char parent_span_id[16] = {0}; + // opentelemetry::proto::collector::trace::v1::ExportTraceServiceRequest request; + // opentelemetry::proto::collector::trace::v1::ExportTraceServiceResponse response; - // span->GetTraceId().ToLowerBase16(trace_id); - // span->GetSpanId().ToLowerBase16(span_id); - // span->GetParentSpanId().ToLowerBase16(parent_span_id); + // std::unique_ptr< + // opentelemetry::proto::collector::trace::v1::TraceService::StubInterface> + // trace_service_stub; - // std::cout << "{" - // << "\n name : " << span->GetName() - // << "\n trace_id : " << std::string(trace_id, 32) - // << "\n span_id : " << std::string(span_id, 16) - // << "\n parent_span_id: " << std::string(parent_span_id, 16) - // << "\n start : " << span->GetStartTime().time_since_epoch().count() - // << "\n duration : " << span->GetDuration().count() << "\n}" - // << "\n"; - // } - // } + // trace_service_stub->Export(&context, request, &response); + + for (auto &recordable : spans) { + auto rec = std::unique_ptr( + static_cast(recordable.release())); + + std::cout << "Name: " << rec->span().name() << std::endl; + } - std::cout << "Hi" << std::endl; return sdktrace::ExportResult::kSuccess; } void Shutdown(std::chrono::microseconds timeout = std::chrono::microseconds(0)) noexcept {} - - - std::unique_ptr< - opentelemetry::proto::collector::trace::v1::TraceService::StubInterface> - trace_service_stub; }; From 2dbb94c27f2f3bd81f8c963687b9ef4005ccfb86 Mon Sep 17 00:00:00 2001 From: Nadia Ciobanu Date: Fri, 26 Jun 2020 20:45:09 +0000 Subject: [PATCH 20/41] Use resource spans --- exporters/otlp/otlp_exporter.h | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/exporters/otlp/otlp_exporter.h b/exporters/otlp/otlp_exporter.h index 01a702a76a..c23bdd446c 100644 --- a/exporters/otlp/otlp_exporter.h +++ b/exporters/otlp/otlp_exporter.h @@ -6,6 +6,8 @@ #include "opentelemetry/proto/collector/trace/v1/trace_service.pb.h" #include "opentelemetry/proto/collector/trace/v1/trace_service.grpc.pb.h" +#include "opentelemetry/proto/trace/v1/trace.pb.h" + #include "recordable.h" #include @@ -25,14 +27,17 @@ class OtlpExporter final : public sdktrace::SpanExporter sdktrace::ExportResult Export( const nostd::span> &spans) noexcept { - // grpc::ClientContext context; + std::cout << "Exporting" << std::endl; + grpc::ClientContext context; + + opentelemetry::proto::collector::trace::v1::ExportTraceServiceRequest request; + opentelemetry::proto::collector::trace::v1::ExportTraceServiceResponse response; - // opentelemetry::proto::collector::trace::v1::ExportTraceServiceRequest request; - // opentelemetry::proto::collector::trace::v1::ExportTraceServiceResponse response; + std::unique_ptr< + opentelemetry::proto::collector::trace::v1::TraceService::StubInterface> + trace_service_stub; - // std::unique_ptr< - // opentelemetry::proto::collector::trace::v1::TraceService::StubInterface> - // trace_service_stub; + opentelemetry::proto::trace::v1::ResourceSpans resource_spans; // trace_service_stub->Export(&context, request, &response); @@ -40,7 +45,10 @@ class OtlpExporter final : public sdktrace::SpanExporter auto rec = std::unique_ptr( static_cast(recordable.release())); - std::cout << "Name: " << rec->span().name() << std::endl; + //std::cout << "Name: " << rec->span().name() << std::endl; + + //request.add_resource_spans(); + //trace_service_stub->Export(&context, request, &response); } return sdktrace::ExportResult::kSuccess; From 9d39c2f47317e056856f1792978b314ef2936a60 Mon Sep 17 00:00:00 2001 From: Nadia Ciobanu Date: Fri, 26 Jun 2020 22:42:27 +0000 Subject: [PATCH 21/41] Get span from resource --- exporters/otlp/otlp_exporter.h | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/exporters/otlp/otlp_exporter.h b/exporters/otlp/otlp_exporter.h index c23bdd446c..73238b36f5 100644 --- a/exporters/otlp/otlp_exporter.h +++ b/exporters/otlp/otlp_exporter.h @@ -37,20 +37,21 @@ class OtlpExporter final : public sdktrace::SpanExporter opentelemetry::proto::collector::trace::v1::TraceService::StubInterface> trace_service_stub; - opentelemetry::proto::trace::v1::ResourceSpans resource_spans; + opentelemetry::proto::trace::v1::ResourceSpans resource_span = request.add_resource_spans(); - // trace_service_stub->Export(&context, request, &response); for (auto &recordable : spans) { auto rec = std::unique_ptr( static_cast(recordable.release())); - //std::cout << "Name: " << rec->span().name() << std::endl; + //std::cout << "Name: " << rec->span().name() << std::endl; - //request.add_resource_spans(); - //trace_service_stub->Export(&context, request, &response); + opentelemetry::proto::trace::v1::Span span = resource_spans.add_spans(); + span->CopyFrom(rec->span()); } + //trace_service_stub->Export(&context, request, &response); + return sdktrace::ExportResult::kSuccess; } From e598dabfd7e7d2a00b63afb235063b435a7cae83 Mon Sep 17 00:00:00 2001 From: Nadia Ciobanu Date: Fri, 26 Jun 2020 23:50:21 +0000 Subject: [PATCH 22/41] Export to collector --- exporters/otlp/otlp_exporter.h | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/exporters/otlp/otlp_exporter.h b/exporters/otlp/otlp_exporter.h index 73238b36f5..8611c4de1f 100644 --- a/exporters/otlp/otlp_exporter.h +++ b/exporters/otlp/otlp_exporter.h @@ -10,6 +10,8 @@ #include "recordable.h" +#include + #include namespace trace = opentelemetry::trace; @@ -36,21 +38,28 @@ class OtlpExporter final : public sdktrace::SpanExporter std::unique_ptr< opentelemetry::proto::collector::trace::v1::TraceService::StubInterface> trace_service_stub; + + const std::string address = "localhost:50051"; + auto channel = grpc::CreateChannel( + address, grpc::InsecureChannelCredentials()); + + trace_service_stub = \ + opentelemetry::proto::collector::trace::v1::TraceService::NewStub(channel); - opentelemetry::proto::trace::v1::ResourceSpans resource_span = request.add_resource_spans(); + opentelemetry::proto::trace::v1::ResourceSpans* resource_span = request.add_resource_spans(); for (auto &recordable : spans) { + //std::cout << "Name: " << rec->span().name() << std::endl; + auto rec = std::unique_ptr( static_cast(recordable.release())); - //std::cout << "Name: " << rec->span().name() << std::endl; - - opentelemetry::proto::trace::v1::Span span = resource_spans.add_spans(); + opentelemetry::proto::trace::v1::Span* span = resource_span->add_spans(); span->CopyFrom(rec->span()); } - //trace_service_stub->Export(&context, request, &response); + trace_service_stub->Export(&context, request, &response); return sdktrace::ExportResult::kSuccess; } From 5b44b1bd576fe7417c44fb5787236af2fe7afc20 Mon Sep 17 00:00:00 2001 From: Nadia Ciobanu Date: Tue, 30 Jun 2020 00:07:51 +0000 Subject: [PATCH 23/41] Change port for collector --- exporters/otlp/otlp_exporter.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exporters/otlp/otlp_exporter.h b/exporters/otlp/otlp_exporter.h index 8611c4de1f..589123b006 100644 --- a/exporters/otlp/otlp_exporter.h +++ b/exporters/otlp/otlp_exporter.h @@ -39,7 +39,7 @@ class OtlpExporter final : public sdktrace::SpanExporter opentelemetry::proto::collector::trace::v1::TraceService::StubInterface> trace_service_stub; - const std::string address = "localhost:50051"; + const std::string address = "localhost:55678"; auto channel = grpc::CreateChannel( address, grpc::InsecureChannelCredentials()); From 6ff134afb72c25c3b655c31685259eac0ac37258 Mon Sep 17 00:00:00 2001 From: Nadia Ciobanu Date: Tue, 30 Jun 2020 16:26:17 +0000 Subject: [PATCH 24/41] Set IDs in exporter --- exporters/otlp/BUILD | 1 + exporters/otlp/otlp_exporter.h | 52 ++++++++++++++++++++++++++-------- 2 files changed, 41 insertions(+), 12 deletions(-) diff --git a/exporters/otlp/BUILD b/exporters/otlp/BUILD index c01db55fbb..2b550cc2cb 100644 --- a/exporters/otlp/BUILD +++ b/exporters/otlp/BUILD @@ -25,6 +25,7 @@ cc_library( include_prefix = "exporters/otlp", deps = [ "//sdk/src/trace", + "//sdk/src/common:random", "@com_github_opentelemetry_proto//:trace_proto_cc", ], ) diff --git a/exporters/otlp/otlp_exporter.h b/exporters/otlp/otlp_exporter.h index 589123b006..dd166dc7a6 100644 --- a/exporters/otlp/otlp_exporter.h +++ b/exporters/otlp/otlp_exporter.h @@ -2,6 +2,7 @@ #include "opentelemetry/sdk/trace/exporter.h" #include "opentelemetry/sdk/trace/span_data.h" +#include "src/common/random.h" #include "opentelemetry/proto/collector/trace/v1/trace_service.pb.h" #include "opentelemetry/proto/collector/trace/v1/trace_service.grpc.pb.h" @@ -18,12 +19,28 @@ namespace trace = opentelemetry::trace; namespace nostd = opentelemetry::nostd; namespace sdktrace = opentelemetry::sdk::trace; namespace otlpexporter = opentelemetry::exporter::otlp; +namespace protocollector = opentelemetry::proto::collector::trace::v1; +namespace prototrace = opentelemetry::proto::trace::v1; + +trace::TraceId GenerateRandomTraceId() +{ + uint8_t trace_id_buf[trace::TraceId::kSize]; + opentelemetry::sdk::common::Random::GenerateRandomBuffer(trace_id_buf); + return trace::TraceId(trace_id_buf); +} + +trace::SpanId GenerateRandomSpanId() +{ + uint8_t span_id_buf[trace::SpanId::kSize]; + opentelemetry::sdk::common::Random::GenerateRandomBuffer(span_id_buf); + return trace::SpanId(span_id_buf); +} class OtlpExporter final : public sdktrace::SpanExporter { std::unique_ptr MakeRecordable() noexcept { - return std::unique_ptr(new opentelemetry::exporter::otlp::Recordable); + return std::unique_ptr(new otlpexporter::Recordable); } sdktrace::ExportResult Export( @@ -32,36 +49,47 @@ class OtlpExporter final : public sdktrace::SpanExporter std::cout << "Exporting" << std::endl; grpc::ClientContext context; - opentelemetry::proto::collector::trace::v1::ExportTraceServiceRequest request; - opentelemetry::proto::collector::trace::v1::ExportTraceServiceResponse response; + protocollector::ExportTraceServiceRequest request; + protocollector::ExportTraceServiceResponse response; std::unique_ptr< - opentelemetry::proto::collector::trace::v1::TraceService::StubInterface> + protocollector::TraceService::StubInterface> trace_service_stub; const std::string address = "localhost:55678"; auto channel = grpc::CreateChannel( address, grpc::InsecureChannelCredentials()); - trace_service_stub = \ - opentelemetry::proto::collector::trace::v1::TraceService::NewStub(channel); - - opentelemetry::proto::trace::v1::ResourceSpans* resource_span = request.add_resource_spans(); + trace_service_stub = protocollector::TraceService::NewStub(channel); + prototrace::ResourceSpans* resource_span = request.add_resource_spans(); for (auto &recordable : spans) { - //std::cout << "Name: " << rec->span().name() << std::endl; auto rec = std::unique_ptr( static_cast(recordable.release())); - opentelemetry::proto::trace::v1::Span* span = resource_span->add_spans(); + std::cout << "Name: " << rec->span().name() << std::endl; + // std::cout << "Start time: " << rec->span().start_time_unixnano() << std::endl; + // std::cout << "End time: " << rec->span().end_time_unixnano() << std::endl; + + // Temporarily set IDs here until Span is updated to do it + rec->SetIds(GenerateRandomTraceId(), GenerateRandomSpanId(), GenerateRandomSpanId()); + + // std::cout << "Trace ID: " << rec->span().trace_id() << std::endl; + // std::cout << "Span ID: " << rec->span().span_id() << std::endl; + // std::cout << "Parent span ID: " << rec->span().parent_span_id() << std::endl; + + prototrace::Span* span = resource_span->add_spans(); span->CopyFrom(rec->span()); } - trace_service_stub->Export(&context, request, &response); + grpc::Status status = trace_service_stub->Export(&context, request, &response); - return sdktrace::ExportResult::kSuccess; + if(status.ok()){ + return sdktrace::ExportResult::kSuccess; + } + return sdktrace::ExportResult::kFailure; } void Shutdown(std::chrono::microseconds timeout = std::chrono::microseconds(0)) noexcept {} From 0468abe53cc0881da6d65bbf3a9b1eaed7012b69 Mon Sep 17 00:00:00 2001 From: Nadia Ciobanu Date: Tue, 30 Jun 2020 18:51:10 +0000 Subject: [PATCH 25/41] Export traces to GCP! --- exporters/otlp/otlp_exporter.h | 7 +- exporters/otlp/recordable.cc | 6 +- .../proto/common/v1/common.proto | 66 ++++++++++++------- .../proto/resource/v1/resource.proto | 2 +- .../opentelemetry/proto/trace/v1/trace.proto | 32 +++++---- 5 files changed, 75 insertions(+), 38 deletions(-) diff --git a/exporters/otlp/otlp_exporter.h b/exporters/otlp/otlp_exporter.h index dd166dc7a6..882fff67ab 100644 --- a/exporters/otlp/otlp_exporter.h +++ b/exporters/otlp/otlp_exporter.h @@ -63,6 +63,7 @@ class OtlpExporter final : public sdktrace::SpanExporter trace_service_stub = protocollector::TraceService::NewStub(channel); prototrace::ResourceSpans* resource_span = request.add_resource_spans(); + prototrace::InstrumentationLibrarySpans* instrumentation_lib = resource_span->add_instrumentation_library_spans(); for (auto &recordable : spans) { @@ -80,15 +81,19 @@ class OtlpExporter final : public sdktrace::SpanExporter // std::cout << "Span ID: " << rec->span().span_id() << std::endl; // std::cout << "Parent span ID: " << rec->span().parent_span_id() << std::endl; - prototrace::Span* span = resource_span->add_spans(); + prototrace::Span* span = instrumentation_lib->add_spans(); + span->CopyFrom(rec->span()); } grpc::Status status = trace_service_stub->Export(&context, request, &response); if(status.ok()){ + std::cout << "Status OK" << std::endl; return sdktrace::ExportResult::kSuccess; } + std::cout << "ERROR " << status.error_code() << ": " << status.error_message() + << std::endl; return sdktrace::ExportResult::kFailure; } diff --git a/exporters/otlp/recordable.cc b/exporters/otlp/recordable.cc index 4af62db8c6..f3b44246ac 100644 --- a/exporters/otlp/recordable.cc +++ b/exporters/otlp/recordable.cc @@ -42,14 +42,14 @@ void Recordable::SetStartTime(opentelemetry::core::SystemTimestamp start_time) n { std::chrono::nanoseconds nano_time = start_time.time_since_epoch(); uint64_t nano_unix_time = nano_time.count(); - span_.set_start_time_unixnano(nano_unix_time); + span_.set_start_time_unix_nano(nano_unix_time); } void Recordable::SetDuration(std::chrono::nanoseconds duration) noexcept { uint64_t unix_duration = duration.count(); - uint64_t unix_end_time = span_.start_time_unixnano() + unix_duration; - span_.set_end_time_unixnano(unix_end_time); + uint64_t unix_end_time = span_.start_time_unix_nano() + unix_duration; + span_.set_end_time_unix_nano(unix_end_time); } } // namespace otlp } // namespace exporter diff --git a/third_party/opentelemetry-proto/opentelemetry/proto/common/v1/common.proto b/third_party/opentelemetry-proto/opentelemetry/proto/common/v1/common.proto index b3b1852459..dc67e43fb6 100644 --- a/third_party/opentelemetry-proto/opentelemetry/proto/common/v1/common.proto +++ b/third_party/opentelemetry-proto/opentelemetry/proto/common/v1/common.proto @@ -21,35 +21,57 @@ option java_package = "io.opentelemetry.proto.common.v1"; option java_outer_classname = "CommonProto"; option go_package = "github.com/open-telemetry/opentelemetry-proto/gen/go/common/v1"; -// AttributeKeyValue is a key-value pair that is used to store Span attributes, Link -// attributes, etc. -message AttributeKeyValue { - // ValueType is the enumeration of possible types that value can have. - enum ValueType { - STRING = 0; - INT = 1; - DOUBLE = 2; - BOOL = 3; - }; - - // key part of the key-value pair. - string key = 1; +// AnyValue is used to represent any type of attribute value. AnyValue may contain a +// primitive value such as a string or integer or it may contain an arbitrary nested +// object containing arrays, key-value lists and primitives. +message AnyValue { + // The value is one of the listed fields. It is valid for all values to be unspecified + // in which case this AnyValue is considered to be "null". + oneof value { + string string_value = 1; + bool bool_value = 2; + int64 int_value = 3; + double double_value = 4; + ArrayValue array_value = 5; + KeyValueList kvlist_value = 6; + } +} - // type of the value. - ValueType type = 2; +// ArrayValue is a list of AnyValue messages. We need ArrayValue as a message +// since oneof in AnyValue does not allow repeated fields. +message ArrayValue { + // Array of values. The array may be empty (contain 0 elements). + repeated AnyValue values = 1; +} - // Only one of the following fields is supposed to contain data (determined by `type` field). - // This is deliberately not using Protobuf `oneof` for performance reasons (verified by benchmarks). +// KeyValueList is a list of KeyValue messages. We need KeyValueList as a message +// since `oneof` in AnyValue does not allow repeated fields. Everywhere else where we need +// a list of KeyValue messages (e.g. in Span) we use `repeated KeyValue` directly to +// avoid unnecessary extra wrapping (which slows down the protocol). The 2 approaches +// are semantically equivalent. +message KeyValueList { + // A collection of key/value pairs of key-value pairs. The list may be empty (may + // contain 0 elements). + repeated KeyValue values = 1; +} - string string_value = 3; - int64 int_value = 4; - double double_value = 5; - bool bool_value = 6; +// KeyValue is a key-value pair that is used to store Span attributes, Link +// attributes, etc. +message KeyValue { + string key = 1; + AnyValue value = 2; } // StringKeyValue is a pair of key/value strings. This is the simpler (and faster) version -// of AttributeKeyValue that only supports string values. +// of KeyValue that only supports string values. message StringKeyValue { string key = 1; string value = 2; } + +// InstrumentationLibrary is a message representing the instrumentation library information +// such as the fully qualified name and version. +message InstrumentationLibrary { + string name = 1; + string version = 2; +} diff --git a/third_party/opentelemetry-proto/opentelemetry/proto/resource/v1/resource.proto b/third_party/opentelemetry-proto/opentelemetry/proto/resource/v1/resource.proto index a9e1711af4..fa5d97c6f8 100644 --- a/third_party/opentelemetry-proto/opentelemetry/proto/resource/v1/resource.proto +++ b/third_party/opentelemetry-proto/opentelemetry/proto/resource/v1/resource.proto @@ -26,7 +26,7 @@ option go_package = "github.com/open-telemetry/opentelemetry-proto/gen/go/resour // Resource information. message Resource { // Set of labels that describe the resource. - repeated opentelemetry.proto.common.v1.AttributeKeyValue attributes = 1; + repeated opentelemetry.proto.common.v1.KeyValue attributes = 1; // dropped_attributes_count is the number of dropped attributes. If the value is 0, then // no attributes were dropped. diff --git a/third_party/opentelemetry-proto/opentelemetry/proto/trace/v1/trace.proto b/third_party/opentelemetry-proto/opentelemetry/proto/trace/v1/trace.proto index 7f0e4a75c0..5a2350fdee 100644 --- a/third_party/opentelemetry-proto/opentelemetry/proto/trace/v1/trace.proto +++ b/third_party/opentelemetry-proto/opentelemetry/proto/trace/v1/trace.proto @@ -24,13 +24,23 @@ option java_package = "io.opentelemetry.proto.trace.v1"; option java_outer_classname = "TraceProto"; option go_package = "github.com/open-telemetry/opentelemetry-proto/gen/go/trace/v1"; -// A collection of spans from a Resource. +// A collection of InstrumentationLibrarySpans from a Resource. message ResourceSpans { // The resource for the spans in this message. // If this field is not set then no resource info is known. opentelemetry.proto.resource.v1.Resource resource = 1; - // A list of Spans that originate from a resource. + // A list of InstrumentationLibrarySpans that originate from a resource. + repeated InstrumentationLibrarySpans instrumentation_library_spans = 2; +} + +// A collection of Spans produced by an InstrumentationLibrary. +message InstrumentationLibrarySpans { + // The instrumentation library information for the spans in this message. + // If this field is not set then no library info is known. + opentelemetry.proto.common.v1.InstrumentationLibrary instrumentation_library = 1; + + // A list of Spans that originate from an instrumentation library. repeated Span spans = 2; } @@ -123,21 +133,21 @@ message Span { // and `SERVER` (callee) to identify queueing latency associated with the span. SpanKind kind = 6; - // start_time_unixnano is the start time of the span. On the client side, this is the time + // start_time_unix_nano is the start time of the span. On the client side, this is the time // kept by the local machine where the span execution starts. On the server side, this // is the time when the server's application handler starts running. // Value is UNIX Epoch time in nanoseconds since 00:00:00 UTC on 1 January 1970. // // This field is semantically required and it is expected that end_time >= start_time. - fixed64 start_time_unixnano = 7; + fixed64 start_time_unix_nano = 7; - // end_time_unixnano is the end time of the span. On the client side, this is the time + // end_time_unix_nano is the end time of the span. On the client side, this is the time // kept by the local machine where the span execution ends. On the server side, this // is the time when the server application handler stops running. // Value is UNIX Epoch time in nanoseconds since 00:00:00 UTC on 1 January 1970. // // This field is semantically required and it is expected that end_time >= start_time. - fixed64 end_time_unixnano = 8; + fixed64 end_time_unix_nano = 8; // attributes is a collection of key/value pairs. The value can be a string, // an integer, a double or the Boolean values `true` or `false`. Note, global attributes @@ -147,7 +157,7 @@ message Span { // "/http/server_latency": 300 // "abc.com/myattribute": true // "abc.com/score": 10.239 - repeated opentelemetry.proto.common.v1.AttributeKeyValue attributes = 9; + repeated opentelemetry.proto.common.v1.KeyValue attributes = 9; // dropped_attributes_count is the number of attributes that were discarded. Attributes // can be discarded because their keys are too long or because there are too many @@ -157,15 +167,15 @@ message Span { // Event is a time-stamped annotation of the span, consisting of user-supplied // text description and key-value pairs. message Event { - // time_unixnano is the time the event occurred. - fixed64 time_unixnano = 1; + // time_unix_nano is the time the event occurred. + fixed64 time_unix_nano = 1; // name of the event. // This field is semantically required to be set to non-empty string. string name = 2; // attributes is a collection of attribute key/value pairs on the event. - repeated opentelemetry.proto.common.v1.AttributeKeyValue attributes = 3; + repeated opentelemetry.proto.common.v1.KeyValue attributes = 3; // dropped_attributes_count is the number of dropped attributes. If the value is 0, // then no attributes were dropped. @@ -195,7 +205,7 @@ message Span { string trace_state = 3; // attributes is a collection of attribute key/value pairs on the link. - repeated opentelemetry.proto.common.v1.AttributeKeyValue attributes = 4; + repeated opentelemetry.proto.common.v1.KeyValue attributes = 4; // dropped_attributes_count is the number of dropped attributes. If the value is 0, // then no attributes were dropped. From 4a5be17583962b14a6d47eabcc4a7bfa42cdac0e Mon Sep 17 00:00:00 2001 From: Nadia Ciobanu Date: Tue, 30 Jun 2020 19:10:12 +0000 Subject: [PATCH 26/41] Add otlp_exporter.cc file --- exporters/otlp/BUILD | 2 +- exporters/otlp/otlp_exporter.cc | 77 +++++++++++++++++++++++++++++++ exporters/otlp/otlp_exporter.h | 81 ++------------------------------- 3 files changed, 83 insertions(+), 77 deletions(-) create mode 100644 exporters/otlp/otlp_exporter.cc diff --git a/exporters/otlp/BUILD b/exporters/otlp/BUILD index 2b550cc2cb..7b68da6a9e 100644 --- a/exporters/otlp/BUILD +++ b/exporters/otlp/BUILD @@ -47,8 +47,8 @@ cc_binary( name = "otlp_exporter", srcs = [ "main.cc", - #"stdout_exporter.h", 'otlp_exporter.h', + 'otlp_exporter.cc', ], deps = [ ":foo_library", diff --git a/exporters/otlp/otlp_exporter.cc b/exporters/otlp/otlp_exporter.cc new file mode 100644 index 0000000000..3dde703733 --- /dev/null +++ b/exporters/otlp/otlp_exporter.cc @@ -0,0 +1,77 @@ +#include "otlp_exporter.h" + +namespace otlpexporter = opentelemetry::exporter::otlp; +namespace protocollector = opentelemetry::proto::collector::trace::v1; +namespace prototrace = opentelemetry::proto::trace::v1; + +trace::TraceId GenerateRandomTraceId() +{ + uint8_t trace_id_buf[trace::TraceId::kSize]; + opentelemetry::sdk::common::Random::GenerateRandomBuffer(trace_id_buf); + return trace::TraceId(trace_id_buf); +} + +trace::SpanId GenerateRandomSpanId() +{ + uint8_t span_id_buf[trace::SpanId::kSize]; + opentelemetry::sdk::common::Random::GenerateRandomBuffer(span_id_buf); + return trace::SpanId(span_id_buf); +} + + +std::unique_ptr OtlpExporter::MakeRecordable() noexcept +{ +return std::unique_ptr(new otlpexporter::Recordable); +} + + +sdktrace::ExportResult OtlpExporter::Export( + const nostd::span> &spans) noexcept +{ + std::cout << "Exporting" << std::endl; + grpc::ClientContext context; + + protocollector::ExportTraceServiceRequest request; + protocollector::ExportTraceServiceResponse response; + + std::unique_ptr trace_service_stub; + + const std::string address = "localhost:55678"; + auto channel = grpc::CreateChannel(address, grpc::InsecureChannelCredentials()); + + trace_service_stub = protocollector::TraceService::NewStub(channel); + + prototrace::ResourceSpans* resource_span = request.add_resource_spans(); + prototrace::InstrumentationLibrarySpans* instrumentation_lib = resource_span->add_instrumentation_library_spans(); + + for (auto &recordable : spans) { + + auto rec = std::unique_ptr( + static_cast(recordable.release())); + + std::cout << "Name: " << rec->span().name() << std::endl; + // std::cout << "Start time: " << rec->span().start_time_unixnano() << std::endl; + // std::cout << "End time: " << rec->span().end_time_unixnano() << std::endl; + + // Temporarily set IDs here until Span is updated to do it + rec->SetIds(GenerateRandomTraceId(), GenerateRandomSpanId(), GenerateRandomSpanId()); + + // std::cout << "Trace ID: " << rec->span().trace_id() << std::endl; + // std::cout << "Span ID: " << rec->span().span_id() << std::endl; + // std::cout << "Parent span ID: " << rec->span().parent_span_id() << std::endl; + + prototrace::Span* span = instrumentation_lib->add_spans(); + + span->CopyFrom(rec->span()); + } + + grpc::Status status = trace_service_stub->Export(&context, request, &response); + + if(status.ok()){ + std::cout << "Status OK" << std::endl; + return sdktrace::ExportResult::kSuccess; + } + std::cout << "ERROR " << status.error_code() << ": " << status.error_message() + << std::endl; + return sdktrace::ExportResult::kFailure; +} \ No newline at end of file diff --git a/exporters/otlp/otlp_exporter.h b/exporters/otlp/otlp_exporter.h index 882fff67ab..289643c1f8 100644 --- a/exporters/otlp/otlp_exporter.h +++ b/exporters/otlp/otlp_exporter.h @@ -18,84 +18,13 @@ namespace trace = opentelemetry::trace; namespace nostd = opentelemetry::nostd; namespace sdktrace = opentelemetry::sdk::trace; -namespace otlpexporter = opentelemetry::exporter::otlp; -namespace protocollector = opentelemetry::proto::collector::trace::v1; -namespace prototrace = opentelemetry::proto::trace::v1; -trace::TraceId GenerateRandomTraceId() +class OtlpExporter final : public opentelemetry::sdk::trace::SpanExporter { - uint8_t trace_id_buf[trace::TraceId::kSize]; - opentelemetry::sdk::common::Random::GenerateRandomBuffer(trace_id_buf); - return trace::TraceId(trace_id_buf); -} + std::unique_ptr MakeRecordable() noexcept override; -trace::SpanId GenerateRandomSpanId() -{ - uint8_t span_id_buf[trace::SpanId::kSize]; - opentelemetry::sdk::common::Random::GenerateRandomBuffer(span_id_buf); - return trace::SpanId(span_id_buf); -} - -class OtlpExporter final : public sdktrace::SpanExporter -{ - std::unique_ptr MakeRecordable() noexcept - { - return std::unique_ptr(new otlpexporter::Recordable); - } - - sdktrace::ExportResult Export( - const nostd::span> &spans) noexcept - { - std::cout << "Exporting" << std::endl; - grpc::ClientContext context; - - protocollector::ExportTraceServiceRequest request; - protocollector::ExportTraceServiceResponse response; - - std::unique_ptr< - protocollector::TraceService::StubInterface> - trace_service_stub; - - const std::string address = "localhost:55678"; - auto channel = grpc::CreateChannel( - address, grpc::InsecureChannelCredentials()); - - trace_service_stub = protocollector::TraceService::NewStub(channel); - - prototrace::ResourceSpans* resource_span = request.add_resource_spans(); - prototrace::InstrumentationLibrarySpans* instrumentation_lib = resource_span->add_instrumentation_library_spans(); - - for (auto &recordable : spans) { - - auto rec = std::unique_ptr( - static_cast(recordable.release())); - - std::cout << "Name: " << rec->span().name() << std::endl; - // std::cout << "Start time: " << rec->span().start_time_unixnano() << std::endl; - // std::cout << "End time: " << rec->span().end_time_unixnano() << std::endl; - - // Temporarily set IDs here until Span is updated to do it - rec->SetIds(GenerateRandomTraceId(), GenerateRandomSpanId(), GenerateRandomSpanId()); - - // std::cout << "Trace ID: " << rec->span().trace_id() << std::endl; - // std::cout << "Span ID: " << rec->span().span_id() << std::endl; - // std::cout << "Parent span ID: " << rec->span().parent_span_id() << std::endl; - - prototrace::Span* span = instrumentation_lib->add_spans(); - - span->CopyFrom(rec->span()); - } - - grpc::Status status = trace_service_stub->Export(&context, request, &response); - - if(status.ok()){ - std::cout << "Status OK" << std::endl; - return sdktrace::ExportResult::kSuccess; - } - std::cout << "ERROR " << status.error_code() << ": " << status.error_message() - << std::endl; - return sdktrace::ExportResult::kFailure; - } + opentelemetry::sdk::trace::ExportResult Export( + const opentelemetry::nostd::span> &spans) noexcept override; - void Shutdown(std::chrono::microseconds timeout = std::chrono::microseconds(0)) noexcept {} + void Shutdown(std::chrono::microseconds timeout = std::chrono::microseconds(0)) noexcept override {}; }; From a6b804d62f409b1d0b3fdaf387ec59b6956728d3 Mon Sep 17 00:00:00 2001 From: Nadia Ciobanu Date: Tue, 30 Jun 2020 21:10:11 +0000 Subject: [PATCH 27/41] Add test and simplify namespaces --- exporters/otlp/BUILD | 9 +++++ exporters/otlp/main.cc | 7 +++- exporters/otlp/otlp_exporter.cc | 49 ++++++++++++++-------------- exporters/otlp/otlp_exporter.h | 11 +++++-- exporters/otlp/otlp_exporter_test.cc | 19 +++++++++++ 5 files changed, 66 insertions(+), 29 deletions(-) create mode 100644 exporters/otlp/otlp_exporter_test.cc diff --git a/exporters/otlp/BUILD b/exporters/otlp/BUILD index 7b68da6a9e..6f697995b1 100644 --- a/exporters/otlp/BUILD +++ b/exporters/otlp/BUILD @@ -72,3 +72,12 @@ cc_test( "@com_google_googletest//:gtest_main", ], ) + +cc_test( + name = "otlp_exporter_test", + srcs = ["otlp_exporter_test.cc"], + deps = [ + ":otlp_exporter", + "@com_google_googletest//:gtest_main", + ], +) diff --git a/exporters/otlp/main.cc b/exporters/otlp/main.cc index 0f489d87c2..a3600e2cdc 100644 --- a/exporters/otlp/main.cc +++ b/exporters/otlp/main.cc @@ -8,12 +8,17 @@ #include "foo_library/foo_library.h" +namespace trace = opentelemetry::trace; +namespace nostd = opentelemetry::nostd; +namespace sdktrace = opentelemetry::sdk::trace; +namespace exporter = opentelemetry::exporter; + namespace { void initTracer() { //auto exporter = std::unique_ptr(new StdoutExporter); - auto exporter = std::unique_ptr(new OtlpExporter); + auto exporter = std::unique_ptr(new exporter::otlp::OtlpExporter); auto processor = std::shared_ptr( new sdktrace::SimpleSpanProcessor(std::move(exporter))); auto provider = nostd::shared_ptr(new sdktrace::TracerProvider(processor)); diff --git a/exporters/otlp/otlp_exporter.cc b/exporters/otlp/otlp_exporter.cc index 3dde703733..0909ad4edb 100644 --- a/exporters/otlp/otlp_exporter.cc +++ b/exporters/otlp/otlp_exporter.cc @@ -1,8 +1,10 @@ #include "otlp_exporter.h" -namespace otlpexporter = opentelemetry::exporter::otlp; -namespace protocollector = opentelemetry::proto::collector::trace::v1; -namespace prototrace = opentelemetry::proto::trace::v1; +OPENTELEMETRY_BEGIN_NAMESPACE +namespace exporter +{ +namespace otlp +{ trace::TraceId GenerateRandomTraceId() { @@ -19,48 +21,42 @@ trace::SpanId GenerateRandomSpanId() } -std::unique_ptr OtlpExporter::MakeRecordable() noexcept +std::unique_ptr OtlpExporter::MakeRecordable() noexcept { -return std::unique_ptr(new otlpexporter::Recordable); +return std::unique_ptr(new Recordable); } -sdktrace::ExportResult OtlpExporter::Export( - const nostd::span> &spans) noexcept +sdk::trace::ExportResult OtlpExporter::Export( + const nostd::span> &spans) noexcept { std::cout << "Exporting" << std::endl; grpc::ClientContext context; - protocollector::ExportTraceServiceRequest request; - protocollector::ExportTraceServiceResponse response; + proto::collector::trace::v1::ExportTraceServiceRequest request; + proto::collector::trace::v1::ExportTraceServiceResponse response; - std::unique_ptr trace_service_stub; + std::unique_ptr trace_service_stub; const std::string address = "localhost:55678"; auto channel = grpc::CreateChannel(address, grpc::InsecureChannelCredentials()); - trace_service_stub = protocollector::TraceService::NewStub(channel); + trace_service_stub = proto::collector::trace::v1::TraceService::NewStub(channel); - prototrace::ResourceSpans* resource_span = request.add_resource_spans(); - prototrace::InstrumentationLibrarySpans* instrumentation_lib = resource_span->add_instrumentation_library_spans(); + proto::trace::v1::ResourceSpans* resource_span = request.add_resource_spans(); + proto::trace::v1::InstrumentationLibrarySpans* instrumentation_lib = resource_span->add_instrumentation_library_spans(); for (auto &recordable : spans) { - auto rec = std::unique_ptr( - static_cast(recordable.release())); + auto rec = std::unique_ptr( + static_cast(recordable.release())); std::cout << "Name: " << rec->span().name() << std::endl; - // std::cout << "Start time: " << rec->span().start_time_unixnano() << std::endl; - // std::cout << "End time: " << rec->span().end_time_unixnano() << std::endl; // Temporarily set IDs here until Span is updated to do it rec->SetIds(GenerateRandomTraceId(), GenerateRandomSpanId(), GenerateRandomSpanId()); - // std::cout << "Trace ID: " << rec->span().trace_id() << std::endl; - // std::cout << "Span ID: " << rec->span().span_id() << std::endl; - // std::cout << "Parent span ID: " << rec->span().parent_span_id() << std::endl; - - prototrace::Span* span = instrumentation_lib->add_spans(); + proto::trace::v1::Span* span = instrumentation_lib->add_spans(); span->CopyFrom(rec->span()); } @@ -69,9 +65,12 @@ sdktrace::ExportResult OtlpExporter::Export( if(status.ok()){ std::cout << "Status OK" << std::endl; - return sdktrace::ExportResult::kSuccess; + return sdk::trace::ExportResult::kSuccess; } std::cout << "ERROR " << status.error_code() << ": " << status.error_message() << std::endl; - return sdktrace::ExportResult::kFailure; -} \ No newline at end of file + return sdk::trace::ExportResult::kFailure; +} +} // namespace otlp +} // namespace exporter +OPENTELEMETRY_END_NAMESPACE diff --git a/exporters/otlp/otlp_exporter.h b/exporters/otlp/otlp_exporter.h index 289643c1f8..9f9240d19d 100644 --- a/exporters/otlp/otlp_exporter.h +++ b/exporters/otlp/otlp_exporter.h @@ -15,10 +15,12 @@ #include -namespace trace = opentelemetry::trace; -namespace nostd = opentelemetry::nostd; -namespace sdktrace = opentelemetry::sdk::trace; +OPENTELEMETRY_BEGIN_NAMESPACE +namespace exporter +{ +namespace otlp +{ class OtlpExporter final : public opentelemetry::sdk::trace::SpanExporter { std::unique_ptr MakeRecordable() noexcept override; @@ -28,3 +30,6 @@ class OtlpExporter final : public opentelemetry::sdk::trace::SpanExporter void Shutdown(std::chrono::microseconds timeout = std::chrono::microseconds(0)) noexcept override {}; }; +} // namespace otlp +} // namespace exporter +OPENTELEMETRY_END_NAMESPACE diff --git a/exporters/otlp/otlp_exporter_test.cc b/exporters/otlp/otlp_exporter_test.cc new file mode 100644 index 0000000000..c3ffc12594 --- /dev/null +++ b/exporters/otlp/otlp_exporter_test.cc @@ -0,0 +1,19 @@ +#include "otlp_exporter.h" + +#include + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace exporter +{ +namespace otlp +{ +TEST(OtlpExporter, MakeRecordable) +{ +} + +TEST(OtlpExporter, Export) +{ +} +} // namespace otlp +} // namespace exporter +OPENTELEMETRY_END_NAMESPACE \ No newline at end of file From 8a580f672e3590ad164651ac6c964fe385061d0c Mon Sep 17 00:00:00 2001 From: Nadia Ciobanu Date: Wed, 1 Jul 2020 00:34:19 +0000 Subject: [PATCH 28/41] Extend test, delete extra files --- exporters/otlp/BUILD | 15 ---------- exporters/otlp/foo_library/foo_library.cc | 33 --------------------- exporters/otlp/foo_library/foo_library.h | 3 -- exporters/otlp/main.cc | 36 ----------------------- exporters/otlp/otlp_exporter.h | 3 -- exporters/otlp/otlp_exporter_test.cc | 28 ++++++++++++++++-- 6 files changed, 26 insertions(+), 92 deletions(-) delete mode 100644 exporters/otlp/foo_library/foo_library.cc delete mode 100644 exporters/otlp/foo_library/foo_library.h delete mode 100644 exporters/otlp/main.cc diff --git a/exporters/otlp/BUILD b/exporters/otlp/BUILD index 6f697995b1..3b80a00380 100644 --- a/exporters/otlp/BUILD +++ b/exporters/otlp/BUILD @@ -31,27 +31,12 @@ cc_library( ) cc_library( - name = "foo_library", - srcs = [ - "foo_library/foo_library.cc", - ], - hdrs = [ - "foo_library/foo_library.h", - ], - deps = [ - "//api", - ], -) - -cc_binary( name = "otlp_exporter", srcs = [ - "main.cc", 'otlp_exporter.h', 'otlp_exporter.cc', ], deps = [ - ":foo_library", ":recordable", "//api", "//sdk/src/trace", diff --git a/exporters/otlp/foo_library/foo_library.cc b/exporters/otlp/foo_library/foo_library.cc deleted file mode 100644 index 0990b85ced..0000000000 --- a/exporters/otlp/foo_library/foo_library.cc +++ /dev/null @@ -1,33 +0,0 @@ -#include "opentelemetry/trace/provider.h" - -namespace trace = opentelemetry::trace; -namespace nostd = opentelemetry::nostd; - -namespace -{ -nostd::shared_ptr get_tracer() -{ - auto provider = trace::Provider::GetTracerProvider(); - return provider->GetTracer("foo_library"); -} - -void f1() -{ - auto span = get_tracer()->StartSpan("f1"); -} - -void f2() -{ - auto span = get_tracer()->StartSpan("f2"); - - f1(); - f1(); -} -} // namespace - -void foo_library() -{ - auto span = get_tracer()->StartSpan("library"); - - f2(); -} diff --git a/exporters/otlp/foo_library/foo_library.h b/exporters/otlp/foo_library/foo_library.h deleted file mode 100644 index 7ac75c0e50..0000000000 --- a/exporters/otlp/foo_library/foo_library.h +++ /dev/null @@ -1,3 +0,0 @@ -#pragma once - -void foo_library(); diff --git a/exporters/otlp/main.cc b/exporters/otlp/main.cc deleted file mode 100644 index a3600e2cdc..0000000000 --- a/exporters/otlp/main.cc +++ /dev/null @@ -1,36 +0,0 @@ -#include "opentelemetry/sdk/trace/simple_processor.h" -#include "opentelemetry/sdk/trace/tracer_provider.h" -#include "opentelemetry/trace/provider.h" - -// Using an exporter that simply dumps span data to stdout. -//#include "stdout_exporter.h" -#include "otlp_exporter.h" - -#include "foo_library/foo_library.h" - -namespace trace = opentelemetry::trace; -namespace nostd = opentelemetry::nostd; -namespace sdktrace = opentelemetry::sdk::trace; -namespace exporter = opentelemetry::exporter; - -namespace -{ -void initTracer() -{ - //auto exporter = std::unique_ptr(new StdoutExporter); - auto exporter = std::unique_ptr(new exporter::otlp::OtlpExporter); - auto processor = std::shared_ptr( - new sdktrace::SimpleSpanProcessor(std::move(exporter))); - auto provider = nostd::shared_ptr(new sdktrace::TracerProvider(processor)); - trace::Provider::SetTracerProvider(provider); -} -} // namespace - -int main() -{ - // Removing this line will leave OT initialized with the default noop - // tracer, thus being effectively deactivated. - initTracer(); - - foo_library(); -} diff --git a/exporters/otlp/otlp_exporter.h b/exporters/otlp/otlp_exporter.h index 9f9240d19d..ccb56d6071 100644 --- a/exporters/otlp/otlp_exporter.h +++ b/exporters/otlp/otlp_exporter.h @@ -6,16 +6,13 @@ #include "opentelemetry/proto/collector/trace/v1/trace_service.pb.h" #include "opentelemetry/proto/collector/trace/v1/trace_service.grpc.pb.h" - #include "opentelemetry/proto/trace/v1/trace.pb.h" #include "recordable.h" #include - #include - OPENTELEMETRY_BEGIN_NAMESPACE namespace exporter { diff --git a/exporters/otlp/otlp_exporter_test.cc b/exporters/otlp/otlp_exporter_test.cc index c3ffc12594..663e280120 100644 --- a/exporters/otlp/otlp_exporter_test.cc +++ b/exporters/otlp/otlp_exporter_test.cc @@ -1,4 +1,7 @@ #include "otlp_exporter.h" +#include "opentelemetry/sdk/trace/simple_processor.h" +#include "opentelemetry/sdk/trace/tracer_provider.h" +#include "opentelemetry/trace/provider.h" #include @@ -7,12 +10,33 @@ namespace exporter { namespace otlp { -TEST(OtlpExporter, MakeRecordable) +TEST(OtlpExporter, ExportInternal) { + auto exporter = std::unique_ptr(new OtlpExporter); + + auto rec = exporter->MakeRecordable(); + // Set span fields + nostd::string_view name = "TestSpan"; + rec->SetName(name); + + nostd::span> recs(&rec, 1); + auto result = exporter->Export(recs); + + EXPECT_EQ(sdk::trace::ExportResult::kSuccess, result); } -TEST(OtlpExporter, Export) +TEST(OtlpExporter, ExportExternal) { + auto exporter = std::unique_ptr(new OtlpExporter); + auto processor = std::shared_ptr( + new sdk::trace::SimpleSpanProcessor(std::move(exporter))); + auto provider = nostd::shared_ptr(new sdk::trace::TracerProvider(processor)); + trace::Provider::SetTracerProvider(provider); + + nostd::shared_ptr tracer = provider->GetTracer("test"); + + auto span = tracer->StartSpan("TestSpan"); + span->End(); } } // namespace otlp } // namespace exporter From 268d59cffa7f08fdd0152d3dc9615055b95b1c8c Mon Sep 17 00:00:00 2001 From: Nadia Ciobanu Date: Wed, 1 Jul 2020 00:54:30 +0000 Subject: [PATCH 29/41] Set IDs in test instead of exporter --- exporters/otlp/otlp_exporter.cc | 21 +-------------------- exporters/otlp/otlp_exporter_test.cc | 27 +++++++++++++++++++++++++-- 2 files changed, 26 insertions(+), 22 deletions(-) diff --git a/exporters/otlp/otlp_exporter.cc b/exporters/otlp/otlp_exporter.cc index 0909ad4edb..97ce2e9369 100644 --- a/exporters/otlp/otlp_exporter.cc +++ b/exporters/otlp/otlp_exporter.cc @@ -6,29 +6,13 @@ namespace exporter namespace otlp { -trace::TraceId GenerateRandomTraceId() -{ - uint8_t trace_id_buf[trace::TraceId::kSize]; - opentelemetry::sdk::common::Random::GenerateRandomBuffer(trace_id_buf); - return trace::TraceId(trace_id_buf); -} - -trace::SpanId GenerateRandomSpanId() -{ - uint8_t span_id_buf[trace::SpanId::kSize]; - opentelemetry::sdk::common::Random::GenerateRandomBuffer(span_id_buf); - return trace::SpanId(span_id_buf); -} - - std::unique_ptr OtlpExporter::MakeRecordable() noexcept { return std::unique_ptr(new Recordable); } - sdk::trace::ExportResult OtlpExporter::Export( - const nostd::span> &spans) noexcept + const nostd::span> &spans) noexcept { std::cout << "Exporting" << std::endl; grpc::ClientContext context; @@ -53,9 +37,6 @@ sdk::trace::ExportResult OtlpExporter::Export( std::cout << "Name: " << rec->span().name() << std::endl; - // Temporarily set IDs here until Span is updated to do it - rec->SetIds(GenerateRandomTraceId(), GenerateRandomSpanId(), GenerateRandomSpanId()); - proto::trace::v1::Span* span = instrumentation_lib->add_spans(); span->CopyFrom(rec->span()); diff --git a/exporters/otlp/otlp_exporter_test.cc b/exporters/otlp/otlp_exporter_test.cc index 663e280120..b163353762 100644 --- a/exporters/otlp/otlp_exporter_test.cc +++ b/exporters/otlp/otlp_exporter_test.cc @@ -10,14 +10,37 @@ namespace exporter { namespace otlp { + +trace::TraceId GenerateRandomTraceId() +{ + uint8_t trace_id_buf[trace::TraceId::kSize]; + opentelemetry::sdk::common::Random::GenerateRandomBuffer(trace_id_buf); + return trace::TraceId(trace_id_buf); +} + +trace::SpanId GenerateRandomSpanId() +{ + uint8_t span_id_buf[trace::SpanId::kSize]; + opentelemetry::sdk::common::Random::GenerateRandomBuffer(span_id_buf); + return trace::SpanId(span_id_buf); +} + TEST(OtlpExporter, ExportInternal) { auto exporter = std::unique_ptr(new OtlpExporter); - auto rec = exporter->MakeRecordable(); - // Set span fields + + // Name nostd::string_view name = "TestSpan"; rec->SetName(name); + // IDs + rec->SetIds(GenerateRandomTraceId(), GenerateRandomSpanId(), GenerateRandomSpanId()); + // Start time + core::SystemTimestamp start_timestamp(std::chrono::system_clock::now()); + rec->SetStartTime(start_timestamp); + // Duration + std::chrono::nanoseconds duration(10); + rec->SetDuration(duration); nostd::span> recs(&rec, 1); auto result = exporter->Export(recs); From 6b428a3ed96d7721ea6a4034c9285d6f9d86fc53 Mon Sep 17 00:00:00 2001 From: Nick Holbrook Date: Fri, 26 Jun 2020 17:33:38 -0400 Subject: [PATCH 30/41] Always Off Sampler (#125) --- .bazelversion | 2 +- .../sdk/trace/samplers/always_off.h | 41 +++++++++++++++++++ sdk/test/trace/BUILD | 11 +++++ sdk/test/trace/CMakeLists.txt | 2 +- sdk/test/trace/always_off_sampler_test.cc | 30 ++++++++++++++ 5 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 sdk/include/opentelemetry/sdk/trace/samplers/always_off.h create mode 100644 sdk/test/trace/always_off_sampler_test.cc diff --git a/.bazelversion b/.bazelversion index 944880fa15..15a2799817 100644 --- a/.bazelversion +++ b/.bazelversion @@ -1 +1 @@ -3.2.0 +3.3.0 diff --git a/sdk/include/opentelemetry/sdk/trace/samplers/always_off.h b/sdk/include/opentelemetry/sdk/trace/samplers/always_off.h new file mode 100644 index 0000000000..0547068280 --- /dev/null +++ b/sdk/include/opentelemetry/sdk/trace/samplers/always_off.h @@ -0,0 +1,41 @@ +#pragma once + +#include "opentelemetry/sdk/trace/sampler.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace trace +{ +namespace trace_api = opentelemetry::trace; +/** + * The always off sampler always returns NOT_RECORD, effectively disabling + * tracing functionality. + */ +class AlwaysOffSampler : public Sampler +{ +public: + /** + * @return Returns NOT_RECORD always + */ + SamplingResult ShouldSample( + const SpanContext * /*parent_context*/, + trace_api::TraceId /*trace_id*/, + nostd::string_view /*name*/, + trace_api::SpanKind /*span_kind*/, + const trace_api::KeyValueIterable & /*attributes*/) noexcept override + { + return { Decision::NOT_RECORD, nullptr }; + } + + /** + * @return Description MUST be AlwaysOffSampler + */ + std::string GetDescription() const noexcept override + { + return "AlwaysOffSampler"; + } +}; +} // namespace trace +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE diff --git a/sdk/test/trace/BUILD b/sdk/test/trace/BUILD index 6748cde17f..f07ee5440a 100644 --- a/sdk/test/trace/BUILD +++ b/sdk/test/trace/BUILD @@ -41,3 +41,14 @@ cc_test( "@com_google_googletest//:gtest_main", ], ) + +cc_test( + name = "always_off_sampler_test", + srcs = [ + "always_off_sampler_test.cc" + ], + deps = [ + "//sdk/src/trace", + "@com_google_googletest//:gtest_main", + ] +) diff --git a/sdk/test/trace/CMakeLists.txt b/sdk/test/trace/CMakeLists.txt index 5bca38440a..052b7e2a86 100644 --- a/sdk/test/trace/CMakeLists.txt +++ b/sdk/test/trace/CMakeLists.txt @@ -1,5 +1,5 @@ foreach(testname tracer_provider_test span_data_test simple_processor_test - tracer_test) + tracer_test always_off_sampler_test) add_executable(${testname} "${testname}.cc") target_link_libraries(${testname} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} opentelemetry_trace) diff --git a/sdk/test/trace/always_off_sampler_test.cc b/sdk/test/trace/always_off_sampler_test.cc new file mode 100644 index 0000000000..2c892fcfa7 --- /dev/null +++ b/sdk/test/trace/always_off_sampler_test.cc @@ -0,0 +1,30 @@ +#include "opentelemetry/sdk/trace/samplers/always_off.h" + +#include + +using opentelemetry::sdk::trace::AlwaysOffSampler; +using opentelemetry::sdk::trace::Decision; + +TEST(AlwaysOffSampler, ShouldSample) +{ + AlwaysOffSampler sampler; + + opentelemetry::trace::TraceId trace_id; + opentelemetry::trace::SpanKind span_kind = opentelemetry::trace::SpanKind::kInternal; + + using M = std::map; + M m1 = {{}}; + opentelemetry::trace::KeyValueIterableView view{m1}; + + auto sampling_result = sampler.ShouldSample(nullptr, trace_id, "", span_kind, view); + + ASSERT_EQ(Decision::NOT_RECORD, sampling_result.decision); + ASSERT_EQ(nullptr, sampling_result.attributes); +} + +TEST(AlwaysOffSampler, GetDescription) +{ + AlwaysOffSampler sampler; + + ASSERT_EQ("AlwaysOffSampler", sampler.GetDescription()); +} From 3d506830c101bc16a6ce89650bf01245f845fc4e Mon Sep 17 00:00:00 2001 From: Oliver Zhang <30583106+ziqizh@users.noreply.github.com> Date: Mon, 29 Jun 2020 18:50:53 -0400 Subject: [PATCH 31/41] Always on sampler (#122) --- sdk/include/opentelemetry/sdk/trace/sampler.h | 4 +- .../sdk/trace/samplers/always_on.h | 36 ++++++++++++++++ sdk/test/trace/BUILD | 19 ++++++-- sdk/test/trace/CMakeLists.txt | 2 +- sdk/test/trace/always_on_sampler_test.cc | 43 +++++++++++++++++++ 5 files changed, 98 insertions(+), 6 deletions(-) create mode 100644 sdk/include/opentelemetry/sdk/trace/samplers/always_on.h create mode 100644 sdk/test/trace/always_on_sampler_test.cc diff --git a/sdk/include/opentelemetry/sdk/trace/sampler.h b/sdk/include/opentelemetry/sdk/trace/sampler.h index 93599b18e7..27071b458c 100644 --- a/sdk/include/opentelemetry/sdk/trace/sampler.h +++ b/sdk/include/opentelemetry/sdk/trace/sampler.h @@ -17,7 +17,7 @@ namespace trace namespace trace_api = opentelemetry::trace; /** - * A sampling Decision for a Span to be created. + * A sampling Decision for a Span to be created. */ enum class Decision { @@ -31,7 +31,7 @@ enum class Decision }; /** - * The output of ShouldSample. + * The output of ShouldSample. * It contains a sampling Decision and a set of Span Attributes. */ struct SamplingResult diff --git a/sdk/include/opentelemetry/sdk/trace/samplers/always_on.h b/sdk/include/opentelemetry/sdk/trace/samplers/always_on.h new file mode 100644 index 0000000000..e50d15829f --- /dev/null +++ b/sdk/include/opentelemetry/sdk/trace/samplers/always_on.h @@ -0,0 +1,36 @@ +#pragma once + +#include "opentelemetry/sdk/trace/sampler.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace trace +{ +namespace trace_api = opentelemetry::trace; +/** + * The always on sampler is a default sampler which always return Decision::RECORD_AND_SAMPLE + */ +class AlwaysOnSampler : public Sampler +{ +public: + /** + * @return Always return Decision RECORD_AND_SAMPLE + */ + inline SamplingResult ShouldSample(const SpanContext * /*parent_context*/, + trace_api::TraceId /*trace_id*/, + nostd::string_view /*name*/, + trace_api::SpanKind /*span_kind*/, + const trace_api::KeyValueIterable & /*attributes*/) noexcept override + { + return {Decision::RECORD_AND_SAMPLE, nullptr}; + } + + /** + * @return Description MUST be AlwaysOnSampler + */ + inline std::string GetDescription() const noexcept override { return "AlwaysOnSampler"; } +}; +} // namespace trace +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE diff --git a/sdk/test/trace/BUILD b/sdk/test/trace/BUILD index f07ee5440a..37018d7eeb 100644 --- a/sdk/test/trace/BUILD +++ b/sdk/test/trace/BUILD @@ -43,12 +43,25 @@ cc_test( ) cc_test( - name = "always_off_sampler_test", + name = "always_on_sampler_test", srcs = [ - "always_off_sampler_test.cc" + "always_on_sampler_test.cc", ], deps = [ "//sdk/src/trace", "@com_google_googletest//:gtest_main", - ] + ], ) + + + +cc_test( + name = "always_off_sampler_test", + srcs = [ + "always_off_sampler_test.cc", + ], + deps = [ + "//sdk/src/trace", + "@com_google_googletest//:gtest_main", + ], +) \ No newline at end of file diff --git a/sdk/test/trace/CMakeLists.txt b/sdk/test/trace/CMakeLists.txt index 052b7e2a86..75bfd4f97a 100644 --- a/sdk/test/trace/CMakeLists.txt +++ b/sdk/test/trace/CMakeLists.txt @@ -1,5 +1,5 @@ foreach(testname tracer_provider_test span_data_test simple_processor_test - tracer_test always_off_sampler_test) + tracer_test always_off_sampler_test always_on_sampler_test) add_executable(${testname} "${testname}.cc") target_link_libraries(${testname} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} opentelemetry_trace) diff --git a/sdk/test/trace/always_on_sampler_test.cc b/sdk/test/trace/always_on_sampler_test.cc new file mode 100644 index 0000000000..a933cd0073 --- /dev/null +++ b/sdk/test/trace/always_on_sampler_test.cc @@ -0,0 +1,43 @@ +#include "opentelemetry/nostd/span.h" +#include "opentelemetry/sdk/trace/samplers/always_on.h" + +#include +#include + +using namespace opentelemetry::sdk::trace; +using namespace opentelemetry::nostd; + +TEST(AlwaysOnSampler, ShouldSample) +{ + AlwaysOnSampler sampler; + + // A buffer of trace_id with no specific meaning + constexpr uint8_t buf[] = {0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7}; + + trace_api::TraceId trace_id_invalid; + trace_api::TraceId trace_id_valid(buf); + std::map key_value_container = {{"key", 0}}; + + // Test with invalid (empty) trace id and empty parent context + auto sampling_result = sampler.ShouldSample( + nullptr, trace_id_invalid, "invalid trace id test", trace_api::SpanKind::kServer, + trace_api::KeyValueIterableView>(key_value_container)); + + ASSERT_EQ(Decision::RECORD_AND_SAMPLE, sampling_result.decision); + ASSERT_EQ(nullptr, sampling_result.attributes); + + // Test with a valid trace id and empty parent context + sampling_result = sampler.ShouldSample( + nullptr, trace_id_valid, "valid trace id test", trace_api::SpanKind::kServer, + trace_api::KeyValueIterableView>(key_value_container)); + + ASSERT_EQ(Decision::RECORD_AND_SAMPLE, sampling_result.decision); + ASSERT_EQ(nullptr, sampling_result.attributes); +} + +TEST(AlwaysOnSampler, GetDescription) +{ + AlwaysOnSampler sampler; + + ASSERT_EQ("AlwaysOnSampler", sampler.GetDescription()); +} From 28b354c4011076a6e943ef0e4270aa6b843617fe Mon Sep 17 00:00:00 2001 From: Max Golovanov Date: Mon, 29 Jun 2020 16:13:06 -0700 Subject: [PATCH 32/41] Add compare operators to nostd::string_view (#124) * Add compare operators to nostd::string_view * Added tests --- api/include/opentelemetry/nostd/string_view.h | 64 +++++++++++++++++-- api/test/nostd/string_view_test.cc | 30 +++++++++ 2 files changed, 87 insertions(+), 7 deletions(-) diff --git a/api/include/opentelemetry/nostd/string_view.h b/api/include/opentelemetry/nostd/string_view.h index 02f6b326a7..e947d23259 100644 --- a/api/include/opentelemetry/nostd/string_view.h +++ b/api/include/opentelemetry/nostd/string_view.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include #include @@ -11,6 +12,9 @@ OPENTELEMETRY_BEGIN_NAMESPACE namespace nostd { + +using Traits = std::char_traits; + /** * Back port of std::string_view to work with pre-cpp-17 compilers. * @@ -20,7 +24,9 @@ namespace nostd class string_view { public: - static constexpr std::size_t npos = static_cast(-1); + typedef std::size_t size_type; + + static constexpr size_type npos = static_cast(-1); string_view() noexcept : length_(0), data_(nullptr) {} @@ -30,7 +36,7 @@ class string_view : length_(str.length()), data_(str.c_str()) {} - string_view(const char *str, size_t len) noexcept : length_(len), data_(str) {} + string_view(const char *str, size_type len) noexcept : length_(len), data_(str) {} explicit operator std::string() const { return {data_, length_}; } @@ -38,17 +44,17 @@ class string_view bool empty() const noexcept { return length_ == 0; } - size_t length() const noexcept { return length_; } + size_type length() const noexcept { return length_; } - size_t size() const noexcept { return length_; } + size_type size() const noexcept { return length_; } const char *begin() const noexcept { return data(); } const char *end() const noexcept { return data() + length(); } - const char &operator[](std::size_t i) { return *(data() + i); } + const char &operator[](size_type i) { return *(data() + i); } - string_view substr(std::size_t pos, std::size_t n = npos) const + string_view substr(size_type pos, size_type n = npos) const { if (pos > length_) { @@ -62,11 +68,55 @@ class string_view return string_view(data_ + pos, n); } + int compare(string_view v) const noexcept + { + size_type len = std::min(size(), v.size()); + int result = Traits::compare(data(), v.data(), len); + if (result == 0) + result = size() == v.size() ? 0 : (size() < v.size() ? -1 : 1); + return result; + }; + + int compare(size_type pos1, size_type count1, string_view v) const + { + return substr(pos1, count1).compare(v); + }; + + int compare(size_type pos1, size_type count1, string_view v, size_type pos2, size_type count2) const + { + return substr(pos1, count1).compare(v.substr(pos2, count2)); + }; + + int compare(const char *s) const + { + return compare(string_view(s)); + }; + + int compare(size_type pos1, size_type count1, const char *s) const + { + return substr(pos1, count1).compare(string_view(s)); + }; + + int compare(size_type pos1, size_type count1, const char *s, size_type count2) const + { + return substr(pos1, count1).compare(string_view(s, count2)); + }; + + bool operator<(const string_view v) const noexcept + { + return compare(v) < 0; + } + + bool operator>(const string_view v) const noexcept + { + return compare(v) > 0; + } + private: // Note: uses the same binary layout as libstdc++'s std::string_view // See // https://github.com/gcc-mirror/gcc/blob/e0c554e4da7310df83bb1dcc7b8e6c4c9c5a2a4f/libstdc%2B%2B-v3/include/std/string_view#L466-L467 - size_t length_; + size_type length_; const char *data_; }; diff --git a/api/test/nostd/string_view_test.cc b/api/test/nostd/string_view_test.cc index 17c52bf6cb..074d3bee49 100644 --- a/api/test/nostd/string_view_test.cc +++ b/api/test/nostd/string_view_test.cc @@ -2,6 +2,8 @@ #include +#include "map" + using opentelemetry::nostd::string_view; TEST(StringViewTest, DefaultConstruction) @@ -72,3 +74,31 @@ TEST(StringViewTest, SubstrOutOfRange) EXPECT_DEATH({ s.substr(10); }, ""); #endif } + +TEST(StringViewTest, Compare) +{ + string_view s1 = "aaa"; + string_view s2 = "bbb"; + string_view s3 = "aaa"; + + // Equals + EXPECT_EQ(s1, s3); + EXPECT_EQ(s1, s1); + + // Less then + EXPECT_LT(s1, s2); + + // Greater then + EXPECT_GT(s2, s1); +} + +TEST(StringViewTest, MapKeyOrdering) +{ + std::map m = {{"bbb", 2}, {"aaa", 1}, {"ccc", 3}}; + size_t i = 1; + for (const auto &kv : m) + { + EXPECT_EQ(kv.second, i); + i++; + } +} From b2f7b13eb78061094c5aa27197bd465955c801e2 Mon Sep 17 00:00:00 2001 From: Janet Vu Date: Tue, 30 Jun 2020 10:56:45 -0400 Subject: [PATCH 33/41] Start zPages README (#131) --- ext/src/zpages/README.md | 46 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 ext/src/zpages/README.md diff --git a/ext/src/zpages/README.md b/ext/src/zpages/README.md new file mode 100644 index 0000000000..f1bad98516 --- /dev/null +++ b/ext/src/zpages/README.md @@ -0,0 +1,46 @@ +# zPages +> Last updated 6/26/20 + +# Table of Contents +- [Summary](#summary) + - [TraceZ](#tracez) + - [RPCz](#rpcz) +- [Usage](#usage) +- [Links of Interest](#links-of-interest) + +## Summary +zPages allow easy viewing of tracing information. When included for a process, zPages will display basic information about that process on a webpage. There are currently two types of zPages: TraceZ and RPCz. + +Including a zPage within a page is useful for developers because it's quicker to get running than adding extra code and installing external exporters like Jaeger and Zipkin. zPages tend to be more lightweight than these external exporters, but are also helpful for debugging latency issues and deadlocks. + +The idea of "zPages" originates from one of OpenTelemetry's predecessors, [OpenCensus](https://opencensus.io/). You can read more about it [here](https://opencensus.io/zpages). OpenCensus has different zPage implementations in [Java](https://opencensus.io/zpages/java/), [Go](https://opencensus.io/zpages/go/), and [Node](https://opencensus.io/zpages/node/) and there has been similar internal solutions developed at companies like Uber, but *this is the first major open-source implementation of zPages in C++*. Within OpenTelemetry, zPages are also being developed in [Java](https://github.com/open-telemetry/opentelemetry-java). + +#### How It Works +On a high level, zPages work by reading a process' spans using a SpanProcessor, which exports spans to the appropriate DataAggregator that a HttpServer uses. + +> TODO: Add picture examples for span overview and individual span view + +### TraceZ +TraceZ is a type of zPage that shows information on tracing spans, and allows users to look closer at specific and individual spans. Details a user would view include span id, name, status, and timestamps. The individual components of TraceZ are as follows: + +- TracezSpanProcessor (TSP) + - Contact point for TraceZ to connect with a process, which collects tracing information and provides an interface for TDA. +- TracezDataAggregator (TDA) + - Intermediary between the TSP and THS, which also performs various functions and calculations to send the correct tracing information to the THS. +- TracezHttpServer (THS) + - User-facing web page generator, which creates HTML pages using TDA that display 1) overall information on all of the process's spans and 2) more detailed information on specific spans when clicked. + +### RPCz +RPCz is a type of zPage that provides details on instrumented sent and received RPC messages. Although there is currently no ongoing development of RPCz for OpenTelemetry, OpenCensus zPages have implementations of RPCz (linked above). + +# Usage + +> TODO: Add instructions to add zPages + +## Links of Interest +- [TracezSpanProcessor Design Doc](https://docs.google.com/document/d/1kO4iZARYyr-EGBlY2VNM3ELU3iw6ZrC58Omup_YT-fU/edit#) (pending review) +- [TracezDataAggregator Design Doc](https://docs.google.com/document/d/1ziKFgvhXFfRXZjOlAHQRR-TzcNcTXzg1p2I9oPCEIoU/edit?ts=5ef0d177#heading=h.5irk4csrpu0y) (pending review) +- [TracezHttpServer Design Doc](https://docs.google.com/document/d/1U1V8QZ5LtGl4Mich-aJ6KZGLHrMIE8pWyspmzvnIefI/edit#) (draft) +- [Contribution Guidelines](https://github.com/open-telemetry/opentelemetry-cpp/blob/master/CONTRIBUTING.md) + + From b235655bfe4ff4c1654171f4817356615268af33 Mon Sep 17 00:00:00 2001 From: Nadia Ciobanu Date: Thu, 2 Jul 2020 23:20:38 +0000 Subject: [PATCH 34/41] Rebase --- exporters/otlp/CMakeLists.txt | 8 ++++++++ exporters/otlp/recordable.cc | 6 ++---- exporters/otlp/recordable_test.cc | 32 +++++++++++++++---------------- 3 files changed, 26 insertions(+), 20 deletions(-) diff --git a/exporters/otlp/CMakeLists.txt b/exporters/otlp/CMakeLists.txt index 07311ce909..3cfc1a5832 100644 --- a/exporters/otlp/CMakeLists.txt +++ b/exporters/otlp/CMakeLists.txt @@ -1,3 +1,11 @@ add_library(opentelemetry_exporter_otprotocol recordable.cc) target_link_libraries(opentelemetry_exporter_otprotocol $) + +add_executable(recordable_test recordable_test.cc) +target_link_libraries(recordable_test + ${GTEST_BOTH_LIBRARIES} + ${CMAKE_THREAD_LIBS_INIT} + opentelemetry_exporter_otprotocol + protobuf::libprotobuf) +gtest_add_tests(TARGET recordable_test TEST_PREFIX exporter. TEST_LIST recordable_test) diff --git a/exporters/otlp/recordable.cc b/exporters/otlp/recordable.cc index f3b44246ac..1677bb6be1 100644 --- a/exporters/otlp/recordable.cc +++ b/exporters/otlp/recordable.cc @@ -40,15 +40,13 @@ void Recordable::SetName(nostd::string_view name) noexcept void Recordable::SetStartTime(opentelemetry::core::SystemTimestamp start_time) noexcept { - std::chrono::nanoseconds nano_time = start_time.time_since_epoch(); - uint64_t nano_unix_time = nano_time.count(); + const uint64_t nano_unix_time = start_time.time_since_epoch().count(); span_.set_start_time_unix_nano(nano_unix_time); } void Recordable::SetDuration(std::chrono::nanoseconds duration) noexcept { - uint64_t unix_duration = duration.count(); - uint64_t unix_end_time = span_.start_time_unix_nano() + unix_duration; + const uint64_t unix_end_time = span_.start_time_unix_nano() + duration.count(); span_.set_end_time_unix_nano(unix_end_time); } } // namespace otlp diff --git a/exporters/otlp/recordable_test.cc b/exporters/otlp/recordable_test.cc index 0318bd0587..b086c46b36 100644 --- a/exporters/otlp/recordable_test.cc +++ b/exporters/otlp/recordable_test.cc @@ -9,28 +9,28 @@ namespace otlp { TEST(Recordable, SetIds) { - Recordable rec; - - const uint8_t trace_id_buff[trace::TraceId::kSize] = {0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 1}; - const uint8_t span_id_buff[trace::SpanId::kSize] = {0, 0, 0, 0, 0, 0, 0, 2}; - const uint8_t parent_span_id_buff[trace::SpanId::kSize] = {0, 0, 0, 0, 0, 0, 0, 3}; - const trace::TraceId trace_id( - nostd::span(trace_id_buff, trace::TraceId::kSize)); + std::array( + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1})); + const trace::SpanId span_id( - nostd::span(span_id_buff, trace::SpanId::kSize)); + std::array( + {0, 0, 0, 0, 0, 0, 0, 2})); + const trace::SpanId parent_span_id( - nostd::span(parent_span_id_buff, trace::SpanId::kSize)); + std::array( + {0, 0, 0, 0, 0, 0, 0, 3})); + + Recordable rec; rec.SetIds(trace_id, span_id, parent_span_id); EXPECT_EQ(rec.span().trace_id(), - std::string(reinterpret_cast(trace_id_buff), trace::TraceId::kSize)); + std::string(reinterpret_cast(trace_id.Id().data()), trace::TraceId::kSize)); EXPECT_EQ(rec.span().span_id(), - std::string(reinterpret_cast(span_id_buff), trace::SpanId::kSize)); + std::string(reinterpret_cast(span_id.Id().data()), trace::SpanId::kSize)); EXPECT_EQ(rec.span().parent_span_id(), - std::string(reinterpret_cast(parent_span_id_buff), trace::SpanId::kSize)); + std::string(reinterpret_cast(parent_span_id.Id().data()), trace::SpanId::kSize)); } TEST(Recordable, SetName) @@ -38,7 +38,7 @@ TEST(Recordable, SetName) Recordable rec; nostd::string_view name = "TestSpan"; rec.SetName(name); - ASSERT_EQ(rec.span().name(), name); + EXPECT_EQ(rec.span().name(), name); } TEST(Recordable, SetStartTime) @@ -51,7 +51,7 @@ TEST(Recordable, SetStartTime) std::chrono::duration_cast(start_time.time_since_epoch()).count(); rec.SetStartTime(start_timestamp); - ASSERT_EQ(rec.span().start_time_unixnano(), unix_start); + EXPECT_EQ(rec.span().start_time_unix_nano(), unix_start); } TEST(Recordable, SetDuration) @@ -66,7 +66,7 @@ TEST(Recordable, SetDuration) rec.SetStartTime(start_timestamp); rec.SetDuration(duration); - ASSERT_EQ(rec.span().end_time_unixnano(), unix_end); + EXPECT_EQ(rec.span().end_time_unix_nano(), unix_end); } } // namespace otlp } // namespace exporter From 0abac8fb2bcaf17ff9657dfba184f4cbc7d07d92 Mon Sep 17 00:00:00 2001 From: Nadia Ciobanu Date: Thu, 2 Jul 2020 17:13:18 +0000 Subject: [PATCH 35/41] Update recordable for new proto defs --- WORKSPACE | 28 ++++++++-------------------- 1 file changed, 8 insertions(+), 20 deletions(-) diff --git a/WORKSPACE b/WORKSPACE index 2f48918241..95e4464772 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -21,33 +21,21 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") # https://github.com/bazelbuild/bazel/issues/6664 http_archive( name = "com_github_grpc_grpc", - strip_prefix = "grpc-master", - urls = ["https://github.com/grpc/grpc/archive/master.tar.gz"], + sha256 = "d6af0859d3ae4693b1955e972aa2e590d6f4d44baaa82651467c6beea453e30e", + strip_prefix = "grpc-1.26.0-pre1", + urls = [ + "https://github.com/grpc/grpc/archive/v1.26.0-pre1.tar.gz", + ], ) load("@com_github_grpc_grpc//bazel:grpc_deps.bzl", "grpc_deps") grpc_deps() -# grpc_deps() cannot load() its deps, this WORKSPACE has to do it. -# See also: https://github.com/bazelbuild/bazel/issues/1943 -load( - "@build_bazel_rules_apple//apple:repositories.bzl", - "apple_rules_dependencies", -) - -apple_rules_dependencies() - -load( - "@build_bazel_apple_support//lib:repositories.bzl", - "apple_support_dependencies", -) - -apple_support_dependencies() - -load("@upb//bazel:repository_defs.bzl", "bazel_version_repository") +# Load extra gRPC dependencies due to https://github.com/grpc/grpc/issues/20511 +load("@com_github_grpc_grpc//bazel:grpc_extra_deps.bzl", "grpc_extra_deps") -bazel_version_repository(name = "upb_bazel_version") +grpc_extra_deps() # Uses older protobuf version because of # https://github.com/protocolbuffers/protobuf/issues/7179 From 3d5e14d7ac863295b4ab032ac970fe155a429586 Mon Sep 17 00:00:00 2001 From: Nadia Ciobanu Date: Thu, 2 Jul 2020 19:00:11 +0000 Subject: [PATCH 36/41] Refactor export --- bazel/opentelemetry_proto.BUILD | 2 -- exporters/otlp/otlp_exporter.cc | 41 +++++++++++++++------------- exporters/otlp/otlp_exporter.h | 12 ++++++-- exporters/otlp/otlp_exporter_test.cc | 20 ++++++++++---- 4 files changed, 46 insertions(+), 29 deletions(-) diff --git a/bazel/opentelemetry_proto.BUILD b/bazel/opentelemetry_proto.BUILD index d8e95b4310..793747fc10 100644 --- a/bazel/opentelemetry_proto.BUILD +++ b/bazel/opentelemetry_proto.BUILD @@ -61,8 +61,6 @@ cc_proto_library( deps = [":trace_proto"], ) - - proto_library( name = "trace_service_proto", srcs = [ diff --git a/exporters/otlp/otlp_exporter.cc b/exporters/otlp/otlp_exporter.cc index 97ce2e9369..6080ffe511 100644 --- a/exporters/otlp/otlp_exporter.cc +++ b/exporters/otlp/otlp_exporter.cc @@ -8,26 +8,13 @@ namespace otlp std::unique_ptr OtlpExporter::MakeRecordable() noexcept { -return std::unique_ptr(new Recordable); + return std::unique_ptr(new Recordable); } -sdk::trace::ExportResult OtlpExporter::Export( - const nostd::span> &spans) noexcept +void OtlpExporter::PopulateRequest(const nostd::span> &spans, + proto::collector::trace::v1::ExportTraceServiceRequest *request) { - std::cout << "Exporting" << std::endl; - grpc::ClientContext context; - - proto::collector::trace::v1::ExportTraceServiceRequest request; - proto::collector::trace::v1::ExportTraceServiceResponse response; - - std::unique_ptr trace_service_stub; - - const std::string address = "localhost:55678"; - auto channel = grpc::CreateChannel(address, grpc::InsecureChannelCredentials()); - - trace_service_stub = proto::collector::trace::v1::TraceService::NewStub(channel); - - proto::trace::v1::ResourceSpans* resource_span = request.add_resource_spans(); + proto::trace::v1::ResourceSpans* resource_span = request->add_resource_spans(); proto::trace::v1::InstrumentationLibrarySpans* instrumentation_lib = resource_span->add_instrumentation_library_spans(); for (auto &recordable : spans) { @@ -35,12 +22,28 @@ sdk::trace::ExportResult OtlpExporter::Export( auto rec = std::unique_ptr( static_cast(recordable.release())); - std::cout << "Name: " << rec->span().name() << std::endl; + //std::cout << "Name: " << rec->span().name() << std::endl; proto::trace::v1::Span* span = instrumentation_lib->add_spans(); - span->CopyFrom(rec->span()); } +} + +sdk::trace::ExportResult OtlpExporter::Export( + const nostd::span> &spans) noexcept +{ + proto::collector::trace::v1::ExportTraceServiceRequest request; + + PopulateRequest(spans, &request); + + // Send request + const std::string address = "localhost:55678"; + auto channel = grpc::CreateChannel(address, grpc::InsecureChannelCredentials()); + + auto trace_service_stub = proto::collector::trace::v1::TraceService::NewStub(channel); + + grpc::ClientContext context; + proto::collector::trace::v1::ExportTraceServiceResponse response; grpc::Status status = trace_service_stub->Export(&context, request, &response); diff --git a/exporters/otlp/otlp_exporter.h b/exporters/otlp/otlp_exporter.h index ccb56d6071..61c4aebef0 100644 --- a/exporters/otlp/otlp_exporter.h +++ b/exporters/otlp/otlp_exporter.h @@ -20,12 +20,18 @@ namespace otlp { class OtlpExporter final : public opentelemetry::sdk::trace::SpanExporter { - std::unique_ptr MakeRecordable() noexcept override; +public: + std::unique_ptr MakeRecordable() noexcept override; - opentelemetry::sdk::trace::ExportResult Export( - const opentelemetry::nostd::span> &spans) noexcept override; + sdk::trace::ExportResult Export( + const nostd::span> &spans) noexcept override; void Shutdown(std::chrono::microseconds timeout = std::chrono::microseconds(0)) noexcept override {}; + +private: + // Add span data from recordables to request + void PopulateRequest(const nostd::span> &spans, + proto::collector::trace::v1::ExportTraceServiceRequest *request); }; } // namespace otlp } // namespace exporter diff --git a/exporters/otlp/otlp_exporter_test.cc b/exporters/otlp/otlp_exporter_test.cc index b163353762..cc45800148 100644 --- a/exporters/otlp/otlp_exporter_test.cc +++ b/exporters/otlp/otlp_exporter_test.cc @@ -4,6 +4,7 @@ #include "opentelemetry/trace/provider.h" #include +// #include OPENTELEMETRY_BEGIN_NAMESPACE namespace exporter @@ -11,6 +12,16 @@ namespace exporter namespace otlp { +// class MockOtlpExporter : public opentelemetry::sdk::trace::SpanExporter +// { +// public: +// MOCK_METHOD(std::unique_ptr, MakeRecordable, (), (override)); +// MOCK_METHOD(sdk::trace::ExportResult, Export, (const nostd::span> +// &spans), (override)); +// MOCK_METHOD(std::unique_ptr, MakeRecordable, (), (override)); +// } + +// Helper functions trace::TraceId GenerateRandomTraceId() { uint8_t trace_id_buf[trace::TraceId::kSize]; @@ -30,15 +41,14 @@ TEST(OtlpExporter, ExportInternal) auto exporter = std::unique_ptr(new OtlpExporter); auto rec = exporter->MakeRecordable(); - // Name - nostd::string_view name = "TestSpan"; + // Set up recordable + nostd::string_view name = "Test Span"; rec->SetName(name); - // IDs rec->SetIds(GenerateRandomTraceId(), GenerateRandomSpanId(), GenerateRandomSpanId()); - // Start time + core::SystemTimestamp start_timestamp(std::chrono::system_clock::now()); rec->SetStartTime(start_timestamp); - // Duration + std::chrono::nanoseconds duration(10); rec->SetDuration(duration); From 466826679baec5ef1be3386209411533e1b07598 Mon Sep 17 00:00:00 2001 From: Nadia Ciobanu Date: Thu, 2 Jul 2020 11:34:02 -0700 Subject: [PATCH 37/41] Update OpenTelemetry protobuf definitions (#140) --- third_party/opentelemetry-proto/README | 2 +- .../metrics/v1/metrics_service_http.yaml | 9 + .../trace/v1/trace_service_http.yaml | 3 - .../proto/metrics/v1/metrics.proto | 233 ++++++++++++------ 4 files changed, 166 insertions(+), 81 deletions(-) create mode 100644 third_party/opentelemetry-proto/opentelemetry/proto/collector/metrics/v1/metrics_service_http.yaml diff --git a/third_party/opentelemetry-proto/README b/third_party/opentelemetry-proto/README index dfd3a68e21..e32d7de183 100644 --- a/third_party/opentelemetry-proto/README +++ b/third_party/opentelemetry-proto/README @@ -1,2 +1,2 @@ From: https://github.com/open-telemetry/opentelemetry-proto -Commit: d496c80b353bc4a4f754ae686b59ca3c41de0946 +Commit: e43e1abc40428a6ee98e3bfd79bec1dfa2ed18cd diff --git a/third_party/opentelemetry-proto/opentelemetry/proto/collector/metrics/v1/metrics_service_http.yaml b/third_party/opentelemetry-proto/opentelemetry/proto/collector/metrics/v1/metrics_service_http.yaml new file mode 100644 index 0000000000..bc5f9ff241 --- /dev/null +++ b/third_party/opentelemetry-proto/opentelemetry/proto/collector/metrics/v1/metrics_service_http.yaml @@ -0,0 +1,9 @@ +# This is an API configuration to generate an HTTP/JSON -> gRPC gateway for the +# OpenTelemetry service using github.com/grpc-ecosystem/grpc-gateway. +type: google.api.Service +config_version: 3 +http: + rules: + - selector: opentelemetry.proto.collector.metrics.v1.MetricsService.Export + post: /v1/metrics + body: "*" diff --git a/third_party/opentelemetry-proto/opentelemetry/proto/collector/trace/v1/trace_service_http.yaml b/third_party/opentelemetry-proto/opentelemetry/proto/collector/trace/v1/trace_service_http.yaml index 7754e5ff12..10eae48d51 100644 --- a/third_party/opentelemetry-proto/opentelemetry/proto/collector/trace/v1/trace_service_http.yaml +++ b/third_party/opentelemetry-proto/opentelemetry/proto/collector/trace/v1/trace_service_http.yaml @@ -7,6 +7,3 @@ http: - selector: opentelemetry.proto.collector.trace.v1.TraceService.Export post: /v1/trace body: "*" - - selector: opentelemetry.proto.collector.metrics.v1.MetricsService.Export - post: /v1/trace - body: "*" \ No newline at end of file diff --git a/third_party/opentelemetry-proto/opentelemetry/proto/metrics/v1/metrics.proto b/third_party/opentelemetry-proto/opentelemetry/proto/metrics/v1/metrics.proto index a15a7b6c2b..1d458824fe 100644 --- a/third_party/opentelemetry-proto/opentelemetry/proto/metrics/v1/metrics.proto +++ b/third_party/opentelemetry-proto/opentelemetry/proto/metrics/v1/metrics.proto @@ -24,14 +24,24 @@ option java_package = "io.opentelemetry.proto.metrics.v1"; option java_outer_classname = "MetricsProto"; option go_package = "github.com/open-telemetry/opentelemetry-proto/gen/go/metrics/v1"; -// A collection of metrics from a Resource. +// A collection of InstrumentationLibraryMetrics from a Resource. message ResourceMetrics { - // A list of metrics that originate from a resource. - repeated Metric metrics = 1; - // The resource for the metrics in this message. // If this field is not set then no resource info is known. - opentelemetry.proto.resource.v1.Resource resource = 2; + opentelemetry.proto.resource.v1.Resource resource = 1; + + // A list of metrics that originate from a resource. + repeated InstrumentationLibraryMetrics instrumentation_library_metrics = 2; +} + +// A collection of Metrics produced by an InstrumentationLibrary. +message InstrumentationLibraryMetrics { + // The instrumentation library information for the metrics in this message. + // If this field is not set then no library info is known. + opentelemetry.proto.common.v1.InstrumentationLibrary instrumentation_library = 1; + + // A list of metrics that originate from an instrumentation library. + repeated Metric metrics = 2; } // Defines a Metric which has one or more timeseries. @@ -90,10 +100,10 @@ message Metric { // Data is a list of one or more DataPoints for a single metric. Only one of the // following fields is used for the data, depending on the type of the metric defined // by MetricDescriptor.type field. - repeated Int64DataPoint int64_datapoints = 2; - repeated DoubleDataPoint double_datapoints = 3; - repeated HistogramDataPoint histogram_datapoints = 4; - repeated SummaryDataPoint summary_datapoints = 5; + repeated Int64DataPoint int64_data_points = 2; + repeated DoubleDataPoint double_data_points = 3; + repeated HistogramDataPoint histogram_data_points = 4; + repeated SummaryDataPoint summary_data_points = 5; } // Defines a metric type and its schema. @@ -108,49 +118,36 @@ message MetricDescriptor { // described by http://unitsofmeasure.org/ucum.html. string unit = 3; - // Type of the metric. It describes how the data is reported. - // - // A gauge is an instantaneous measurement of a value. - // - // A counter/cumulative measurement is a value accumulated over a time - // interval. In a time series, cumulative measurements should have the same - // start time, increasing values, until an event resets the cumulative value - // to zero and sets a new start time for the subsequent points. + // Type is the type of values a metric has. enum Type { - // Do not use this default value. - UNSPECIFIED = 0; - - // Integer gauge. The value can go both up and down over time. - // Corresponding values are stored in Int64DataPoint. - GAUGE_INT64 = 1; - - // Floating point gauge. The value can go both up and down over time. - // Corresponding values are stored in DoubleDataPoint. - GAUGE_DOUBLE = 2; - - // Histogram gauge measurement. - // Used in scenarios like a snapshot of time that current items in a queue - // have spent there. - // Corresponding values are stored in HistogramDataPoint. The count and sum of the - // histogram can go both up and down over time. Recorded values are always >= 0. - GAUGE_HISTOGRAM = 3; - - // Integer counter measurement. The value cannot decrease; if value is reset then - // start_time_unixnano should also be reset. - // Corresponding values are stored in Int64DataPoint. - COUNTER_INT64 = 4; - - // Floating point counter measurement. The value cannot decrease, if - // resets then the start_time_unixnano should also be reset. - // Recorded values are always >= 0. - // Corresponding values are stored in DoubleDataPoint. - COUNTER_DOUBLE = 5; - - // Histogram cumulative measurement. - // Corresponding values are stored in HistogramDataPoint. The count and sum of the - // histogram cannot decrease; if values are reset then start_time_unixnano - // should also be reset to the new start timestamp. - CUMULATIVE_HISTOGRAM = 6; + // INVALID_TYPE is the default Type, it MUST not be used. + INVALID_TYPE = 0; + + // INT64 values are signed 64-bit integers. + // + // A Metric of this Type MUST store its values as Int64DataPoint. + INT64 = 1; + + // MONOTONIC_INT64 values are monotonically increasing signed 64-bit + // integers. + // + // A Metric of this Type MUST store its values as Int64DataPoint. + MONOTONIC_INT64 = 2; + + // DOUBLE values are double-precision floating-point numbers. + // + // A Metric of this Type MUST store its values as DoubleDataPoint. + DOUBLE = 3; + + // MONOTONIC_DOUBLE values are monotonically increasing double-precision + // floating-point numbers. + // + // A Metric of this Type MUST store its values as DoubleDataPoint. + MONOTONIC_DOUBLE = 4; + + // Histogram measurement. + // Corresponding values are stored in HistogramDataPoint. + HISTOGRAM = 5; // Summary value. Some frameworks implemented Histograms as a summary of observations // (usually things like request durations and response sizes). While it @@ -158,13 +155,88 @@ message MetricDescriptor { // values, it calculates configurable percentiles over a sliding time // window. // Corresponding values are stored in SummaryDataPoint. - SUMMARY = 7; + SUMMARY = 6; } + + // type is the type of values this metric has. Type type = 4; - // The set of labels associated with the metric descriptor. Labels in this list apply to - // all data points. - repeated opentelemetry.proto.common.v1.StringKeyValue labels = 5; + // Temporality is the temporal quality values of a metric have. It + // describes how those values relate to the time interval over which they + // are reported. + enum Temporality { + // INVALID_TEMPORALITY is the default Temporality, it MUST not be + // used. + INVALID_TEMPORALITY = 0; + + // INSTANTANEOUS is a metric whose values are measured at a particular + // instant. The values are not aggregated over any time interval and are + // unique per timestamp. As such, these metrics are not expected to have + // an associated start time. + INSTANTANEOUS = 1; + + // DELTA is a metric whose values are the aggregation of measurements + // made over a time interval. Successive metrics contain aggregation of + // values from continuous and non-overlapping intervals. + // + // The values for a DELTA metric are based only on the time interval + // associated with one measurement cycle. There is no dependency on + // previous measurements like is the case for CUMULATIVE metrics. + // + // For example, consider a system measuring the number of requests that + // it receives and reports the sum of these requests every second as a + // DELTA metric: + // + // 1. The system starts receiving at time=t_0. + // 2. A request is received, the system measures 1 request. + // 3. A request is received, the system measures 1 request. + // 4. A request is received, the system measures 1 request. + // 5. The 1 second collection cycle ends. A metric is exported for the + // number of requests received over the interval of time t_0 to + // t_0+1 with a value of 3. + // 6. A request is received, the system measures 1 request. + // 7. A request is received, the system measures 1 request. + // 8. The 1 second collection cycle ends. A metric is exported for the + // number of requests received over the interval of time t_0+1 to + // t_0+2 with a value of 2. + DELTA = 2; + + // CUMULATIVE is a metric whose values are the aggregation of + // successively made measurements from a fixed start time until the last + // reported measurement. This means that current values of a CUMULATIVE + // metric depend on all previous measurements since the start time. + // Because of this, the sender is required to retain this state in some + // form. If this state is lost or invalidated, the CUMULATIVE metric + // values MUST be reset and a new fixed start time following the last + // reported measurement time sent MUST be used. + // + // For example, consider a system measuring the number of requests that + // it receives and reports the sum of these requests every second as a + // CUMULATIVE metric: + // + // 1. The system starts receiving at time=t_0. + // 2. A request is received, the system measures 1 request. + // 3. A request is received, the system measures 1 request. + // 4. A request is received, the system measures 1 request. + // 5. The 1 second collection cycle ends. A metric is exported for the + // number of requests received over the interval of time t_0 to + // t_0+1 with a value of 3. + // 6. A request is received, the system measures 1 request. + // 7. A request is received, the system measures 1 request. + // 8. The 1 second collection cycle ends. A metric is exported for the + // number of requests received over the interval of time t_0 to + // t_0+2 with a value of 5. + // 9. The system experiences a fault and loses state. + // 10. The system recovers and resumes receiving at time=t_1. + // 11. A request is received, the system measures 1 request. + // 12. The 1 second collection cycle ends. A metric is exported for the + // number of requests received over the interval of time t_1 to + // t_0+1 with a value of 1. + CUMULATIVE = 3; + } + + // temporality is the Temporality of values this metric has. + Temporality temporality = 5; } // Int64DataPoint is a single data point in a timeseries that describes the time-varying @@ -173,20 +245,20 @@ message Int64DataPoint { // The set of labels that uniquely identify this timeseries. repeated opentelemetry.proto.common.v1.StringKeyValue labels = 1; - // start_time_unixnano is the time when the cumulative value was reset to zero. + // start_time_unix_nano is the time when the cumulative value was reset to zero. // This is used for Counter type only. For Gauge the value is not specified and // defaults to 0. // - // The cumulative value is over the time interval (start_time_unixnano, timestamp_unixnano]. + // The cumulative value is over the time interval (start_time_unix_nano, time_unix_nano]. // Value is UNIX Epoch time in nanoseconds since 00:00:00 UTC on 1 January 1970. // // Value of 0 indicates that the timestamp is unspecified. In that case the timestamp // may be decided by the backend. - fixed64 start_time_unixnano = 2; + fixed64 start_time_unix_nano = 2; - // timestamp_unixnano is the moment when this value was recorded. + // time_unix_nano is the moment when this value was recorded. // Value is UNIX Epoch time in nanoseconds since 00:00:00 UTC on 1 January 1970. - fixed64 timestamp_unixnano = 3; + fixed64 time_unix_nano = 3; // value itself. int64 value = 4; @@ -198,20 +270,20 @@ message DoubleDataPoint { // The set of labels that uniquely identify this timeseries. repeated opentelemetry.proto.common.v1.StringKeyValue labels = 1; - // start_time_unixnano is the time when the cumulative value was reset to zero. + // start_time_unix_nano is the time when the cumulative value was reset to zero. // This is used for Counter type only. For Gauge the value is not specified and // defaults to 0. // - // The cumulative value is over the time interval (start_time_unixnano, timestamp_unixnano]. + // The cumulative value is over the time interval (start_time_unix_nano, time_unix_nano]. // Value is UNIX Epoch time in nanoseconds since 00:00:00 UTC on 1 January 1970. // // Value of 0 indicates that the timestamp is unspecified. In that case the timestamp // may be decided by the backend. - fixed64 start_time_unixnano = 2; + fixed64 start_time_unix_nano = 2; - // timestamp_unixnano is the moment when this value was recorded. + // time_unix_nano is the moment when this value was recorded. // Value is UNIX Epoch time in nanoseconds since 00:00:00 UTC on 1 January 1970. - fixed64 timestamp_unixnano = 3; + fixed64 time_unix_nano = 3; // value itself. double value = 4; @@ -224,19 +296,19 @@ message HistogramDataPoint { // The set of labels that uniquely identify this timeseries. repeated opentelemetry.proto.common.v1.StringKeyValue labels = 1; - // start_time_unixnano is the time when the cumulative value was reset to zero. + // start_time_unix_nano is the time when the cumulative value was reset to zero. // - // The cumulative value is over the time interval (start_time_unixnano, timestamp_unixnano]. + // The cumulative value is over the time interval (start_time_unix_nano, time_unix_nano]. // Value is UNIX Epoch time in nanoseconds since 00:00:00 UTC on 1 January 1970. // // Value of 0 indicates that the timestamp is unspecified. In that case the timestamp // may be decided by the backend. // Note: this field is always unspecified and ignored if MetricDescriptor.type==GAUGE_HISTOGRAM. - fixed64 start_time_unixnano = 2; + fixed64 start_time_unix_nano = 2; - // timestamp_unixnano is the moment when this value was recorded. + // time_unix_nano is the moment when this value was recorded. // Value is UNIX Epoch time in nanoseconds since 00:00:00 UTC on 1 January 1970. - fixed64 timestamp_unixnano = 3; + fixed64 time_unix_nano = 3; // count is the number of values in the population. Must be non-negative. This value // must be equal to the sum of the "count" fields in buckets if a histogram is provided. @@ -262,9 +334,9 @@ message HistogramDataPoint { // the defined bounds. double value = 1; - // timestamp_unixnano is the moment when this exemplar was recorded. + // time_unix_nano is the moment when this exemplar was recorded. // Value is UNIX Epoch time in nanoseconds since 00:00:00 UTC on 1 January 1970. - fixed64 timestamp_unixnano = 2; + fixed64 time_unix_nano = 2; // exemplar_attachments are contextual information about the example value. // Keys in this list must be unique. @@ -318,18 +390,18 @@ message SummaryDataPoint { // The set of labels that uniquely identify this timeseries. repeated opentelemetry.proto.common.v1.StringKeyValue labels = 1; - // start_time_unixnano is the time when the cumulative value was reset to zero. + // start_time_unix_nano is the time when the cumulative value was reset to zero. // - // The cumulative value is over the time interval (start_time_unixnano, timestamp_unixnano]. + // The cumulative value is over the time interval (start_time_unix_nano, time_unix_nano]. // Value is UNIX Epoch time in nanoseconds since 00:00:00 UTC on 1 January 1970. // // Value of 0 indicates that the timestamp is unspecified. In that case the timestamp // may be decided by the backend. - fixed64 start_time_unixnano = 2; + fixed64 start_time_unix_nano = 2; - // timestamp_unixnano is the moment when this value was recorded. + // time_unix_nano is the moment when this value was recorded. // Value is UNIX Epoch time in nanoseconds since 00:00:00 UTC on 1 January 1970. - fixed64 timestamp_unixnano = 3; + fixed64 time_unix_nano = 3; // The total number of recorded values since start_time. Optional since // some systems don't expose this. @@ -340,6 +412,13 @@ message SummaryDataPoint { double sum = 5; // Represents the value at a given percentile of a distribution. + // + // To record Min and Max values following conventions are used: + // - The 100th percentile is equivalent to the maximum value observed. + // - The 0th percentile is equivalent to the minimum value observed. + // + // See the following issue for more context: + // https://github.com/open-telemetry/opentelemetry-proto/issues/125 message ValueAtPercentile { // The percentile of a distribution. Must be in the interval // [0.0, 100.0]. From a7a7693e1212eea702ace1917f57dc5931ed6ad4 Mon Sep 17 00:00:00 2001 From: Nadia Ciobanu Date: Thu, 2 Jul 2020 20:47:52 +0000 Subject: [PATCH 38/41] Use mocks in test --- bazel/opentelemetry_proto.BUILD | 1 + exporters/otlp/otlp_exporter.cc | 51 +++++++++++++--------- exporters/otlp/otlp_exporter.h | 11 +++-- exporters/otlp/otlp_exporter_test.cc | 64 ++++++++++++++++------------ 4 files changed, 78 insertions(+), 49 deletions(-) diff --git a/bazel/opentelemetry_proto.BUILD b/bazel/opentelemetry_proto.BUILD index 793747fc10..8b6c50d680 100644 --- a/bazel/opentelemetry_proto.BUILD +++ b/bazel/opentelemetry_proto.BUILD @@ -81,4 +81,5 @@ cc_grpc_library( srcs = [":trace_service_proto"], grpc_only = True, deps = [":trace_service_proto_cc"], + generate_mocks = True, ) diff --git a/exporters/otlp/otlp_exporter.cc b/exporters/otlp/otlp_exporter.cc index 6080ffe511..ca1cf7a420 100644 --- a/exporters/otlp/otlp_exporter.cc +++ b/exporters/otlp/otlp_exporter.cc @@ -6,29 +6,48 @@ namespace exporter namespace otlp { -std::unique_ptr OtlpExporter::MakeRecordable() noexcept -{ - return std::unique_ptr(new Recordable); -} +const std::string KCollectorAddress = "localhost:55678"; + +// ----------------------------- Helper functions ------------------------------ -void OtlpExporter::PopulateRequest(const nostd::span> &spans, - proto::collector::trace::v1::ExportTraceServiceRequest *request) +// Add span protobufs contained in recordables to request +void PopulateRequest(const nostd::span> &spans, + proto::collector::trace::v1::ExportTraceServiceRequest *request) { - proto::trace::v1::ResourceSpans* resource_span = request->add_resource_spans(); - proto::trace::v1::InstrumentationLibrarySpans* instrumentation_lib = resource_span->add_instrumentation_library_spans(); + auto resource_span = request->add_resource_spans(); + auto instrumentation_lib = resource_span->add_instrumentation_library_spans(); for (auto &recordable : spans) { - auto rec = std::unique_ptr( - static_cast(recordable.release())); - - //std::cout << "Name: " << rec->span().name() << std::endl; + static_cast(recordable.release())); proto::trace::v1::Span* span = instrumentation_lib->add_spans(); span->CopyFrom(rec->span()); } } +// Establish connection to OpenTelemetry Collector +std::unique_ptr MakeServiceStub() +{ + auto channel = grpc::CreateChannel(KCollectorAddress, grpc::InsecureChannelCredentials()); + return proto::collector::trace::v1::TraceService::NewStub(channel); +} + +// -------------------------------- Contructors -------------------------------- + +OtlpExporter::OtlpExporter(): OtlpExporter(MakeServiceStub()) {} + +OtlpExporter::OtlpExporter( + std::unique_ptr stub): + trace_service_stub_(std::move(stub)) {} + +// ----------------------------- Exporter methods ------------------------------ + +std::unique_ptr OtlpExporter::MakeRecordable() noexcept +{ + return std::unique_ptr(new Recordable); +} + sdk::trace::ExportResult OtlpExporter::Export( const nostd::span> &spans) noexcept { @@ -36,16 +55,10 @@ sdk::trace::ExportResult OtlpExporter::Export( PopulateRequest(spans, &request); - // Send request - const std::string address = "localhost:55678"; - auto channel = grpc::CreateChannel(address, grpc::InsecureChannelCredentials()); - - auto trace_service_stub = proto::collector::trace::v1::TraceService::NewStub(channel); - grpc::ClientContext context; proto::collector::trace::v1::ExportTraceServiceResponse response; - grpc::Status status = trace_service_stub->Export(&context, request, &response); + grpc::Status status = trace_service_stub_->Export(&context, request, &response); if(status.ok()){ std::cout << "Status OK" << std::endl; diff --git a/exporters/otlp/otlp_exporter.h b/exporters/otlp/otlp_exporter.h index 61c4aebef0..7c1223d14a 100644 --- a/exporters/otlp/otlp_exporter.h +++ b/exporters/otlp/otlp_exporter.h @@ -21,6 +21,8 @@ namespace otlp class OtlpExporter final : public opentelemetry::sdk::trace::SpanExporter { public: + OtlpExporter(); + std::unique_ptr MakeRecordable() noexcept override; sdk::trace::ExportResult Export( @@ -29,9 +31,12 @@ class OtlpExporter final : public opentelemetry::sdk::trace::SpanExporter void Shutdown(std::chrono::microseconds timeout = std::chrono::microseconds(0)) noexcept override {}; private: - // Add span data from recordables to request - void PopulateRequest(const nostd::span> &spans, - proto::collector::trace::v1::ExportTraceServiceRequest *request); + // For testing + friend class OtlpExporterTestPeer; + + std::unique_ptr trace_service_stub_; + + OtlpExporter(std::unique_ptr stub); }; } // namespace otlp } // namespace exporter diff --git a/exporters/otlp/otlp_exporter_test.cc b/exporters/otlp/otlp_exporter_test.cc index cc45800148..dcf31c2f58 100644 --- a/exporters/otlp/otlp_exporter_test.cc +++ b/exporters/otlp/otlp_exporter_test.cc @@ -2,9 +2,9 @@ #include "opentelemetry/sdk/trace/simple_processor.h" #include "opentelemetry/sdk/trace/tracer_provider.h" #include "opentelemetry/trace/provider.h" +#include "opentelemetry/proto/collector/trace/v1/trace_service_mock.grpc.pb.h" #include -// #include OPENTELEMETRY_BEGIN_NAMESPACE namespace exporter @@ -12,33 +12,39 @@ namespace exporter namespace otlp { -// class MockOtlpExporter : public opentelemetry::sdk::trace::SpanExporter -// { -// public: -// MOCK_METHOD(std::unique_ptr, MakeRecordable, (), (override)); -// MOCK_METHOD(sdk::trace::ExportResult, Export, (const nostd::span> -// &spans), (override)); -// MOCK_METHOD(std::unique_ptr, MakeRecordable, (), (override)); -// } - -// Helper functions -trace::TraceId GenerateRandomTraceId() -{ - uint8_t trace_id_buf[trace::TraceId::kSize]; - opentelemetry::sdk::common::Random::GenerateRandomBuffer(trace_id_buf); - return trace::TraceId(trace_id_buf); -} +using namespace testing; -trace::SpanId GenerateRandomSpanId() +class OtlpExporterTestPeer : public ::testing::Test { - uint8_t span_id_buf[trace::SpanId::kSize]; - opentelemetry::sdk::common::Random::GenerateRandomBuffer(span_id_buf); - return trace::SpanId(span_id_buf); -} +public: + std::unique_ptr GetExporter( + proto::collector::trace::v1::TraceService::StubInterface* mock_stub) + { + return std::unique_ptr( + new OtlpExporter(std::unique_ptr(mock_stub))); + } + + trace::TraceId GenerateRandomTraceId() + { + uint8_t trace_id_buf[trace::TraceId::kSize]; + opentelemetry::sdk::common::Random::GenerateRandomBuffer(trace_id_buf); + return trace::TraceId(trace_id_buf); + } -TEST(OtlpExporter, ExportInternal) + trace::SpanId GenerateRandomSpanId() + { + uint8_t span_id_buf[trace::SpanId::kSize]; + opentelemetry::sdk::common::Random::GenerateRandomBuffer(span_id_buf); + return trace::SpanId(span_id_buf); + } +}; + +TEST_F(OtlpExporterTestPeer, ExportInternal) { - auto exporter = std::unique_ptr(new OtlpExporter); + auto mock_stub = new proto::collector::trace::v1::MockTraceServiceStub(); + EXPECT_CALL(*mock_stub, Export(_,_,_)).Times(AtLeast(1)).WillRepeatedly(Return(grpc::Status::OK)); + + auto exporter = std::unique_ptr(GetExporter(mock_stub)); auto rec = exporter->MakeRecordable(); // Set up recordable @@ -58,9 +64,13 @@ TEST(OtlpExporter, ExportInternal) EXPECT_EQ(sdk::trace::ExportResult::kSuccess, result); } -TEST(OtlpExporter, ExportExternal) +TEST_F(OtlpExporterTestPeer, ExportExternal) { - auto exporter = std::unique_ptr(new OtlpExporter); + auto mock_stub = new proto::collector::trace::v1::MockTraceServiceStub(); + EXPECT_CALL(*mock_stub, Export(_,_,_)).Times(AtLeast(1)).WillRepeatedly(Return(grpc::Status::OK)); + + auto exporter = std::unique_ptr(GetExporter(mock_stub)); + auto processor = std::shared_ptr( new sdk::trace::SimpleSpanProcessor(std::move(exporter))); auto provider = nostd::shared_ptr(new sdk::trace::TracerProvider(processor)); @@ -68,7 +78,7 @@ TEST(OtlpExporter, ExportExternal) nostd::shared_ptr tracer = provider->GetTracer("test"); - auto span = tracer->StartSpan("TestSpan"); + auto span = tracer->StartSpan("Test Span"); span->End(); } } // namespace otlp From ec36cb91331e0987756b9b4c427606e9db97a879 Mon Sep 17 00:00:00 2001 From: Nadia Ciobanu Date: Thu, 2 Jul 2020 22:04:58 +0000 Subject: [PATCH 39/41] Update tests --- exporters/otlp/otlp_exporter.cc | 10 ++-- exporters/otlp/otlp_exporter.h | 4 +- exporters/otlp/otlp_exporter_test.cc | 72 ++++++++++++---------------- 3 files changed, 35 insertions(+), 51 deletions(-) diff --git a/exporters/otlp/otlp_exporter.cc b/exporters/otlp/otlp_exporter.cc index ca1cf7a420..d3c274cb65 100644 --- a/exporters/otlp/otlp_exporter.cc +++ b/exporters/otlp/otlp_exporter.cc @@ -60,13 +60,11 @@ sdk::trace::ExportResult OtlpExporter::Export( grpc::Status status = trace_service_stub_->Export(&context, request, &response); - if(status.ok()){ - std::cout << "Status OK" << std::endl; - return sdk::trace::ExportResult::kSuccess; + if(!status.ok()){ + std::cerr << "OTLP trace exporter: Export() failed\n"; + return sdk::trace::ExportResult::kFailure; } - std::cout << "ERROR " << status.error_code() << ": " << status.error_message() - << std::endl; - return sdk::trace::ExportResult::kFailure; + return sdk::trace::ExportResult::kSuccess; } } // namespace otlp } // namespace exporter diff --git a/exporters/otlp/otlp_exporter.h b/exporters/otlp/otlp_exporter.h index 7c1223d14a..3565f96b1e 100644 --- a/exporters/otlp/otlp_exporter.h +++ b/exporters/otlp/otlp_exporter.h @@ -2,12 +2,10 @@ #include "opentelemetry/sdk/trace/exporter.h" #include "opentelemetry/sdk/trace/span_data.h" -#include "src/common/random.h" - #include "opentelemetry/proto/collector/trace/v1/trace_service.pb.h" #include "opentelemetry/proto/collector/trace/v1/trace_service.grpc.pb.h" #include "opentelemetry/proto/trace/v1/trace.pb.h" - +#include "src/common/random.h" #include "recordable.h" #include diff --git a/exporters/otlp/otlp_exporter_test.cc b/exporters/otlp/otlp_exporter_test.cc index dcf31c2f58..155e4f27c3 100644 --- a/exporters/otlp/otlp_exporter_test.cc +++ b/exporters/otlp/otlp_exporter_test.cc @@ -6,69 +6,53 @@ #include +using namespace testing; + OPENTELEMETRY_BEGIN_NAMESPACE namespace exporter { namespace otlp { -using namespace testing; - class OtlpExporterTestPeer : public ::testing::Test { public: std::unique_ptr GetExporter( proto::collector::trace::v1::TraceService::StubInterface* mock_stub) { - return std::unique_ptr( - new OtlpExporter(std::unique_ptr(mock_stub))); - } - - trace::TraceId GenerateRandomTraceId() - { - uint8_t trace_id_buf[trace::TraceId::kSize]; - opentelemetry::sdk::common::Random::GenerateRandomBuffer(trace_id_buf); - return trace::TraceId(trace_id_buf); - } - - trace::SpanId GenerateRandomSpanId() - { - uint8_t span_id_buf[trace::SpanId::kSize]; - opentelemetry::sdk::common::Random::GenerateRandomBuffer(span_id_buf); - return trace::SpanId(span_id_buf); + return std::unique_ptr( + new OtlpExporter(std::unique_ptr(mock_stub))); } }; -TEST_F(OtlpExporterTestPeer, ExportInternal) +// Call Export() directly +TEST_F(OtlpExporterTestPeer, ExportUnitTest) { auto mock_stub = new proto::collector::trace::v1::MockTraceServiceStub(); - EXPECT_CALL(*mock_stub, Export(_,_,_)).Times(AtLeast(1)).WillRepeatedly(Return(grpc::Status::OK)); + auto exporter = std::shared_ptr(GetExporter(mock_stub)); - auto exporter = std::unique_ptr(GetExporter(mock_stub)); - auto rec = exporter->MakeRecordable(); - - // Set up recordable - nostd::string_view name = "Test Span"; - rec->SetName(name); - rec->SetIds(GenerateRandomTraceId(), GenerateRandomSpanId(), GenerateRandomSpanId()); - - core::SystemTimestamp start_timestamp(std::chrono::system_clock::now()); - rec->SetStartTime(start_timestamp); - - std::chrono::nanoseconds duration(10); - rec->SetDuration(duration); - - nostd::span> recs(&rec, 1); - auto result = exporter->Export(recs); + auto recordable_1 = exporter->MakeRecordable(); + recordable_1->SetName("Test span 1"); + auto recordable_2 = exporter->MakeRecordable(); + recordable_2->SetName("Test span 2"); + // Test successful RPC + nostd::span> batch_1(&recordable_1, 1); + EXPECT_CALL(*mock_stub, Export(_,_,_)).Times(Exactly(1)).WillOnce(Return(grpc::Status::OK)); + auto result = exporter->Export(batch_1); EXPECT_EQ(sdk::trace::ExportResult::kSuccess, result); + + // Test failed RPC + nostd::span> batch2(&recordable_2, 1); + EXPECT_CALL(*mock_stub, Export(_,_,_)).Times(Exactly(1)).WillOnce(Return(grpc::Status::CANCELLED)); + auto result = exporter->Export(batch_2); + EXPECT_EQ(sdk::trace::ExportResult::kFailure, result); } -TEST_F(OtlpExporterTestPeer, ExportExternal) +// Create spans, let processor call Export() +TEST_F(OtlpExporterTestPeer, ExportIntegrationTest) { auto mock_stub = new proto::collector::trace::v1::MockTraceServiceStub(); - EXPECT_CALL(*mock_stub, Export(_,_,_)).Times(AtLeast(1)).WillRepeatedly(Return(grpc::Status::OK)); - auto exporter = std::unique_ptr(GetExporter(mock_stub)); auto processor = std::shared_ptr( @@ -78,9 +62,13 @@ TEST_F(OtlpExporterTestPeer, ExportExternal) nostd::shared_ptr tracer = provider->GetTracer("test"); - auto span = tracer->StartSpan("Test Span"); - span->End(); + EXPECT_CALL(*mock_stub, Export(_,_,_)).Times(AtLeast(1)).WillRepeatedly(Return(grpc::Status::OK)); + + auto parent_span = tracer->StartSpan("Test parent span"); + auto child_span = tracer->StartSpan("Test child span"); + child_span->End(); + parent_span->End(); } } // namespace otlp } // namespace exporter -OPENTELEMETRY_END_NAMESPACE \ No newline at end of file +OPENTELEMETRY_END_NAMESPACE From c328581737a561632fe62c9505fa9ed3965de872 Mon Sep 17 00:00:00 2001 From: Nadia Ciobanu Date: Thu, 2 Jul 2020 22:37:08 +0000 Subject: [PATCH 40/41] Fix test --- exporters/otlp/otlp_exporter_test.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/exporters/otlp/otlp_exporter_test.cc b/exporters/otlp/otlp_exporter_test.cc index 155e4f27c3..17e8cb9eb0 100644 --- a/exporters/otlp/otlp_exporter_test.cc +++ b/exporters/otlp/otlp_exporter_test.cc @@ -43,9 +43,9 @@ TEST_F(OtlpExporterTestPeer, ExportUnitTest) EXPECT_EQ(sdk::trace::ExportResult::kSuccess, result); // Test failed RPC - nostd::span> batch2(&recordable_2, 1); + nostd::span> batch_2(&recordable_2, 1); EXPECT_CALL(*mock_stub, Export(_,_,_)).Times(Exactly(1)).WillOnce(Return(grpc::Status::CANCELLED)); - auto result = exporter->Export(batch_2); + result = exporter->Export(batch_2); EXPECT_EQ(sdk::trace::ExportResult::kFailure, result); } From 6a465258028ab69320b5c3ded9ae85b757f7767a Mon Sep 17 00:00:00 2001 From: Nadia Ciobanu Date: Thu, 2 Jul 2020 22:55:55 +0000 Subject: [PATCH 41/41] Update workspace --- WORKSPACE | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/WORKSPACE b/WORKSPACE index 95e4464772..b8d0fb0752 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -37,6 +37,10 @@ load("@com_github_grpc_grpc//bazel:grpc_extra_deps.bzl", "grpc_extra_deps") grpc_extra_deps() +load("@upb//bazel:repository_defs.bzl", "bazel_version_repository") + +bazel_version_repository(name = "upb_bazel_version") + # Uses older protobuf version because of # https://github.com/protocolbuffers/protobuf/issues/7179 http_archive(