From e2b44dddce144bfcfec8a7fe90bb66f2dc131993 Mon Sep 17 00:00:00 2001 From: Huan Du Date: Wed, 27 Mar 2024 19:10:04 +0800 Subject: [PATCH] support gRPC protocol in CollectorExporter --- .gitignore | 1 + lib/sdk.dart | 3 +- .../collector/trace/v1/trace_service.pb.dart | 21 +---- .../trace/v1/trace_service.pbenum.dart | 3 - .../trace/v1/trace_service.pbgrpc.dart | 59 +++++++++++++ .../trace/v1/trace_service.pbjson.dart | 39 --------- .../proto/common/v1/common.pb.dart | 3 - .../proto/common/v1/common.pbenum.dart | 3 - .../proto/common/v1/common.pbjson.dart | 3 - .../proto/resource/v1/resource.pb.dart | 11 +-- .../proto/resource/v1/resource.pbenum.dart | 3 - .../proto/resource/v1/resource.pbjson.dart | 3 - .../proto/trace/v1/trace.pb.dart | 87 +++++++++++-------- .../proto/trace/v1/trace.pbenum.dart | 7 +- .../proto/trace/v1/trace.pbjson.dart | 8 +- .../trace/exporters/collector_exporter.dart | 45 ++++++++-- pubspec.yaml | 11 +-- 17 files changed, 173 insertions(+), 137 deletions(-) create mode 100644 lib/src/sdk/proto/opentelemetry/proto/collector/trace/v1/trace_service.pbgrpc.dart diff --git a/.gitignore b/.gitignore index 79f51c3d..ca93ff5c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .dart_tool .packages pubspec.lock +opentelemetry-proto \ No newline at end of file diff --git a/lib/sdk.dart b/lib/sdk.dart index ced41047..6c09d28f 100644 --- a/lib/sdk.dart +++ b/lib/sdk.dart @@ -8,7 +8,8 @@ export 'src/sdk/time_providers/datetime_time_provider.dart' show DateTimeTimeProvider; export 'src/sdk/time_providers/time_provider.dart' show TimeProvider; export 'src/sdk/trace/exporters/span_exporter.dart' show SpanExporter; -export 'src/sdk/trace/exporters/collector_exporter.dart' show CollectorExporter; +export 'src/sdk/trace/exporters/collector_exporter.dart' + show CollectorExporter, CollectorExporterProtocol; export 'src/sdk/trace/exporters/console_exporter.dart' show ConsoleExporter; export 'src/sdk/trace/id_generator.dart' show IdGenerator; export 'src/sdk/trace/sampling/always_off_sampler.dart' show AlwaysOffSampler; diff --git a/lib/src/sdk/proto/opentelemetry/proto/collector/trace/v1/trace_service.pb.dart b/lib/src/sdk/proto/opentelemetry/proto/collector/trace/v1/trace_service.pb.dart index 8555bf73..3a9fdb1e 100644 --- a/lib/src/sdk/proto/opentelemetry/proto/collector/trace/v1/trace_service.pb.dart +++ b/lib/src/sdk/proto/opentelemetry/proto/collector/trace/v1/trace_service.pb.dart @@ -1,6 +1,3 @@ -// Copyright 2021-2022 Workiva. -// Licensed under the Apache License, Version 2.0. Please see https://github.com/Workiva/opentelemetry-dart/blob/master/LICENSE for more information - // // Generated code. Do not modify. // source: opentelemetry/proto/collector/trace/v1/trace_service.proto @@ -12,17 +9,16 @@ // ignore_for_file: non_constant_identifier_names, prefer_final_fields // ignore_for_file: unnecessary_import, unnecessary_this, unused_import -import 'dart:async' as $async; import 'dart:core' as $core; import 'package:fixnum/fixnum.dart' as $fixnum; import 'package:protobuf/protobuf.dart' as $pb; -import '../../../trace/v1/trace.pb.dart' as $2; +import '../../../trace/v1/trace.pb.dart' as $6; class ExportTraceServiceRequest extends $pb.GeneratedMessage { factory ExportTraceServiceRequest({ - $core.Iterable<$2.ResourceSpans>? resourceSpans, + $core.Iterable<$6.ResourceSpans>? resourceSpans, }) { final $result = create(); if (resourceSpans != null) { @@ -35,7 +31,7 @@ class ExportTraceServiceRequest extends $pb.GeneratedMessage { factory ExportTraceServiceRequest.fromJson($core.String i, [$pb.ExtensionRegistry r = $pb.ExtensionRegistry.EMPTY]) => create()..mergeFromJson(i, r); static final $pb.BuilderInfo _i = $pb.BuilderInfo(_omitMessageNames ? '' : 'ExportTraceServiceRequest', package: const $pb.PackageName(_omitMessageNames ? '' : 'opentelemetry.proto.collector.trace.v1'), createEmptyInstance: create) - ..pc<$2.ResourceSpans>(1, _omitFieldNames ? '' : 'resourceSpans', $pb.PbFieldType.PM, subBuilder: $2.ResourceSpans.create) + ..pc<$6.ResourceSpans>(1, _omitFieldNames ? '' : 'resourceSpans', $pb.PbFieldType.PM, subBuilder: $6.ResourceSpans.create) ..hasRequiredFields = false ; @@ -66,7 +62,7 @@ class ExportTraceServiceRequest extends $pb.GeneratedMessage { /// data from multiple origins typically batch the data before forwarding further and /// in that case this array will contain multiple elements. @$pb.TagNumber(1) - $core.List<$2.ResourceSpans> get resourceSpans => $_getList(0); + $core.List<$6.ResourceSpans> get resourceSpans => $_getList(0); } class ExportTraceServiceResponse extends $pb.GeneratedMessage { @@ -211,15 +207,6 @@ class ExportTracePartialSuccess extends $pb.GeneratedMessage { void clearErrorMessage() => clearField(2); } -class TraceServiceApi { - $pb.RpcClient _client; - TraceServiceApi(this._client); - - $async.Future export($pb.ClientContext? ctx, ExportTraceServiceRequest request) => - _client.invoke(ctx, 'TraceService', 'Export', request, ExportTraceServiceResponse()) - ; -} - const _omitFieldNames = $core.bool.fromEnvironment('protobuf.omit_field_names'); const _omitMessageNames = $core.bool.fromEnvironment('protobuf.omit_message_names'); diff --git a/lib/src/sdk/proto/opentelemetry/proto/collector/trace/v1/trace_service.pbenum.dart b/lib/src/sdk/proto/opentelemetry/proto/collector/trace/v1/trace_service.pbenum.dart index 859bf978..4dba9078 100644 --- a/lib/src/sdk/proto/opentelemetry/proto/collector/trace/v1/trace_service.pbenum.dart +++ b/lib/src/sdk/proto/opentelemetry/proto/collector/trace/v1/trace_service.pbenum.dart @@ -1,6 +1,3 @@ -// Copyright 2021-2022 Workiva. -// Licensed under the Apache License, Version 2.0. Please see https://github.com/Workiva/opentelemetry-dart/blob/master/LICENSE for more information - // // Generated code. Do not modify. // source: opentelemetry/proto/collector/trace/v1/trace_service.proto diff --git a/lib/src/sdk/proto/opentelemetry/proto/collector/trace/v1/trace_service.pbgrpc.dart b/lib/src/sdk/proto/opentelemetry/proto/collector/trace/v1/trace_service.pbgrpc.dart new file mode 100644 index 00000000..a050fddd --- /dev/null +++ b/lib/src/sdk/proto/opentelemetry/proto/collector/trace/v1/trace_service.pbgrpc.dart @@ -0,0 +1,59 @@ +// +// Generated code. Do not modify. +// source: opentelemetry/proto/collector/trace/v1/trace_service.proto +// +// @dart = 2.12 + +// ignore_for_file: annotate_overrides, camel_case_types, comment_references +// ignore_for_file: constant_identifier_names, library_prefixes +// ignore_for_file: non_constant_identifier_names, prefer_final_fields +// ignore_for_file: unnecessary_import, unnecessary_this, unused_import + +import 'dart:async' as $async; +import 'dart:core' as $core; + +import 'package:grpc/service_api.dart' as $grpc; +import 'package:protobuf/protobuf.dart' as $pb; + +import 'trace_service.pb.dart' as $1; + +export 'trace_service.pb.dart'; + +@$pb.GrpcServiceName('opentelemetry.proto.collector.trace.v1.TraceService') +class TraceServiceClient extends $grpc.Client { + static final _$export = $grpc.ClientMethod<$1.ExportTraceServiceRequest, $1.ExportTraceServiceResponse>( + '/opentelemetry.proto.collector.trace.v1.TraceService/Export', + ($1.ExportTraceServiceRequest value) => value.writeToBuffer(), + ($core.List<$core.int> value) => $1.ExportTraceServiceResponse.fromBuffer(value)); + + TraceServiceClient($grpc.ClientChannel channel, + {$grpc.CallOptions? options, + $core.Iterable<$grpc.ClientInterceptor>? interceptors}) + : super(channel, options: options, + interceptors: interceptors); + + $grpc.ResponseFuture<$1.ExportTraceServiceResponse> export($1.ExportTraceServiceRequest request, {$grpc.CallOptions? options}) { + return $createUnaryCall(_$export, request, options: options); + } +} + +@$pb.GrpcServiceName('opentelemetry.proto.collector.trace.v1.TraceService') +abstract class TraceServiceBase extends $grpc.Service { + $core.String get $name => 'opentelemetry.proto.collector.trace.v1.TraceService'; + + TraceServiceBase() { + $addMethod($grpc.ServiceMethod<$1.ExportTraceServiceRequest, $1.ExportTraceServiceResponse>( + 'Export', + export_Pre, + false, + false, + ($core.List<$core.int> value) => $1.ExportTraceServiceRequest.fromBuffer(value), + ($1.ExportTraceServiceResponse value) => value.writeToBuffer())); + } + + $async.Future<$1.ExportTraceServiceResponse> export_Pre($grpc.ServiceCall call, $async.Future<$1.ExportTraceServiceRequest> request) async { + return export(call, await request); + } + + $async.Future<$1.ExportTraceServiceResponse> export($grpc.ServiceCall call, $1.ExportTraceServiceRequest request); +} diff --git a/lib/src/sdk/proto/opentelemetry/proto/collector/trace/v1/trace_service.pbjson.dart b/lib/src/sdk/proto/opentelemetry/proto/collector/trace/v1/trace_service.pbjson.dart index f2766c20..62aa021c 100644 --- a/lib/src/sdk/proto/opentelemetry/proto/collector/trace/v1/trace_service.pbjson.dart +++ b/lib/src/sdk/proto/opentelemetry/proto/collector/trace/v1/trace_service.pbjson.dart @@ -1,6 +1,3 @@ -// Copyright 2021-2022 Workiva. -// Licensed under the Apache License, Version 2.0. Please see https://github.com/Workiva/opentelemetry-dart/blob/master/LICENSE for more information - // // Generated code. Do not modify. // source: opentelemetry/proto/collector/trace/v1/trace_service.proto @@ -16,10 +13,6 @@ import 'dart:convert' as $convert; import 'dart:core' as $core; import 'dart:typed_data' as $typed_data; -import '../../../common/v1/common.pbjson.dart' as $0; -import '../../../resource/v1/resource.pbjson.dart' as $1; -import '../../../trace/v1/trace.pbjson.dart' as $2; - @$core.Deprecated('Use exportTraceServiceRequestDescriptor instead') const ExportTraceServiceRequest$json = { '1': 'ExportTraceServiceRequest', @@ -61,35 +54,3 @@ final $typed_data.Uint8List exportTracePartialSuccessDescriptor = $convert.base6 'ChlFeHBvcnRUcmFjZVBhcnRpYWxTdWNjZXNzEiUKDnJlamVjdGVkX3NwYW5zGAEgASgDUg1yZW' 'plY3RlZFNwYW5zEiMKDWVycm9yX21lc3NhZ2UYAiABKAlSDGVycm9yTWVzc2FnZQ=='); -const $core.Map<$core.String, $core.dynamic> TraceServiceBase$json = { - '1': 'TraceService', - '2': [ - {'1': 'Export', '2': '.opentelemetry.proto.collector.trace.v1.ExportTraceServiceRequest', '3': '.opentelemetry.proto.collector.trace.v1.ExportTraceServiceResponse', '4': {}}, - ], -}; - -@$core.Deprecated('Use traceServiceDescriptor instead') -const $core.Map<$core.String, $core.Map<$core.String, $core.dynamic>> TraceServiceBase$messageJson = { - '.opentelemetry.proto.collector.trace.v1.ExportTraceServiceRequest': ExportTraceServiceRequest$json, - '.opentelemetry.proto.trace.v1.ResourceSpans': $2.ResourceSpans$json, - '.opentelemetry.proto.resource.v1.Resource': $1.Resource$json, - '.opentelemetry.proto.common.v1.KeyValue': $0.KeyValue$json, - '.opentelemetry.proto.common.v1.AnyValue': $0.AnyValue$json, - '.opentelemetry.proto.common.v1.ArrayValue': $0.ArrayValue$json, - '.opentelemetry.proto.common.v1.KeyValueList': $0.KeyValueList$json, - '.opentelemetry.proto.trace.v1.ScopeSpans': $2.ScopeSpans$json, - '.opentelemetry.proto.common.v1.InstrumentationScope': $0.InstrumentationScope$json, - '.opentelemetry.proto.trace.v1.Span': $2.Span$json, - '.opentelemetry.proto.trace.v1.Span.Event': $2.Span_Event$json, - '.opentelemetry.proto.trace.v1.Span.Link': $2.Span_Link$json, - '.opentelemetry.proto.trace.v1.Status': $2.Status$json, - '.opentelemetry.proto.collector.trace.v1.ExportTraceServiceResponse': ExportTraceServiceResponse$json, - '.opentelemetry.proto.collector.trace.v1.ExportTracePartialSuccess': ExportTracePartialSuccess$json, -}; - -/// Descriptor for `TraceService`. Decode as a `google.protobuf.ServiceDescriptorProto`. -final $typed_data.Uint8List traceServiceDescriptor = $convert.base64Decode( - 'CgxUcmFjZVNlcnZpY2USkQEKBkV4cG9ydBJBLm9wZW50ZWxlbWV0cnkucHJvdG8uY29sbGVjdG' - '9yLnRyYWNlLnYxLkV4cG9ydFRyYWNlU2VydmljZVJlcXVlc3QaQi5vcGVudGVsZW1ldHJ5LnBy' - 'b3RvLmNvbGxlY3Rvci50cmFjZS52MS5FeHBvcnRUcmFjZVNlcnZpY2VSZXNwb25zZSIA'); - diff --git a/lib/src/sdk/proto/opentelemetry/proto/common/v1/common.pb.dart b/lib/src/sdk/proto/opentelemetry/proto/common/v1/common.pb.dart index b75293c2..a71de5ec 100644 --- a/lib/src/sdk/proto/opentelemetry/proto/common/v1/common.pb.dart +++ b/lib/src/sdk/proto/opentelemetry/proto/common/v1/common.pb.dart @@ -1,6 +1,3 @@ -// Copyright 2021-2022 Workiva. -// Licensed under the Apache License, Version 2.0. Please see https://github.com/Workiva/opentelemetry-dart/blob/master/LICENSE for more information - // // Generated code. Do not modify. // source: opentelemetry/proto/common/v1/common.proto diff --git a/lib/src/sdk/proto/opentelemetry/proto/common/v1/common.pbenum.dart b/lib/src/sdk/proto/opentelemetry/proto/common/v1/common.pbenum.dart index 0e704d5b..b9a6f7f4 100644 --- a/lib/src/sdk/proto/opentelemetry/proto/common/v1/common.pbenum.dart +++ b/lib/src/sdk/proto/opentelemetry/proto/common/v1/common.pbenum.dart @@ -1,6 +1,3 @@ -// Copyright 2021-2022 Workiva. -// Licensed under the Apache License, Version 2.0. Please see https://github.com/Workiva/opentelemetry-dart/blob/master/LICENSE for more information - // // Generated code. Do not modify. // source: opentelemetry/proto/common/v1/common.proto diff --git a/lib/src/sdk/proto/opentelemetry/proto/common/v1/common.pbjson.dart b/lib/src/sdk/proto/opentelemetry/proto/common/v1/common.pbjson.dart index 58efe690..f91c6dd7 100644 --- a/lib/src/sdk/proto/opentelemetry/proto/common/v1/common.pbjson.dart +++ b/lib/src/sdk/proto/opentelemetry/proto/common/v1/common.pbjson.dart @@ -1,6 +1,3 @@ -// Copyright 2021-2022 Workiva. -// Licensed under the Apache License, Version 2.0. Please see https://github.com/Workiva/opentelemetry-dart/blob/master/LICENSE for more information - // // Generated code. Do not modify. // source: opentelemetry/proto/common/v1/common.proto diff --git a/lib/src/sdk/proto/opentelemetry/proto/resource/v1/resource.pb.dart b/lib/src/sdk/proto/opentelemetry/proto/resource/v1/resource.pb.dart index 5013d4d8..5bbb66ef 100644 --- a/lib/src/sdk/proto/opentelemetry/proto/resource/v1/resource.pb.dart +++ b/lib/src/sdk/proto/opentelemetry/proto/resource/v1/resource.pb.dart @@ -1,6 +1,3 @@ -// Copyright 2021-2022 Workiva. -// Licensed under the Apache License, Version 2.0. Please see https://github.com/Workiva/opentelemetry-dart/blob/master/LICENSE for more information - // // Generated code. Do not modify. // source: opentelemetry/proto/resource/v1/resource.proto @@ -16,12 +13,12 @@ import 'dart:core' as $core; import 'package:protobuf/protobuf.dart' as $pb; -import '../../common/v1/common.pb.dart' as $0; +import '../../common/v1/common.pb.dart' as $3; /// Resource information. class Resource extends $pb.GeneratedMessage { factory Resource({ - $core.Iterable<$0.KeyValue>? attributes, + $core.Iterable<$3.KeyValue>? attributes, $core.int? droppedAttributesCount, }) { final $result = create(); @@ -38,7 +35,7 @@ class Resource extends $pb.GeneratedMessage { factory Resource.fromJson($core.String i, [$pb.ExtensionRegistry r = $pb.ExtensionRegistry.EMPTY]) => create()..mergeFromJson(i, r); static final $pb.BuilderInfo _i = $pb.BuilderInfo(_omitMessageNames ? '' : 'Resource', package: const $pb.PackageName(_omitMessageNames ? '' : 'opentelemetry.proto.resource.v1'), createEmptyInstance: create) - ..pc<$0.KeyValue>(1, _omitFieldNames ? '' : 'attributes', $pb.PbFieldType.PM, subBuilder: $0.KeyValue.create) + ..pc<$3.KeyValue>(1, _omitFieldNames ? '' : 'attributes', $pb.PbFieldType.PM, subBuilder: $3.KeyValue.create) ..a<$core.int>(2, _omitFieldNames ? '' : 'droppedAttributesCount', $pb.PbFieldType.OU3) ..hasRequiredFields = false ; @@ -68,7 +65,7 @@ class Resource extends $pb.GeneratedMessage { /// Attribute keys MUST be unique (it is not allowed to have more than one /// attribute with the same key). @$pb.TagNumber(1) - $core.List<$0.KeyValue> get attributes => $_getList(0); + $core.List<$3.KeyValue> get attributes => $_getList(0); /// dropped_attributes_count is the number of dropped attributes. If the value is 0, then /// no attributes were dropped. diff --git a/lib/src/sdk/proto/opentelemetry/proto/resource/v1/resource.pbenum.dart b/lib/src/sdk/proto/opentelemetry/proto/resource/v1/resource.pbenum.dart index 79afd6c4..09534d5b 100644 --- a/lib/src/sdk/proto/opentelemetry/proto/resource/v1/resource.pbenum.dart +++ b/lib/src/sdk/proto/opentelemetry/proto/resource/v1/resource.pbenum.dart @@ -1,6 +1,3 @@ -// Copyright 2021-2022 Workiva. -// Licensed under the Apache License, Version 2.0. Please see https://github.com/Workiva/opentelemetry-dart/blob/master/LICENSE for more information - // // Generated code. Do not modify. // source: opentelemetry/proto/resource/v1/resource.proto diff --git a/lib/src/sdk/proto/opentelemetry/proto/resource/v1/resource.pbjson.dart b/lib/src/sdk/proto/opentelemetry/proto/resource/v1/resource.pbjson.dart index 3f5a11b9..c4f0b3e9 100644 --- a/lib/src/sdk/proto/opentelemetry/proto/resource/v1/resource.pbjson.dart +++ b/lib/src/sdk/proto/opentelemetry/proto/resource/v1/resource.pbjson.dart @@ -1,6 +1,3 @@ -// Copyright 2021-2022 Workiva. -// Licensed under the Apache License, Version 2.0. Please see https://github.com/Workiva/opentelemetry-dart/blob/master/LICENSE for more information - // // Generated code. Do not modify. // source: opentelemetry/proto/resource/v1/resource.proto diff --git a/lib/src/sdk/proto/opentelemetry/proto/trace/v1/trace.pb.dart b/lib/src/sdk/proto/opentelemetry/proto/trace/v1/trace.pb.dart index 83f26615..611f06a2 100644 --- a/lib/src/sdk/proto/opentelemetry/proto/trace/v1/trace.pb.dart +++ b/lib/src/sdk/proto/opentelemetry/proto/trace/v1/trace.pb.dart @@ -1,6 +1,3 @@ -// Copyright 2021-2022 Workiva. -// Licensed under the Apache License, Version 2.0. Please see https://github.com/Workiva/opentelemetry-dart/blob/master/LICENSE for more information - // // Generated code. Do not modify. // source: opentelemetry/proto/trace/v1/trace.proto @@ -17,8 +14,8 @@ import 'dart:core' as $core; import 'package:fixnum/fixnum.dart' as $fixnum; import 'package:protobuf/protobuf.dart' as $pb; -import '../../common/v1/common.pb.dart' as $0; -import '../../resource/v1/resource.pb.dart' as $1; +import '../../common/v1/common.pb.dart' as $3; +import '../../resource/v1/resource.pb.dart' as $4; import 'trace.pbenum.dart'; export 'trace.pbenum.dart'; @@ -85,7 +82,7 @@ class TracesData extends $pb.GeneratedMessage { /// A collection of ScopeSpans from a Resource. class ResourceSpans extends $pb.GeneratedMessage { factory ResourceSpans({ - $1.Resource? resource, + $4.Resource? resource, $core.Iterable? scopeSpans, $core.String? schemaUrl, }) { @@ -106,7 +103,7 @@ class ResourceSpans extends $pb.GeneratedMessage { factory ResourceSpans.fromJson($core.String i, [$pb.ExtensionRegistry r = $pb.ExtensionRegistry.EMPTY]) => create()..mergeFromJson(i, r); static final $pb.BuilderInfo _i = $pb.BuilderInfo(_omitMessageNames ? '' : 'ResourceSpans', package: const $pb.PackageName(_omitMessageNames ? '' : 'opentelemetry.proto.trace.v1'), createEmptyInstance: create) - ..aOM<$1.Resource>(1, _omitFieldNames ? '' : 'resource', subBuilder: $1.Resource.create) + ..aOM<$4.Resource>(1, _omitFieldNames ? '' : 'resource', subBuilder: $4.Resource.create) ..pc(2, _omitFieldNames ? '' : 'scopeSpans', $pb.PbFieldType.PM, subBuilder: ScopeSpans.create) ..aOS(3, _omitFieldNames ? '' : 'schemaUrl') ..hasRequiredFields = false @@ -136,15 +133,15 @@ class ResourceSpans extends $pb.GeneratedMessage { /// The resource for the spans in this message. /// If this field is not set then no resource info is known. @$pb.TagNumber(1) - $1.Resource get resource => $_getN(0); + $4.Resource get resource => $_getN(0); @$pb.TagNumber(1) - set resource($1.Resource v) { setField(1, v); } + set resource($4.Resource v) { setField(1, v); } @$pb.TagNumber(1) $core.bool hasResource() => $_has(0); @$pb.TagNumber(1) void clearResource() => clearField(1); @$pb.TagNumber(1) - $1.Resource ensureResource() => $_ensure(0); + $4.Resource ensureResource() => $_ensure(0); /// A list of ScopeSpans that originate from a resource. @$pb.TagNumber(2) @@ -168,7 +165,7 @@ class ResourceSpans extends $pb.GeneratedMessage { /// A collection of Spans produced by an InstrumentationScope. class ScopeSpans extends $pb.GeneratedMessage { factory ScopeSpans({ - $0.InstrumentationScope? scope, + $3.InstrumentationScope? scope, $core.Iterable? spans, $core.String? schemaUrl, }) { @@ -189,7 +186,7 @@ class ScopeSpans extends $pb.GeneratedMessage { factory ScopeSpans.fromJson($core.String i, [$pb.ExtensionRegistry r = $pb.ExtensionRegistry.EMPTY]) => create()..mergeFromJson(i, r); static final $pb.BuilderInfo _i = $pb.BuilderInfo(_omitMessageNames ? '' : 'ScopeSpans', package: const $pb.PackageName(_omitMessageNames ? '' : 'opentelemetry.proto.trace.v1'), createEmptyInstance: create) - ..aOM<$0.InstrumentationScope>(1, _omitFieldNames ? '' : 'scope', subBuilder: $0.InstrumentationScope.create) + ..aOM<$3.InstrumentationScope>(1, _omitFieldNames ? '' : 'scope', subBuilder: $3.InstrumentationScope.create) ..pc(2, _omitFieldNames ? '' : 'spans', $pb.PbFieldType.PM, subBuilder: Span.create) ..aOS(3, _omitFieldNames ? '' : 'schemaUrl') ..hasRequiredFields = false @@ -220,15 +217,15 @@ class ScopeSpans extends $pb.GeneratedMessage { /// Semantically when InstrumentationScope isn't set, it is equivalent with /// an empty instrumentation scope name (unknown). @$pb.TagNumber(1) - $0.InstrumentationScope get scope => $_getN(0); + $3.InstrumentationScope get scope => $_getN(0); @$pb.TagNumber(1) - set scope($0.InstrumentationScope v) { setField(1, v); } + set scope($3.InstrumentationScope v) { setField(1, v); } @$pb.TagNumber(1) $core.bool hasScope() => $_has(0); @$pb.TagNumber(1) void clearScope() => clearField(1); @$pb.TagNumber(1) - $0.InstrumentationScope ensureScope() => $_ensure(0); + $3.InstrumentationScope ensureScope() => $_ensure(0); /// A list of Spans that originate from an instrumentation scope. @$pb.TagNumber(2) @@ -254,7 +251,7 @@ class Span_Event extends $pb.GeneratedMessage { factory Span_Event({ $fixnum.Int64? timeUnixNano, $core.String? name, - $core.Iterable<$0.KeyValue>? attributes, + $core.Iterable<$3.KeyValue>? attributes, $core.int? droppedAttributesCount, }) { final $result = create(); @@ -279,7 +276,7 @@ class Span_Event extends $pb.GeneratedMessage { static final $pb.BuilderInfo _i = $pb.BuilderInfo(_omitMessageNames ? '' : 'Span.Event', package: const $pb.PackageName(_omitMessageNames ? '' : 'opentelemetry.proto.trace.v1'), createEmptyInstance: create) ..a<$fixnum.Int64>(1, _omitFieldNames ? '' : 'timeUnixNano', $pb.PbFieldType.OF6, defaultOrMaker: $fixnum.Int64.ZERO) ..aOS(2, _omitFieldNames ? '' : 'name') - ..pc<$0.KeyValue>(3, _omitFieldNames ? '' : 'attributes', $pb.PbFieldType.PM, subBuilder: $0.KeyValue.create) + ..pc<$3.KeyValue>(3, _omitFieldNames ? '' : 'attributes', $pb.PbFieldType.PM, subBuilder: $3.KeyValue.create) ..a<$core.int>(4, _omitFieldNames ? '' : 'droppedAttributesCount', $pb.PbFieldType.OU3) ..hasRequiredFields = false ; @@ -330,7 +327,7 @@ class Span_Event extends $pb.GeneratedMessage { /// Attribute keys MUST be unique (it is not allowed to have more than one /// attribute with the same key). @$pb.TagNumber(3) - $core.List<$0.KeyValue> get attributes => $_getList(2); + $core.List<$3.KeyValue> get attributes => $_getList(2); /// dropped_attributes_count is the number of dropped attributes. If the value is 0, /// then no attributes were dropped. @@ -353,7 +350,7 @@ class Span_Link extends $pb.GeneratedMessage { $core.List<$core.int>? traceId, $core.List<$core.int>? spanId, $core.String? traceState, - $core.Iterable<$0.KeyValue>? attributes, + $core.Iterable<$3.KeyValue>? attributes, $core.int? droppedAttributesCount, $core.int? flags, }) { @@ -386,7 +383,7 @@ class Span_Link extends $pb.GeneratedMessage { ..a<$core.List<$core.int>>(1, _omitFieldNames ? '' : 'traceId', $pb.PbFieldType.OY) ..a<$core.List<$core.int>>(2, _omitFieldNames ? '' : 'spanId', $pb.PbFieldType.OY) ..aOS(3, _omitFieldNames ? '' : 'traceState') - ..pc<$0.KeyValue>(4, _omitFieldNames ? '' : 'attributes', $pb.PbFieldType.PM, subBuilder: $0.KeyValue.create) + ..pc<$3.KeyValue>(4, _omitFieldNames ? '' : 'attributes', $pb.PbFieldType.PM, subBuilder: $3.KeyValue.create) ..a<$core.int>(5, _omitFieldNames ? '' : 'droppedAttributesCount', $pb.PbFieldType.OU3) ..a<$core.int>(6, _omitFieldNames ? '' : 'flags', $pb.PbFieldType.OF3) ..hasRequiredFields = false @@ -448,7 +445,7 @@ class Span_Link extends $pb.GeneratedMessage { /// Attribute keys MUST be unique (it is not allowed to have more than one /// attribute with the same key). @$pb.TagNumber(4) - $core.List<$0.KeyValue> get attributes => $_getList(3); + $core.List<$3.KeyValue> get attributes => $_getList(3); /// dropped_attributes_count is the number of dropped attributes. If the value is 0, /// then no attributes were dropped. @@ -461,14 +458,23 @@ class Span_Link extends $pb.GeneratedMessage { @$pb.TagNumber(5) void clearDroppedAttributesCount() => clearField(5); - /// Flags, a bit field. 8 least significant bits are the trace - /// flags as defined in W3C Trace Context specification. Readers - /// MUST not assume that 24 most significant bits will be zero. - /// When creating new spans, the most-significant 24-bits MUST be - /// zero. To read the 8-bit W3C trace flag (use flags & - /// SPAN_FLAGS_TRACE_FLAGS_MASK). [Optional]. + /// Flags, a bit field. + /// + /// Bits 0-7 (8 least significant bits) are the trace flags as defined in W3C Trace + /// Context specification. To read the 8-bit W3C trace flag, use + /// `flags & SPAN_FLAGS_TRACE_FLAGS_MASK`. /// /// See https://www.w3.org/TR/trace-context-2/#trace-flags for the flag definitions. + /// + /// Bits 8 and 9 represent the 3 states of whether the link is remote. + /// The states are (unknown, is not remote, is remote). + /// To read whether the value is known, use `(flags & SPAN_FLAGS_CONTEXT_HAS_IS_REMOTE_MASK) != 0`. + /// To read whether the link is remote, use `(flags & SPAN_FLAGS_CONTEXT_IS_REMOTE_MASK) != 0`. + /// + /// Readers MUST NOT assume that bits 10-31 (22 most significant bits) will be zero. + /// When creating new spans, bits 10-31 (most-significant 22-bits) MUST be zero. + /// + /// [Optional]. @$pb.TagNumber(6) $core.int get flags => $_getIZ(5); @$pb.TagNumber(6) @@ -492,7 +498,7 @@ class Span extends $pb.GeneratedMessage { Span_SpanKind? kind, $fixnum.Int64? startTimeUnixNano, $fixnum.Int64? endTimeUnixNano, - $core.Iterable<$0.KeyValue>? attributes, + $core.Iterable<$3.KeyValue>? attributes, $core.int? droppedAttributesCount, $core.Iterable? events, $core.int? droppedEventsCount, @@ -565,7 +571,7 @@ class Span extends $pb.GeneratedMessage { ..e(6, _omitFieldNames ? '' : 'kind', $pb.PbFieldType.OE, defaultOrMaker: Span_SpanKind.SPAN_KIND_UNSPECIFIED, valueOf: Span_SpanKind.valueOf, enumValues: Span_SpanKind.values) ..a<$fixnum.Int64>(7, _omitFieldNames ? '' : 'startTimeUnixNano', $pb.PbFieldType.OF6, defaultOrMaker: $fixnum.Int64.ZERO) ..a<$fixnum.Int64>(8, _omitFieldNames ? '' : 'endTimeUnixNano', $pb.PbFieldType.OF6, defaultOrMaker: $fixnum.Int64.ZERO) - ..pc<$0.KeyValue>(9, _omitFieldNames ? '' : 'attributes', $pb.PbFieldType.PM, subBuilder: $0.KeyValue.create) + ..pc<$3.KeyValue>(9, _omitFieldNames ? '' : 'attributes', $pb.PbFieldType.PM, subBuilder: $3.KeyValue.create) ..a<$core.int>(10, _omitFieldNames ? '' : 'droppedAttributesCount', $pb.PbFieldType.OU3) ..pc(11, _omitFieldNames ? '' : 'events', $pb.PbFieldType.PM, subBuilder: Span_Event.create) ..a<$core.int>(12, _omitFieldNames ? '' : 'droppedEventsCount', $pb.PbFieldType.OU3) @@ -725,7 +731,7 @@ class Span extends $pb.GeneratedMessage { /// Attribute keys MUST be unique (it is not allowed to have more than one /// attribute with the same key). @$pb.TagNumber(9) - $core.List<$0.KeyValue> get attributes => $_getList(8); + $core.List<$3.KeyValue> get attributes => $_getList(8); /// 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 @@ -783,20 +789,27 @@ class Span extends $pb.GeneratedMessage { @$pb.TagNumber(15) Status ensureStatus() => $_ensure(14); - /// Flags, a bit field. 8 least significant bits are the trace - /// flags as defined in W3C Trace Context specification. Readers - /// MUST not assume that 24 most significant bits will be zero. - /// To read the 8-bit W3C trace flag, use `flags & SPAN_FLAGS_TRACE_FLAGS_MASK`. + /// Flags, a bit field. + /// + /// Bits 0-7 (8 least significant bits) are the trace flags as defined in W3C Trace + /// Context specification. To read the 8-bit W3C trace flag, use + /// `flags & SPAN_FLAGS_TRACE_FLAGS_MASK`. + /// + /// See https://www.w3.org/TR/trace-context-2/#trace-flags for the flag definitions. + /// + /// Bits 8 and 9 represent the 3 states of whether a span's parent + /// is remote. The states are (unknown, is not remote, is remote). + /// To read whether the value is known, use `(flags & SPAN_FLAGS_CONTEXT_HAS_IS_REMOTE_MASK) != 0`. + /// To read whether the span is remote, use `(flags & SPAN_FLAGS_CONTEXT_IS_REMOTE_MASK) != 0`. /// /// When creating span messages, if the message is logically forwarded from another source /// with an equivalent flags fields (i.e., usually another OTLP span message), the field SHOULD /// be copied as-is. If creating from a source that does not have an equivalent flags field - /// (such as a runtime representation of an OpenTelemetry span), the high 24 bits MUST + /// (such as a runtime representation of an OpenTelemetry span), the high 22 bits MUST /// be set to zero. + /// Readers MUST NOT assume that bits 10-31 (22 most significant bits) will be zero. /// /// [Optional]. - /// - /// See https://www.w3.org/TR/trace-context-2/#trace-flags for the flag definitions. @$pb.TagNumber(16) $core.int get flags => $_getIZ(15); @$pb.TagNumber(16) diff --git a/lib/src/sdk/proto/opentelemetry/proto/trace/v1/trace.pbenum.dart b/lib/src/sdk/proto/opentelemetry/proto/trace/v1/trace.pbenum.dart index 26638460..f382400b 100644 --- a/lib/src/sdk/proto/opentelemetry/proto/trace/v1/trace.pbenum.dart +++ b/lib/src/sdk/proto/opentelemetry/proto/trace/v1/trace.pbenum.dart @@ -1,6 +1,3 @@ -// Copyright 2021-2022 Workiva. -// Licensed under the Apache License, Version 2.0. Please see https://github.com/Workiva/opentelemetry-dart/blob/master/LICENSE for more information - // // Generated code. Do not modify. // source: opentelemetry/proto/trace/v1/trace.proto @@ -33,10 +30,14 @@ import 'package:protobuf/protobuf.dart' as $pb; class SpanFlags extends $pb.ProtobufEnum { static const SpanFlags SPAN_FLAGS_DO_NOT_USE = SpanFlags._(0, _omitEnumNames ? '' : 'SPAN_FLAGS_DO_NOT_USE'); static const SpanFlags SPAN_FLAGS_TRACE_FLAGS_MASK = SpanFlags._(255, _omitEnumNames ? '' : 'SPAN_FLAGS_TRACE_FLAGS_MASK'); + static const SpanFlags SPAN_FLAGS_CONTEXT_HAS_IS_REMOTE_MASK = SpanFlags._(256, _omitEnumNames ? '' : 'SPAN_FLAGS_CONTEXT_HAS_IS_REMOTE_MASK'); + static const SpanFlags SPAN_FLAGS_CONTEXT_IS_REMOTE_MASK = SpanFlags._(512, _omitEnumNames ? '' : 'SPAN_FLAGS_CONTEXT_IS_REMOTE_MASK'); static const $core.List values = [ SPAN_FLAGS_DO_NOT_USE, SPAN_FLAGS_TRACE_FLAGS_MASK, + SPAN_FLAGS_CONTEXT_HAS_IS_REMOTE_MASK, + SPAN_FLAGS_CONTEXT_IS_REMOTE_MASK, ]; static final $core.Map<$core.int, SpanFlags> _byValue = $pb.ProtobufEnum.initByValue(values); diff --git a/lib/src/sdk/proto/opentelemetry/proto/trace/v1/trace.pbjson.dart b/lib/src/sdk/proto/opentelemetry/proto/trace/v1/trace.pbjson.dart index b0b05c45..9b06e5e2 100644 --- a/lib/src/sdk/proto/opentelemetry/proto/trace/v1/trace.pbjson.dart +++ b/lib/src/sdk/proto/opentelemetry/proto/trace/v1/trace.pbjson.dart @@ -1,6 +1,3 @@ -// Copyright 2021-2022 Workiva. -// Licensed under the Apache License, Version 2.0. Please see https://github.com/Workiva/opentelemetry-dart/blob/master/LICENSE for more information - // // Generated code. Do not modify. // source: opentelemetry/proto/trace/v1/trace.proto @@ -22,13 +19,16 @@ const SpanFlags$json = { '2': [ {'1': 'SPAN_FLAGS_DO_NOT_USE', '2': 0}, {'1': 'SPAN_FLAGS_TRACE_FLAGS_MASK', '2': 255}, + {'1': 'SPAN_FLAGS_CONTEXT_HAS_IS_REMOTE_MASK', '2': 256}, + {'1': 'SPAN_FLAGS_CONTEXT_IS_REMOTE_MASK', '2': 512}, ], }; /// Descriptor for `SpanFlags`. Decode as a `google.protobuf.EnumDescriptorProto`. final $typed_data.Uint8List spanFlagsDescriptor = $convert.base64Decode( 'CglTcGFuRmxhZ3MSGQoVU1BBTl9GTEFHU19ET19OT1RfVVNFEAASIAobU1BBTl9GTEFHU19UUk' - 'FDRV9GTEFHU19NQVNLEP8B'); + 'FDRV9GTEFHU19NQVNLEP8BEioKJVNQQU5fRkxBR1NfQ09OVEVYVF9IQVNfSVNfUkVNT1RFX01B' + 'U0sQgAISJgohU1BBTl9GTEFHU19DT05URVhUX0lTX1JFTU9URV9NQVNLEIAE'); @$core.Deprecated('Use tracesDataDescriptor instead') const TracesData$json = { diff --git a/lib/src/sdk/trace/exporters/collector_exporter.dart b/lib/src/sdk/trace/exporters/collector_exporter.dart index 07f12ffd..2be6099d 100644 --- a/lib/src/sdk/trace/exporters/collector_exporter.dart +++ b/lib/src/sdk/trace/exporters/collector_exporter.dart @@ -6,6 +6,7 @@ import 'dart:async'; import 'package:fixnum/fixnum.dart'; import 'package:http/http.dart' as http; import 'package:logging/logging.dart'; +import 'package:grpc/grpc.dart'; import '../../../../api.dart' as api; import '../../../../sdk.dart' as sdk; @@ -15,18 +16,36 @@ import '../../proto/opentelemetry/proto/common/v1/common.pb.dart' as pb_common; import '../../proto/opentelemetry/proto/resource/v1/resource.pb.dart' as pb_resource; import '../../proto/opentelemetry/proto/trace/v1/trace.pb.dart' as pb_trace; +import '../../proto/opentelemetry/proto/collector/trace/v1/trace_service.pbgrpc.dart' + as pb_trace_service_grpc; + +enum CollectorExporterProtocol { httpProtobuf, gRPC } class CollectorExporter implements sdk.SpanExporter { final Logger _log = Logger('opentelemetry.CollectorExporter'); + final CollectorExporterProtocol protocol; final Uri uri; final http.Client client; final Map headers; var _isShutdown = false; + late pb_trace_service_grpc.TraceServiceClient _clientStub; - CollectorExporter(this.uri, - {http.Client? httpClient, this.headers = const {}}) - : client = httpClient ?? http.Client(); + CollectorExporter( + this.uri, { + http.Client? httpClient, + this.headers = const {}, + this.protocol = CollectorExporterProtocol.httpProtobuf, + }) : client = httpClient ?? http.Client() { + if (protocol == CollectorExporterProtocol.gRPC) { + _clientStub = pb_trace_service_grpc.TraceServiceClient( + ClientChannel( + uri.host, + port: uri.port, + ), + ); + } + } @override void export(List spans) { @@ -48,10 +67,24 @@ class CollectorExporter implements sdk.SpanExporter { try { final body = pb_trace_service.ExportTraceServiceRequest( resourceSpans: _spansToProtobuf(spans)); - final headers = {'Content-Type': 'application/x-protobuf'} - ..addAll(this.headers); - await client.post(uri, body: body.writeToBuffer(), headers: headers); + switch (protocol) { + case CollectorExporterProtocol.gRPC: + await _clientStub.export( + body, + options: CallOptions( + metadata: headers, + ), + ); + break; + + case CollectorExporterProtocol.httpProtobuf: + final headers = {'Content-Type': 'application/x-protobuf'} + ..addAll(this.headers); + + await client.post(uri, body: body.writeToBuffer(), headers: headers); + break; + } } catch (e) { _log.warning('Failed to export ${spans.length} spans.', e); } diff --git a/pubspec.yaml b/pubspec.yaml index 80a56d4a..f2013e4a 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,19 +1,20 @@ name: opentelemetry -version: 0.18.1 +version: 0.18.1-grpc description: A framework for collecting traces from applications. homepage: https://github.com/Workiva/opentelemetry-dart environment: sdk: ">=2.19.0 <4.0.0" dependencies: - async: '>=2.5.0 <3.0.0' + async: ">=2.5.0 <3.0.0" collection: ^1.15.0 - fixnum: '>=1.0.0 <2.0.0' + fixnum: ">=1.0.0 <2.0.0" + grpc: ^3.2.4 http: ">=0.13.0 <2.0.0" logging: ^1.0.0 - meta: '>=1.6.0 <2.0.0' + meta: ">=1.6.0 <2.0.0" protobuf: ">=2.0.0 <4.0.0" - quiver: '>=3.0.0 <4.0.0' + quiver: ">=3.0.0 <4.0.0" dev_dependencies: build_runner: ^2.3.3