-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
map Azure logs into OpenTelemetry fields/attributes (#16357)
This change allows users of the Azure Event Hub receiver to choose between two representations of their Azure resource/service logs in OpenTelemetry. The "raw" format (existing behavior) maps the AMQP message properties and data to the OpenTelemetry attributes and body (as bytes), respectively. The new "azure" format parses the AMQP message data and maps the Azure structured log into OpenTelemetry attributes; the body of the LogRecords are empty.
- Loading branch information
1 parent
7c1b702
commit 31064e1
Showing
22 changed files
with
824 additions
and
18 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,16 @@ | ||
# 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: adds alternate log formatter that maps Azure log fields into OpenTelemetry attributes | ||
|
||
# One or more tracking issues related to the change | ||
issues: [16283] | ||
|
||
# (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: |
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.
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.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
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/plog" | ||
) | ||
|
||
type azureLogFormatConverter struct { | ||
buildInfo component.BuildInfo | ||
} | ||
|
||
func newAzureLogFormatConverter(settings component.ReceiverCreateSettings) *azureLogFormatConverter { | ||
return &azureLogFormatConverter{buildInfo: settings.BuildInfo} | ||
} | ||
|
||
func (c *azureLogFormatConverter) ToLogs(event *eventhub.Event) (plog.Logs, error) { | ||
logs, err := transform(c.buildInfo, event.Data) | ||
return logs, err | ||
} |
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,199 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package azureeventhubreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/azureeventhubreceiver" | ||
|
||
import ( | ||
"bytes" | ||
"strconv" | ||
|
||
jsoniter "github.com/json-iterator/go" | ||
"github.com/relvacode/iso8601" | ||
"go.opentelemetry.io/collector/component" | ||
"go.opentelemetry.io/collector/pdata/pcommon" | ||
"go.opentelemetry.io/collector/pdata/plog" | ||
conventions "go.opentelemetry.io/collector/semconv/v1.13.0" | ||
) | ||
|
||
const azureCategory = "azure.category" | ||
const azureCorrelationID = "azure.correlation.id" | ||
const azureDuration = "azure.duration" | ||
const azureIdentity = "azure.identity" | ||
const azureOperationName = "azure.operation.name" | ||
const azureOperationVersion = "azure.operation.version" | ||
const azureProperties = "azure.properties" | ||
const azureResourceID = "azure.resource.id" | ||
const azureResultType = "azure.result.type" | ||
const azureResultSignature = "azure.result.signature" | ||
const azureResultDescription = "azure.result.description" | ||
const azureTenantID = "azure.tenant.id" | ||
|
||
const receiverScopeName = "otelcol/" + typeStr | ||
|
||
// azureRecords represents an array of Azure log records | ||
// as exported via an Azure Event Hub | ||
type azureRecords struct { | ||
Records []azureLogRecord `json:"records"` | ||
} | ||
|
||
// azureLogRecord represents a single Azure log following | ||
// the common schema: | ||
// https://learn.microsoft.com/en-us/azure/azure-monitor/essentials/resource-logs-schema | ||
type azureLogRecord struct { | ||
Time string `json:"time"` | ||
ResourceID string `json:"resourceId"` | ||
TenantID *string `json:"tenantId"` | ||
OperationName string `json:"operationName"` | ||
OperationVersion *string `json:"operationVersion"` | ||
Category string `json:"category"` | ||
ResultType *string `json:"resultType"` | ||
ResultSignature *string `json:"resultSignature"` | ||
ResultDescription *string `json:"resultDescription"` | ||
DurationMs *string `json:"durationMs"` | ||
CallerIPAddress *string `json:"callerIpAddress"` | ||
CorrelationID *string `json:"correlationId"` | ||
Identity *interface{} `json:"identity"` | ||
Level *string `json:"Level"` | ||
Location *string `json:"location"` | ||
Properties *interface{} `json:"properties"` | ||
} | ||
|
||
// asTimestamp will parse an ISO8601 string into an OpenTelemetry | ||
// nanosecond timestamp. If the string cannot be parsed, it will | ||
// return zero and the error. | ||
func asTimestamp(s string) (pcommon.Timestamp, error) { | ||
t, err := iso8601.ParseString(s) | ||
if err != nil { | ||
return 0, err | ||
} | ||
return pcommon.Timestamp(t.UnixNano()), nil | ||
} | ||
|
||
// asSeverity converts the Azure log level to equivalent | ||
// OpenTelemetry severity numbers. If the log level is not | ||
// valid, then the 'Unspecified' value is returned. | ||
func asSeverity(s string) plog.SeverityNumber { | ||
switch s { | ||
case "Informational": | ||
return plog.SeverityNumberInfo | ||
case "Warning": | ||
return plog.SeverityNumberWarn | ||
case "Error": | ||
return plog.SeverityNumberError | ||
case "Critical": | ||
return plog.SeverityNumberFatal | ||
default: | ||
return plog.SeverityNumberUnspecified | ||
} | ||
} | ||
|
||
// setIf will modify the given raw map by setting | ||
// the key and value iff the value is not null and | ||
// not the empty string. | ||
func setIf(attrs map[string]interface{}, key string, value *string) { | ||
if value != nil && *value != "" { | ||
attrs[key] = *value | ||
} | ||
} | ||
|
||
// extractRawAttributes creates a raw attribute map and | ||
// inserts attributes from the Azure log record. Optional | ||
// attributes are only inserted if they are defined. The | ||
// azureDuration value is only set if the value in the | ||
// Azure log record can be parsed as an integer. | ||
func extractRawAttributes(log azureLogRecord) map[string]interface{} { | ||
var attrs = map[string]interface{}{} | ||
|
||
attrs[azureCategory] = log.Category | ||
setIf(attrs, azureCorrelationID, log.CorrelationID) | ||
if log.DurationMs != nil { | ||
duration, err := strconv.ParseInt(*log.DurationMs, 10, 64) | ||
if err == nil { | ||
attrs[azureDuration] = duration | ||
} | ||
} | ||
if log.Identity != nil { | ||
attrs[azureIdentity] = *log.Identity | ||
} | ||
attrs[azureOperationName] = log.OperationName | ||
setIf(attrs, azureOperationVersion, log.OperationVersion) | ||
if log.Properties != nil { | ||
attrs[azureProperties] = *log.Properties | ||
} | ||
setIf(attrs, azureResultDescription, log.ResultDescription) | ||
setIf(attrs, azureResultSignature, log.ResultSignature) | ||
setIf(attrs, azureResultType, log.ResultType) | ||
setIf(attrs, azureTenantID, log.TenantID) | ||
|
||
setIf(attrs, conventions.AttributeCloudRegion, log.Location) | ||
attrs[conventions.AttributeCloudProvider] = conventions.AttributeCloudProviderAzure | ||
|
||
setIf(attrs, conventions.AttributeNetSockPeerAddr, log.CallerIPAddress) | ||
return attrs | ||
} | ||
|
||
// transform takes a byte array containing a JSON-encoded | ||
// payload with Azure log records and transforms it into | ||
// an OpenTelemetry plog.Logs object. The data in the Azure | ||
// log record appears as fields and attributes in the | ||
// OpenTelemetry representation; the bodies of the | ||
// OpenTelemetry log records are empty. | ||
func transform(buildInfo component.BuildInfo, data []byte) (plog.Logs, error) { | ||
|
||
l := plog.NewLogs() | ||
|
||
var azureLogs azureRecords | ||
decoder := jsoniter.NewDecoder(bytes.NewReader(data)) | ||
if err := decoder.Decode(&azureLogs); err != nil { | ||
return l, err | ||
} | ||
|
||
resourceLogs := l.ResourceLogs().AppendEmpty() | ||
scopeLogs := resourceLogs.ScopeLogs().AppendEmpty() | ||
scopeLogs.Scope().SetName(receiverScopeName) | ||
scopeLogs.Scope().SetVersion(buildInfo.Version) | ||
logRecords := scopeLogs.LogRecords() | ||
|
||
resourceID := "" | ||
for _, azureLog := range azureLogs.Records { | ||
resourceID = azureLog.ResourceID | ||
nanos, err := asTimestamp(azureLog.Time) | ||
if err != nil { | ||
continue | ||
} | ||
|
||
lr := logRecords.AppendEmpty() | ||
|
||
lr.SetTimestamp(nanos) | ||
|
||
if azureLog.Level != nil { | ||
severity := asSeverity(*azureLog.Level) | ||
lr.SetSeverityNumber(severity) | ||
lr.SetSeverityText(*azureLog.Level) | ||
} | ||
|
||
if err := lr.Attributes().FromRaw(extractRawAttributes(azureLog)); err != nil { | ||
return l, err | ||
} | ||
|
||
// The Azure resource ID will be pulled into a common resource attribute. | ||
// This implementation assumes that a single log message from Azure will | ||
// contain ONLY logs from a single resource. | ||
if resourceID != "" { | ||
resourceLogs.Resource().Attributes().PutStr(azureResourceID, resourceID) | ||
} | ||
} | ||
|
||
return l, nil | ||
} |
Oops, something went wrong.