|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +// Find more information about Cloud dns logs at: |
| 5 | +// https://docs.cloud.google.com/dns/docs/monitoring#dns-log-record-format |
| 6 | +package dnslog // import "github.com/open-telemetry/opentelemetry-collector-contrib/extension/encoding/googlecloudlogentryencodingextension/internal/dnslog" |
| 7 | + |
| 8 | +import ( |
| 9 | + "fmt" |
| 10 | + |
| 11 | + gojson "github.com/goccy/go-json" |
| 12 | + "go.opentelemetry.io/collector/pdata/pcommon" |
| 13 | + semconv "go.opentelemetry.io/otel/semconv/v1.26.0" |
| 14 | + |
| 15 | + "github.com/open-telemetry/opentelemetry-collector-contrib/extension/encoding/googlecloudlogentryencodingextension/internal/shared" |
| 16 | +) |
| 17 | + |
| 18 | +const ( |
| 19 | + CloudDNSQueryLogSuffix = "dns.googleapis.com%2Fdns_queries" |
| 20 | + |
| 21 | + // Query related attributes. Ref: https://datatracker.ietf.org/doc/html/rfc1035#section-4.1.2 |
| 22 | + // gcpDNSQueryName holds the DNS query name |
| 23 | + gcpDNSQueryName = "gcp.dns.query.name" |
| 24 | + // gcpDNSQueryType holds the DNS query type |
| 25 | + gcpDNSQueryType = "gcp.dns.query.type" |
| 26 | + |
| 27 | + // Response related attributes |
| 28 | + // gcpDNSResponseCode holds the DNS response code |
| 29 | + gcpDNSResponseCode = "gcp.dns.response.code" |
| 30 | + // gcpDNSAliasQueryResponseCode holds the response code for alias queries |
| 31 | + gcpDNSAliasQueryResponseCode = "gcp.dns.alias_query.response.code" |
| 32 | + // gcpDNSAuthAnswer indicates whether the response is authoritative |
| 33 | + gcpDNSAuthAnswer = "gcp.dns.auth_answer" // Ref: https://datatracker.ietf.org/doc/html/rfc1035 |
| 34 | + // gcpDNSRdata holds DNS answer in presentation format |
| 35 | + gcpDNSRdata = "gcp.dns.rdata" |
| 36 | + |
| 37 | + // Network related attributes |
| 38 | + // gcpDNSDestinationIP holds target IP address |
| 39 | + gcpDNSDestinationIP = "gcp.dns.destination_ip" |
| 40 | + // gcpDNSSourceNetwork holds the source network name |
| 41 | + gcpDNSSourceNetwork = "gcp.dns.source.network" |
| 42 | + // gcpDNSSourceType holds the source type of the DNS query |
| 43 | + gcpDNSSourceType = "gcp.dns.source.type" |
| 44 | + |
| 45 | + // Target related attributes |
| 46 | + // gcpDNSTargetName holds the target name |
| 47 | + gcpDNSTargetName = "gcp.dns.target.name" |
| 48 | + // gcpDNSTargetType holds the type of target resolving the DNS query |
| 49 | + gcpDNSTargetType = "gcp.dns.target.type" |
| 50 | + |
| 51 | + // Performance and error related attributes |
| 52 | + // gcpDNSServerLatency holds the server-side latency in seconds |
| 53 | + gcpDNSServerLatency = "gcp.dns.server_latency" |
| 54 | + // gcpDNSEgressError holds Egress proxy error, the actual error as received from the on-premises DNS server |
| 55 | + gcpDNSEgressError = "gcp.dns.egress_error" |
| 56 | + // gcpDNSHealthyIPs holds addresses in the ResourceRecordSet that are known to be HEALTHY. |
| 57 | + gcpDNSHealthyIPs = "gcp.dns.healthy_ips" |
| 58 | + // gcpDNSUnhealthyIPs holds addresses in the ResourceRecordSet that are known to be UNHEALTHY. |
| 59 | + gcpDNSUnhealthyIPs = "gcp.dns.unhealthy_ips" |
| 60 | + |
| 61 | + // DNS feature related attributes |
| 62 | + // gcpDNSDNS64Translated indicates whether DNS64 translation was applied |
| 63 | + gcpDNSDNS64Translated = "gcp.dns.dns64_translated" |
| 64 | + |
| 65 | + // VM instance related attributes |
| 66 | + // gcpDNSVMInstanceID holds the Compute Engine VM instance ID as an integer |
| 67 | + gcpDNSVMInstanceID = "gcp.dns.vm.instance.id" |
| 68 | + // gcpDNSVMInstanceIDString holds the Compute Engine VM instance ID as a string |
| 69 | + gcpDNSVMInstanceIDString = "gcp.dns.vm.instance.id_string" |
| 70 | + // gcpDNSVMInstanceName holds the VM instance name |
| 71 | + gcpDNSVMInstanceName = "gcp.dns.vm.instance.name" |
| 72 | + // gcpDNSVMProjectID holds the Google Cloud project ID of the network from which the query was sent |
| 73 | + gcpDNSVMProjectID = "gcp.dns.vm.project_id" |
| 74 | + // gcpDNSVMZoneName holds the name of the VM zone from which the query was sent |
| 75 | + gcpDNSVMZoneName = "gcp.dns.vm.zone" |
| 76 | +) |
| 77 | + |
| 78 | +type dnslog struct { |
| 79 | + AliasQueryResponseCode string `json:"alias_query_response_code"` |
| 80 | + AuthAnswer *bool `json:"authAnswer"` |
| 81 | + DestinationIP string `json:"destinationIP"` |
| 82 | + DNS64Translated *bool `json:"dns64Translated"` |
| 83 | + EgressError string `json:"egressError"` |
| 84 | + HealthyIps string `json:"healthyIps"` |
| 85 | + Location string `json:"location"` |
| 86 | + Protocol string `json:"protocol"` |
| 87 | + ProjectID string `json:"project_id"` |
| 88 | + QueryName string `json:"queryName"` |
| 89 | + QueryType string `json:"queryType"` |
| 90 | + Rdata string `json:"rdata"` |
| 91 | + ResponseCode string `json:"responseCode"` |
| 92 | + ServerLatency *float64 `json:"serverLatency"` |
| 93 | + SourceIP string `json:"sourceIP"` |
| 94 | + SourceNetwork string `json:"sourceNetwork"` |
| 95 | + SourceType string `json:"source_type"` |
| 96 | + TargetName string `json:"target_name"` |
| 97 | + TargetType string `json:"target_type"` |
| 98 | + UnhealthyIps string `json:"unhealthyIps"` |
| 99 | + VMInstanceID *int64 `json:"vmInstanceId"` |
| 100 | + VMInstanceIDStr string `json:"vmInstanceIdString"` |
| 101 | + VMInstanceName string `json:"vmInstanceName"` |
| 102 | + VMProjectID string `json:"vmProjectId"` |
| 103 | + VMZoneName string `json:"vmZoneName"` |
| 104 | +} |
| 105 | + |
| 106 | +func handleQueryAttributes(log *dnslog, attr pcommon.Map) { |
| 107 | + shared.PutStr(gcpDNSQueryName, log.QueryName, attr) |
| 108 | + shared.PutStr(gcpDNSQueryType, log.QueryType, attr) |
| 109 | +} |
| 110 | + |
| 111 | +func handleResponseAttributes(log *dnslog, attr pcommon.Map) { |
| 112 | + shared.PutStr(gcpDNSResponseCode, log.ResponseCode, attr) |
| 113 | + shared.PutStr(gcpDNSAliasQueryResponseCode, log.AliasQueryResponseCode, attr) |
| 114 | + shared.PutBool(gcpDNSAuthAnswer, log.AuthAnswer, attr) |
| 115 | + shared.PutStr(gcpDNSRdata, log.Rdata, attr) |
| 116 | +} |
| 117 | + |
| 118 | +func handleNetworkAttributes(log *dnslog, attr pcommon.Map) { |
| 119 | + shared.PutStr(gcpDNSDestinationIP, log.DestinationIP, attr) |
| 120 | + shared.PutStr(gcpDNSSourceNetwork, log.SourceNetwork, attr) |
| 121 | + shared.PutStr(gcpDNSSourceType, log.SourceType, attr) |
| 122 | + shared.PutStr(string(semconv.ClientAddressKey), log.SourceIP, attr) |
| 123 | + shared.PutStr(string(semconv.NetworkTransportKey), log.Protocol, attr) |
| 124 | + shared.PutStr(string(semconv.CloudRegionKey), log.Location, attr) |
| 125 | +} |
| 126 | + |
| 127 | +func handleTargetAttributes(log *dnslog, attr pcommon.Map) { |
| 128 | + shared.PutStr(gcpDNSTargetName, log.TargetName, attr) |
| 129 | + shared.PutStr(gcpDNSTargetType, log.TargetType, attr) |
| 130 | +} |
| 131 | + |
| 132 | +func handlePerformanceAndErrorAttributes(log *dnslog, attr pcommon.Map) { |
| 133 | + shared.PutDouble(gcpDNSServerLatency, log.ServerLatency, attr) |
| 134 | + shared.PutStr(gcpDNSEgressError, log.EgressError, attr) |
| 135 | + shared.PutStr(gcpDNSHealthyIPs, log.HealthyIps, attr) |
| 136 | + shared.PutStr(gcpDNSUnhealthyIPs, log.UnhealthyIps, attr) |
| 137 | +} |
| 138 | + |
| 139 | +func handleDNSFeatureAttributes(log *dnslog, attr pcommon.Map) { |
| 140 | + shared.PutBool(gcpDNSDNS64Translated, log.DNS64Translated, attr) |
| 141 | +} |
| 142 | + |
| 143 | +func handleVMInstanceAttributes(log *dnslog, attr pcommon.Map) { |
| 144 | + shared.PutInt(gcpDNSVMInstanceID, log.VMInstanceID, attr) |
| 145 | + shared.PutStr(gcpDNSVMInstanceIDString, log.VMInstanceIDStr, attr) |
| 146 | + shared.PutStr(gcpDNSVMInstanceName, log.VMInstanceName, attr) |
| 147 | + shared.PutStr(gcpDNSVMProjectID, log.VMProjectID, attr) |
| 148 | + shared.PutStr(gcpDNSVMZoneName, log.VMZoneName, attr) |
| 149 | +} |
| 150 | + |
| 151 | +func ParsePayloadIntoAttributes(payload []byte, attr pcommon.Map) error { |
| 152 | + var log dnslog |
| 153 | + if err := gojson.Unmarshal(payload, &log); err != nil { |
| 154 | + return fmt.Errorf("failed to unmarshal DNS log: %w", err) |
| 155 | + } |
| 156 | + |
| 157 | + handleQueryAttributes(&log, attr) |
| 158 | + handleResponseAttributes(&log, attr) |
| 159 | + handleNetworkAttributes(&log, attr) |
| 160 | + handleTargetAttributes(&log, attr) |
| 161 | + handlePerformanceAndErrorAttributes(&log, attr) |
| 162 | + handleDNSFeatureAttributes(&log, attr) |
| 163 | + handleVMInstanceAttributes(&log, attr) |
| 164 | + |
| 165 | + return nil |
| 166 | +} |
0 commit comments