Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Populate resource to OTLP proto data #758

Merged
merged 6 commits into from
May 16, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .markdownlintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
third_party/**
tools/**
examples/otlp/README.md
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Increment the:

## [Unreleased]

* [EXPORTER] Populate resource to OTLP proto data ([#758](https://github.com/open-telemetry/opentelemetry-cpp/pull/758))

## [0.6.0] 2021-05-11

* [EXPORTER] Add Jaeger exporter ([#534](https://github.com/open-telemetry/opentelemetry-cpp/pull/534))
Expand Down
46 changes: 22 additions & 24 deletions examples/otlp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,34 @@ SDK](https://github.com/open-telemetry/opentelemetry-cpp). The application then
calls a `foo_library` which has been instrumented using the [OpenTelemetry
API](https://github.com/open-telemetry/opentelemetry-cpp/tree/main/api).

To enable TLS authentication for OTLP grpc exporter, SslCredentials can be used by
specifying the path to client certificate pem file, or the string containing this certificate via OtlpExporterOptions. The path to such a
.pem file can be provided as a command-line argument alongwith the collector endpoint
to the main binary invocation above.
To enable TLS authentication for OTLP grpc exporter, SslCredentials can be used
by specifying the path to client certificate pem file, or the string containing
this certificate via OtlpExporterOptions. The path to such a .pem file can be
provided as a command-line argument alongwith the collector endpoint to the main
binary invocation above.

Resulting spans are exported to the **OpenTelemetry Collector** using the OTLP
exporter. The OpenTelemetry Collector can be configured to export to other
backends (see list of [supported
backends](https://github.com/open-telemetry/opentelemetry-collector/blob/main/exporter/README.md)).

For instructions on downloading and running the OpenTelemetry Collector, see
[Getting Started](https://opentelemetry.io/docs/collector/about/).

Here is an example of a Collector `config.yaml` file that can be used to export
to [Zipkin](https://zipkin.io/) via the Collector using the OTLP exporter:

```yml
receivers:
otlp:
protocols:
grpc:
endpoint: localhost:4317
exporters:
zipkin:
endpoint: "http://localhost:9411/api/v2/spans"
service:
pipelines:
traces:
receivers: [otlp]
exporters: [zipkin]
Follow below command to run the **OpenTelemetry Collector** with OTLP receiver
in docker which dumps the received data into console. See [Getting
Started](https://opentelemetry.io/docs/collector/about/) for more information.

Open a terminal window at the root directory of this repo and launch the
OpenTelemetry Collector with an OTLP receiver by running:

- On Unix based systems use:

```console
docker run --rm -it -p 4317:4317 -v $(pwd)/examples/otlp:/cfg otel/opentelemetry-collector:0.19.0 --config=/cfg/opentelemetry-collector-config/config.dev.yaml
```

- On Windows use:

```console
docker run --rm -it -p 4317:4317 -v "%cd%/examples/otlp":/cfg otel/opentelemetry-collector:0.19.0 --config=/cfg/opentelemetry-collector-config/config.dev.yaml
```

Note that the OTLP exporter connects to the Collector at `localhost:4317` by
Expand Down
15 changes: 15 additions & 0 deletions examples/otlp/opentelemetry-collector-config/config.dev.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
exporters:
logging:
loglevel: DEBUG
receivers:
otlp:
protocols:
grpc:
endpoint: 0.0.0.0:4317
service:
pipelines:
traces:
receivers:
- otlp
exporters:
- logging
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Recordable final : public sdk::trace::Recordable
const proto::trace::v1::Span &span() const noexcept { return span_; }

/** Dynamically converts the resource of this span into a proto. */
proto::resource::v1::Resource ProtoResource() const noexcept;
void PopulateProtoResource(proto::resource::v1::Resource *resource) const noexcept;

void SetIdentity(const opentelemetry::trace::SpanContext &span_context,
opentelemetry::trace::SpanId parent_span_id) noexcept override;
Expand Down Expand Up @@ -53,7 +53,7 @@ class Recordable final : public sdk::trace::Recordable

private:
proto::trace::v1::Span span_;
const opentelemetry::sdk::resource::Resource *resource_;
const opentelemetry::sdk::resource::Resource *resource_ = nullptr;
};
} // namespace otlp
} // namespace exporter
Expand Down
12 changes: 11 additions & 1 deletion exporters/otlp/src/otlp_exporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,18 @@ void PopulateRequest(const nostd::span<std::unique_ptr<sdk::trace::Recordable>>
for (auto &recordable : spans)
{
auto rec = std::unique_ptr<Recordable>(static_cast<Recordable *>(recordable.release()));
// TODO - Handle Resource
*instrumentation_lib->add_spans() = std::move(rec->span());

if (!has_resource)
{
std::unique_ptr<proto::resource::v1::Resource> proto_resource{
new proto::resource::v1::Resource};

rec->PopulateProtoResource(proto_resource.get());

resource_span->set_allocated_resource(proto_resource.release());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit - Just wondering if line 35 - 40 can be replaced with single line:

resource_span->mutable_resource() = rec->ProtoResource();

using older implementation of ProtoResource.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I chose set_allocated_resource which could avoid allocate the Resource twice or more, but yes, it look simpler with mutable_resource. Updated the PR to the older implementation of ProtoResource as this optimization may not deserve the extra complexity.

has_resource = true;
}
}
}

Expand Down
6 changes: 2 additions & 4 deletions exporters/otlp/src/recordable.cc
Original file line number Diff line number Diff line change
Expand Up @@ -216,17 +216,15 @@ void PopulateAttribute(opentelemetry::proto::common::v1::KeyValue *attribute,
}
}

proto::resource::v1::Resource Recordable::ProtoResource() const noexcept
void Recordable::PopulateProtoResource(proto::resource::v1::Resource *proto) const noexcept
{
proto::resource::v1::Resource proto;
if (resource_)
{
for (const auto &kv : resource_->GetAttributes())
{
PopulateAttribute(proto.add_attributes(), kv.first, kv.second);
PopulateAttribute(proto->add_attributes(), kv.first, kv.second);
}
}
return proto;
}

void Recordable::SetResource(const opentelemetry::sdk::resource::Resource &resource) noexcept
Expand Down