set service.name iff it's not set by the OTEL_SERVICE_NAME env var

This commit is contained in:
Grant Limberg 2025-07-15 10:19:22 -07:00
parent cf77a6a805
commit a0cc7babdf

View file

@ -1074,16 +1074,19 @@ class OneServiceImpl : public OneService {
auto processor = std::unique_ptr<sdktrace::SpanProcessor>(new sdktrace::BatchSpanProcessor(std::move(exporter), batch_options));
auto processors = std::vector<std::unique_ptr<sdktrace::SpanProcessor> >();
processors.push_back(std::move(processor));
char buf[256];
auto versionString = std::stringstream();
versionString << ZEROTIER_ONE_VERSION_MAJOR << "." << ZEROTIER_ONE_VERSION_MINOR << "." << ZEROTIER_ONE_VERSION_REVISION;
auto resource_attributes = sdkresource::ResourceAttributes { { "service.version", versionString.str() }, { "service.node_id", _node->identity().address().toString(buf) }, { "service.namespace", "com.zerotier.zerotier-one" } };
auto resource = sdkresource::Resource::Create(resource_attributes);
auto recvd_attributes = resource.GetAttributes();
if (! recvd_attributes.contains("service.name")) {
auto recvd_attributes = resource.GetAttributes().GetAttributes();
// If service.name is not set, we set it to "zerotier-one"
if (recvd_attributes.find("service.name") != recvd_attributes.end()) {
resource_attributes["service.name"] = "zerotier-one";
resource = sdkresource::Resource::Create(resource_attributes);
}
auto sampler = std::unique_ptr<sdktrace::Sampler>(new sdktrace::TraceIdRatioBasedSampler(_exporterSampleRate));
auto tracer_context = std::make_unique<sdktrace::TracerContext>(std::move(processors), resource, std::move(sampler));
_traceProvider = opentelemetry::nostd::shared_ptr<sdktrace::TracerProvider>(new sdktrace::TracerProvider(std::move(tracer_context)));