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 all 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 @@ -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
7 changes: 6 additions & 1 deletion exporters/otlp/src/otlp_exporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@ 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)
{
*resource_span->mutable_resource() = rec->ProtoResource();
has_resource = true;
}
}
}

Expand Down
1 change: 1 addition & 0 deletions exporters/otlp/src/recordable.cc
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ proto::resource::v1::Resource Recordable::ProtoResource() const noexcept
PopulateAttribute(proto.add_attributes(), kv.first, kv.second);
}
}

return proto;
}

Expand Down
22 changes: 22 additions & 0 deletions exporters/otlp/test/recordable_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,28 @@ TEST(Recordable, AddLink)
}
}

TEST(Recordable, SetResource)
{
Recordable rec;
const std::string service_name_key = "service.name";
std::string service_name = "test-otlp";
auto resource =
opentelemetry::sdk::resource::Resource::Create({{service_name_key, service_name}});
rec.SetResource(resource);

auto proto_resource = rec.ProtoResource();
bool found_service_name = false;
for (size_t i = 0; i < proto_resource.attributes_size(); i++)
{
auto attr = proto_resource.attributes(static_cast<int>(i));
if (attr.key() == service_name_key && attr.value().string_value() == service_name)
{
found_service_name = true;
}
}
EXPECT_TRUE(found_service_name);
}

// Test non-int single types. Int single types are tested using templates (see IntAttributeTest)
TEST(Recordable, SetSingleAtrribute)
{
Expand Down