Replace span labels with StackDriver versions (#453)

* replace span labels with StackDriver versions

* fix typo in SpanExtensions

Co-Authored-By: Nicolai Brogaard <nmb@minserver.dk>

* fix typo in SpanExtensions

Co-Authored-By: Nicolai Brogaard <nmb@minserver.dk>

* add label conversion from http.path to /http/url

Co-authored-by: Nicolai Brogaard <nmb@minserver.dk>
Co-authored-by: Sergey Kanzhelev <S.Kanzhelev@live.com>
This commit is contained in:
Mike Goldsmith 2020-01-21 23:15:30 +00:00 committed by Sergey Kanzhelev
parent 77e8510e61
commit cb0753d077
2 changed files with 29 additions and 1 deletions

View File

@ -40,9 +40,15 @@ namespace Samples
using (DistributedContext.SetCurrent(dc))
{
using (tracer.StartActiveSpan("incoming request", out var span))
using (tracer.StartActiveSpan("/getuser", out ISpan span))
{
span.AddEvent("Processing video.");
span.PutHttpMethodAttribute("GET");
span.PutHttpHostAttribute("localhost", 8080);
span.PutHttpPathAttribute("/resource");
span.PutHttpStatusCodeAttribute(200);
span.PutHttpUserAgentAttribute("Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0");
Thread.Sleep(TimeSpan.FromMilliseconds(10));
}
}

View File

@ -13,6 +13,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
using System.Collections.Generic;
using System.Linq;
using Google.Cloud.Trace.V2;
using Google.Protobuf.WellKnownTypes;
@ -23,6 +25,15 @@ namespace OpenTelemetry.Exporter.Stackdriver.Implementation
{
internal static class SpanExtensions
{
private static Dictionary<string, string> httpLabelsToReplace = new Dictionary<string, string>
{
{ "http.method", "/http/method" },
{ "http.host", "/http/host" },
{ "http.status_code", "/http/status_code" },
{ "http.user_agent", "/agent" },
{ "http.path", "/http/url" },
};
/// <summary>
/// Translating <see cref="SpanData"/> to Stackdriver's Span
/// According to <see href="https://cloud.google.com/trace/docs/reference/v2/rpc/google.devtools.cloudtrace.v2"/> specifications.
@ -76,6 +87,17 @@ namespace OpenTelemetry.Exporter.Stackdriver.Implementation
};
}
// StackDriver uses different labels that are used to categorize spans
// replace attribute keys with StackDriver version
foreach (var entry in httpLabelsToReplace)
{
if (span.Attributes.AttributeMap.TryGetValue(entry.Key, out var attrValue))
{
span.Attributes.AttributeMap.Remove(entry.Key);
span.Attributes.AttributeMap.Add(entry.Value, attrValue);
}
}
return span;
}