-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[receiver/azureeventhubreceiver]Implemented receiver to support traces (
#33583) **Description:** Implemented `azureeventhubreceiver` to support traces <!--Ex. Fixing a bug - Describe the bug and how this fixes the issue. Ex. Adding a feature - Explain what this achieves.--> **Link to tracking Issue:** <Issue number if applicable> **Testing:** <Describe what testing was performed and which tests were added.> **Documentation:** Added information in to `README.md`
- Loading branch information
Showing
10 changed files
with
344 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Use this changelog template to create an entry for release notes. | ||
|
||
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' | ||
change_type: enhancement | ||
|
||
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) | ||
component: azureeventhubreceiver | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: Added traces support in azureeventhubreceiver | ||
|
||
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
issues: [33583] | ||
|
||
# (Optional) One or more lines of additional information to render under the primary note. | ||
# These lines will be padded with 2 spaces and then inserted directly into the document. | ||
# Use pipe (|) for multiline entries. | ||
subtext: | ||
|
||
# If your change doesn't affect end users or the exported elements of any package, | ||
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. | ||
# Optional: The change log or logs in which this entry should be included. | ||
# e.g. '[user]' or '[user, api]' | ||
# Include 'user' if the change is relevant to end users. | ||
# Include 'api' if there is a change to a library API. | ||
# Default: '[user]' | ||
change_logs: [user] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package azure // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/azure" | ||
|
||
import ( | ||
"bytes" | ||
"encoding/hex" | ||
"net/url" | ||
|
||
jsoniter "github.com/json-iterator/go" | ||
"go.opentelemetry.io/collector/pdata/pcommon" | ||
"go.opentelemetry.io/collector/pdata/ptrace" | ||
conventions "go.opentelemetry.io/collector/semconv/v1.13.0" | ||
"go.uber.org/zap" | ||
) | ||
|
||
const ( | ||
// Constants for OpenTelemetry Specs | ||
traceAzureResourceID = "azure.resource.id" | ||
traceScopeName = "otelcol/azureresourcetraces" | ||
) | ||
|
||
type azureTracesRecords struct { | ||
Records []azureTracesRecord `json:"records"` | ||
} | ||
|
||
// Azure Trace Records based on Azure AppRequests & AppDependencies table data | ||
// the common record schema reference: | ||
// https://learn.microsoft.com/en-us/azure/azure-monitor/reference/tables/apprequests | ||
// https://learn.microsoft.com/en-us/azure/azure-monitor/reference/tables/appdependencies | ||
type azureTracesRecord struct { | ||
Time string `json:"time"` | ||
ResourceID string `json:"resourceId"` | ||
ResourceGUID string `json:"ResourceGUID"` | ||
Type string `json:"Type"` | ||
AppRoleInstance string `json:"AppRoleInstance"` | ||
AppRoleName string `json:"AppRoleName"` | ||
AppVersion string `json:"AppVersion"` | ||
ClientCity string `json:"ClientCity"` | ||
ClientCountryOrRegion string `json:"ClientCountryOrRegion"` | ||
ClientIP string `json:"ClientIP"` | ||
ClientStateOrProvince string `json:"ClientStateOrProvince"` | ||
ClientType string `json:"ClientType"` | ||
IKey string `json:"IKey"` | ||
OperationName string `json:"OperationName"` | ||
OperationID string `json:"OperationId"` | ||
ParentID string `json:"ParentId"` | ||
SDKVersion string `json:"SDKVersion"` | ||
Properties map[string]string `json:"Properties"` | ||
Measurements map[string]float64 `json:"Measurements"` | ||
SpanID string `json:"Id"` | ||
Name string `json:"Name"` | ||
URL string `json:"Url"` | ||
Source string `json:"Source"` | ||
Success bool `json:"Success"` | ||
ResultCode string `json:"ResultCode"` | ||
DurationMs float64 `json:"DurationMs"` | ||
PerformanceBucket string `json:"PerformanceBucket"` | ||
ItemCount float64 `json:"ItemCount"` | ||
} | ||
|
||
var _ ptrace.Unmarshaler = (*TracesUnmarshaler)(nil) | ||
|
||
type TracesUnmarshaler struct { | ||
Version string | ||
Logger *zap.Logger | ||
} | ||
|
||
func (r TracesUnmarshaler) UnmarshalTraces(buf []byte) (ptrace.Traces, error) { | ||
t := ptrace.NewTraces() | ||
|
||
var azureTraces azureTracesRecords | ||
decoder := jsoniter.NewDecoder(bytes.NewReader(buf)) | ||
err := decoder.Decode(&azureTraces) | ||
if err != nil { | ||
return t, err | ||
} | ||
|
||
resourceTraces := t.ResourceSpans().AppendEmpty() | ||
resource := resourceTraces.Resource() | ||
resource.Attributes().PutStr(conventions.AttributeTelemetrySDKName, traceScopeName) | ||
resource.Attributes().PutStr(conventions.AttributeTelemetrySDKLanguage, conventions.AttributeTelemetrySDKLanguageGo) | ||
resource.Attributes().PutStr(conventions.AttributeTelemetrySDKVersion, r.Version) | ||
resource.Attributes().PutStr(conventions.AttributeCloudProvider, conventions.AttributeCloudProviderAzure) | ||
|
||
scopeSpans := resourceTraces.ScopeSpans().AppendEmpty() | ||
|
||
spans := scopeSpans.Spans() | ||
|
||
resourceID := "" | ||
for _, azureTrace := range azureTraces.Records { | ||
if resourceID == "" && azureTrace.ResourceID != "" { | ||
resourceID = azureTrace.ResourceID | ||
} | ||
|
||
resource.Attributes().PutStr("service.name", azureTrace.AppRoleName) | ||
|
||
nanos, err := asTimestamp(azureTrace.Time) | ||
if err != nil { | ||
r.Logger.Warn("Invalid Timestamp", zap.String("time", azureTrace.Time)) | ||
continue | ||
} | ||
|
||
var traceID, traceErr = TraceIDFromHex(azureTrace.OperationID) | ||
if traceErr != nil { | ||
r.Logger.Warn("Invalid TraceID", zap.String("traceID", azureTrace.OperationID)) | ||
return t, err | ||
} | ||
var spanID, spanErr = SpanIDFromHex(azureTrace.SpanID) | ||
if spanErr != nil { | ||
r.Logger.Warn("Invalid SpanID", zap.String("spanID", azureTrace.SpanID)) | ||
return t, err | ||
} | ||
var parentID, parentErr = SpanIDFromHex(azureTrace.ParentID) | ||
if parentErr != nil { | ||
r.Logger.Warn("Invalid ParentID", zap.String("parentID", azureTrace.ParentID)) | ||
return t, err | ||
} | ||
|
||
span := spans.AppendEmpty() | ||
span.SetTraceID(traceID) | ||
span.SetSpanID(spanID) | ||
span.SetParentSpanID(parentID) | ||
|
||
span.Attributes().PutStr("OperationName", azureTrace.OperationName) | ||
span.Attributes().PutStr("AppRoleName", azureTrace.AppRoleName) | ||
span.Attributes().PutStr("AppRoleInstance", azureTrace.AppRoleInstance) | ||
span.Attributes().PutStr("Type", azureTrace.Type) | ||
|
||
span.Attributes().PutStr("http.url", azureTrace.URL) | ||
|
||
urlObj, _ := url.Parse(azureTrace.URL) | ||
hostname := urlObj.Host | ||
hostpath := urlObj.Path | ||
scheme := urlObj.Scheme | ||
|
||
span.Attributes().PutStr("http.host", hostname) | ||
span.Attributes().PutStr("http.path", hostpath) | ||
span.Attributes().PutStr("http.response.status_code", azureTrace.ResultCode) | ||
span.Attributes().PutStr("http.client_ip", azureTrace.ClientIP) | ||
span.Attributes().PutStr("http.client_city", azureTrace.ClientCity) | ||
span.Attributes().PutStr("http.client_type", azureTrace.ClientType) | ||
span.Attributes().PutStr("http.client_state", azureTrace.ClientStateOrProvince) | ||
span.Attributes().PutStr("http.client_type", azureTrace.ClientType) | ||
span.Attributes().PutStr("http.client_country", azureTrace.ClientCountryOrRegion) | ||
span.Attributes().PutStr("http.scheme", scheme) | ||
span.Attributes().PutStr("http.method", azureTrace.Properties["HTTP Method"]) | ||
|
||
span.SetKind(ptrace.SpanKindServer) | ||
span.SetName(azureTrace.Name) | ||
span.SetStartTimestamp(nanos) | ||
span.SetEndTimestamp(nanos + pcommon.Timestamp(azureTrace.DurationMs*1e6)) | ||
} | ||
|
||
if resourceID != "" { | ||
resourceTraces.Resource().Attributes().PutStr(traceAzureResourceID, resourceID) | ||
} else { | ||
r.Logger.Warn("No ResourceID Set on Traces!") | ||
} | ||
|
||
return t, nil | ||
} | ||
|
||
func TraceIDFromHex(hexStr string) (pcommon.TraceID, error) { | ||
bytes, err := hex.DecodeString(hexStr) | ||
if err != nil { | ||
return pcommon.TraceID{}, err | ||
} | ||
var id pcommon.TraceID | ||
copy(id[:], bytes) | ||
return id, nil | ||
} | ||
|
||
func SpanIDFromHex(hexStr string) (pcommon.SpanID, error) { | ||
bytes, err := hex.DecodeString(hexStr) | ||
if err != nil { | ||
return pcommon.SpanID{}, err | ||
} | ||
var id pcommon.SpanID | ||
copy(id[:], bytes) | ||
return id, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
receiver/azureeventhubreceiver/azureresourcetraces_unmarshaler.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package azureeventhubreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/azureeventhubreceiver" | ||
|
||
import ( | ||
eventhub "github.com/Azure/azure-event-hubs-go/v3" | ||
"go.opentelemetry.io/collector/component" | ||
"go.opentelemetry.io/collector/pdata/ptrace" | ||
"go.uber.org/zap" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/azure" | ||
) | ||
|
||
type azureTracesEventUnmarshaler struct { | ||
unmarshaler *azure.TracesUnmarshaler | ||
} | ||
|
||
func newAzureTracesUnmarshaler(buildInfo component.BuildInfo, logger *zap.Logger) eventTracesUnmarshaler { | ||
return azureTracesEventUnmarshaler{ | ||
unmarshaler: &azure.TracesUnmarshaler{ | ||
Version: buildInfo.Version, | ||
Logger: logger, | ||
}, | ||
} | ||
} | ||
|
||
// UnmarshalTraces takes a byte array containing a JSON-encoded | ||
// payload with Azure records and transforms it into | ||
// an OpenTelemetry ptraces.traces object. The data in the Azure | ||
// record appears as fields and attributes in the | ||
// OpenTelemetry representation; the bodies of the | ||
// OpenTelemetry trace records are empty. | ||
func (r azureTracesEventUnmarshaler) UnmarshalTraces(event *eventhub.Event) (ptrace.Traces, error) { | ||
return r.unmarshaler.UnmarshalTraces(event.Data) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
receiver/azureeventhubreceiver/internal/metadata/generated_status.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.