Skip to content
This repository was archived by the owner on Dec 18, 2023. It is now read-only.

Commit 452dcde

Browse files
got rid of Console.WriteLine (#89)
* got rid of Console.WriteLine * addressed PR feedback - mostly rename StackDriver to stackdriver
1 parent 5ba6f20 commit 452dcde

File tree

8 files changed

+152
-13
lines changed

8 files changed

+152
-13
lines changed

src/OpenCensus.Exporter.ApplicationInsights/Implementation/MetricsExporterThread.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,6 @@ internal void Export()
190190
});
191191
this.telemetryClient.TrackMetric(metricTelemetry);
192192
}
193-
194-
Console.WriteLine(view);
195-
Console.WriteLine(data);
196193
}
197194
}
198195
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// <copyright file="ExporterOcagentEventSource.cs" company="OpenCensus Authors">
2+
// Copyright 2018, OpenCensus Authors
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
// </copyright>
16+
17+
namespace OpenCensus.Exporter.Ocagent.Implementation
18+
{
19+
using System;
20+
using System.Diagnostics.Tracing;
21+
using System.Globalization;
22+
using System.Threading;
23+
24+
[EventSource(Name = "OpenCensus-Exporter-Ocagent")]
25+
internal class ExporterOcagentEventSource : EventSource
26+
{
27+
public static readonly ExporterOcagentEventSource Log = new ExporterOcagentEventSource();
28+
29+
[NonEvent]
30+
public void FailedToConvertToProtoDefinitionError(Exception ex)
31+
{
32+
if (Log.IsEnabled(EventLevel.Error, EventKeywords.All))
33+
{
34+
this.FailedToConvertToProtoDefinitionError(ToInvariantString(ex));
35+
}
36+
}
37+
38+
[Event(1, Message = "Exporter failed to convert SpanData content into GRPC proto definition. Data will not be sent. Exception: {0}", Level = EventLevel.Error)]
39+
public void FailedToConvertToProtoDefinitionError(string ex)
40+
{
41+
this.WriteEvent(1, ex);
42+
}
43+
44+
/// <summary>
45+
/// Returns a culture-independent string representation of the given <paramref name="exception"/> object,
46+
/// appropriate for diagnostics tracing.
47+
/// </summary>
48+
private static string ToInvariantString(Exception exception)
49+
{
50+
CultureInfo originalUICulture = Thread.CurrentThread.CurrentUICulture;
51+
52+
try
53+
{
54+
Thread.CurrentThread.CurrentUICulture = CultureInfo.InvariantCulture;
55+
return exception.ToString();
56+
}
57+
finally
58+
{
59+
Thread.CurrentThread.CurrentUICulture = originalUICulture;
60+
}
61+
}
62+
}
63+
}

src/OpenCensus.Exporter.Ocagent/Implementation/SpanDataExtentions.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,11 @@ internal static Span ToProtoSpan(this ISpanData spanData)
7373
}
7474
catch (Exception e)
7575
{
76-
Console.WriteLine(e);
77-
78-
// TODO: log
76+
// TODO: Is there a way to handle this better?
77+
// This type of error processing is very aggressive and doesn't follow the
78+
// error handling practices when smart defaults should be used when possible.
79+
// See: https://github.com/census-instrumentation/opencensus-csharp/blob/develop/docs/error-handling.md
80+
ExporterOcagentEventSource.Log.FailedToConvertToProtoDefinitionError(e);
7981
}
8082

8183
return null;
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// <copyright file="ExporterStackdriverEventSource.cs" company="OpenCensus Authors">
2+
// Copyright 2018, OpenCensus Authors
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
// </copyright>
16+
17+
namespace OpenCensus.Exporter.Stackdriver.Implementation
18+
{
19+
using System;
20+
using System.Diagnostics.Tracing;
21+
using System.Globalization;
22+
using System.Threading;
23+
24+
[EventSource(Name = "OpenCensus-Exporter-Stackdriver")]
25+
internal class ExporterStackdriverEventSource : EventSource
26+
{
27+
public static readonly ExporterStackdriverEventSource Log = new ExporterStackdriverEventSource();
28+
29+
[NonEvent]
30+
public void UnknownProblemInWorkerThreadError(Exception ex)
31+
{
32+
if (Log.IsEnabled(EventLevel.Error, EventKeywords.All))
33+
{
34+
this.UnknownProblemInWorkerThreadError(ToInvariantString(ex));
35+
}
36+
}
37+
38+
[Event(1, Message = "Stackdriver exporter encountered an unknown error and will shut down. Exception: {0}", Level = EventLevel.Error)]
39+
public void UnknownProblemInWorkerThreadError(string ex)
40+
{
41+
this.WriteEvent(1, ex);
42+
}
43+
44+
[NonEvent]
45+
public void UnknownProblemWhileCreatingStackdriverTimeSeriesError(Exception ex)
46+
{
47+
if (Log.IsEnabled(EventLevel.Error, EventKeywords.All))
48+
{
49+
this.UnknownProblemWhileCreatingStackdriverTimeSeriesError(ToInvariantString(ex));
50+
}
51+
}
52+
53+
[Event(2, Message = "Stackdriver exporter failed to create time series. Time series will be lost. Exception: {0}", Level = EventLevel.Error)]
54+
public void UnknownProblemWhileCreatingStackdriverTimeSeriesError(string ex)
55+
{
56+
this.WriteEvent(2, ex);
57+
}
58+
59+
/// <summary>
60+
/// Returns a culture-independent string representation of the given <paramref name="exception"/> object,
61+
/// appropriate for diagnostics tracing.
62+
/// </summary>
63+
private static string ToInvariantString(Exception exception)
64+
{
65+
CultureInfo originalUICulture = Thread.CurrentThread.CurrentUICulture;
66+
67+
try
68+
{
69+
Thread.CurrentThread.CurrentUICulture = CultureInfo.InvariantCulture;
70+
return exception.ToString();
71+
}
72+
finally
73+
{
74+
Thread.CurrentThread.CurrentUICulture = originalUICulture;
75+
}
76+
}
77+
}
78+
}

src/OpenCensus.Exporter.Stackdriver/Implementation/MetricsConversions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ public static TypedValue CreateTypedValue(
176176
private static IList<long> CreateBucketCounts(IList<long> bucketCounts)
177177
{
178178
// The first bucket (underflow bucket) should always be 0 count because the Metrics first bucket
179-
// is [0, first_bound) but StackDriver distribution consists of an underflow bucket (number 0).
179+
// is [0, first_bound) but Stackdriver distribution consists of an underflow bucket (number 0).
180180
var ret = new List<long>();
181181
ret.Add(0L);
182182
ret.AddRange(bucketCounts);

src/OpenCensus.Exporter.Stackdriver/Implementation/StackdriverStatsExporter.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ private void DoWork()
170170
}
171171
catch (Exception ex)
172172
{
173-
Console.WriteLine(ex);
173+
ExporterStackdriverEventSource.Log.UnknownProblemInWorkerThreadError(ex);
174174
}
175175
}
176176

@@ -267,8 +267,7 @@ private void Export()
267267
}
268268
catch (RpcException e)
269269
{
270-
// TODO - zeltser - figure out where to send the error from exception
271-
Console.WriteLine(e);
270+
ExporterStackdriverEventSource.Log.UnknownProblemWhileCreatingStackdriverTimeSeriesError(e);
272271
}
273272
}
274273
}

src/OpenCensus.Exporter.Stackdriver/Implementation/StackdriverTraceExporter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ public StackdriverTraceExporter(string projectId)
155155

156156
// Set header mutation for every outgoing API call to Stackdriver so the BE knows
157157
// which version of OC client is calling it as well as which version of the exporter
158-
CallSettings callSettings = CallSettings.FromHeaderMutation(StackDriverCallHeaderAppender);
158+
CallSettings callSettings = CallSettings.FromHeaderMutation(StackdriverCallHeaderAppender);
159159
traceServiceSettings = new TraceServiceSettings();
160160
traceServiceSettings.CallSettings = callSettings;
161161
}
@@ -199,7 +199,7 @@ public void Export(IEnumerable<ISpanData> spanDataList)
199199
/// Appends OpenCensus headers for every outgoing request to Stackdriver Backend
200200
/// </summary>
201201
/// <param name="metadata">The metadata that is sent with every outgoing http request</param>
202-
private static void StackDriverCallHeaderAppender(Metadata metadata)
202+
private static void StackdriverCallHeaderAppender(Metadata metadata)
203203
{
204204

205205
metadata.Add("AGENT_LABEL_KEY", "g.co/agent");

src/OpenCensus/Implementation/OpenCensusEventSource.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public void FailedReadingEnvironmentVariableWarning(string environmentVariableNa
6363
}
6464

6565
/// <summary>
66-
/// Returns a culture-independent string representation of the given <paramref name="exception"/> object,
66+
/// Returns a culture-independent string representation of the given <paramref name="exception"/> object,
6767
/// appropriate for diagnostics tracing.
6868
/// </summary>
6969
private static string ToInvariantString(Exception exception)

0 commit comments

Comments
 (0)