15
15
"""OTLP Exporter"""
16
16
17
17
import logging
18
+ import os
18
19
from abc import ABC , abstractmethod
19
20
from collections .abc import Mapping , Sequence
20
21
from time import sleep
30
31
StatusCode ,
31
32
insecure_channel ,
32
33
secure_channel ,
34
+ ssl_channel_credentials ,
33
35
)
34
36
37
+ from opentelemetry .configuration import Configuration
35
38
from opentelemetry .proto .common .v1 .common_pb2 import AnyValue , KeyValue
36
39
from opentelemetry .proto .resource .v1 .resource_pb2 import Resource
37
40
from opentelemetry .sdk .resources import Resource as SDKResource
@@ -113,6 +116,16 @@ def _get_resource_data(
113
116
return resource_data
114
117
115
118
119
+ def _load_credential_from_file (filepath ) -> ChannelCredentials :
120
+ try :
121
+ with open (filepath , "rb" ) as f :
122
+ credential = f .read ()
123
+ return ssl_channel_credentials (credential )
124
+ except FileNotFoundError :
125
+ logger .exception ("Failed to read credential file" )
126
+ return None
127
+
128
+
116
129
# pylint: disable=no-member
117
130
class OTLPExporterMixin (
118
131
ABC , Generic [SDKDataT , ExportServiceRequestT , ExportResultT ]
@@ -121,24 +134,47 @@ class OTLPExporterMixin(
121
134
122
135
Args:
123
136
endpoint: OpenTelemetry Collector receiver endpoint
137
+ insecure: Connection type
124
138
credentials: ChannelCredentials object for server authentication
125
139
metadata: Metadata to send when exporting
140
+ timeout: Backend request timeout in seconds
126
141
"""
127
142
128
143
def __init__ (
129
144
self ,
130
- endpoint : str = "localhost:55680" ,
131
- credentials : ChannelCredentials = None ,
132
- metadata : Optional [Tuple [Any ]] = None ,
145
+ endpoint : Optional [str ] = None ,
146
+ insecure : Optional [bool ] = None ,
147
+ credentials : Optional [ChannelCredentials ] = None ,
148
+ headers : Optional [str ] = None ,
149
+ timeout : Optional [int ] = None ,
133
150
):
134
151
super ().__init__ ()
135
152
136
- self ._metadata = metadata
153
+ endpoint = (
154
+ endpoint
155
+ or Configuration ().EXPORTER_OTLP_ENDPOINT
156
+ or "localhost:55680"
157
+ )
158
+
159
+ if insecure is None :
160
+ insecure = Configuration ().EXPORTER_OTLP_INSECURE
161
+ if insecure is None :
162
+ insecure = False
163
+
164
+ self ._headers = headers or Configuration ().EXPORTER_OTLP_HEADERS
165
+ self ._timeout = (
166
+ timeout
167
+ or Configuration ().EXPORTER_OTLP_TIMEOUT
168
+ or 10 # default: 10 seconds
169
+ )
137
170
self ._collector_span_kwargs = None
138
171
139
- if credentials is None :
172
+ if insecure :
140
173
self ._client = self ._stub (insecure_channel (endpoint ))
141
174
else :
175
+ credentials = credentials or _load_credential_from_file (
176
+ Configuration ().EXPORTER_OTLP_CERTIFICATE
177
+ )
142
178
self ._client = self ._stub (secure_channel (endpoint , credentials ))
143
179
144
180
@abstractmethod
@@ -164,7 +200,8 @@ def _export(self, data: TypingSequence[SDKDataT]) -> ExportResultT:
164
200
try :
165
201
self ._client .Export (
166
202
request = self ._translate_data (data ),
167
- metadata = self ._metadata ,
203
+ metadata = self ._headers ,
204
+ timeout = self ._timeout ,
168
205
)
169
206
170
207
return self ._result .SUCCESS
0 commit comments