66package dnslog // import "github.com/open-telemetry/opentelemetry-collector-contrib/extension/encoding/googlecloudlogentryencodingextension/internal/dnslog"
77
88import (
9+ "errors"
910 "fmt"
11+ "strings"
1012
1113 gojson "github.com/goccy/go-json"
1214 "go.opentelemetry.io/collector/pdata/pcommon"
13- semconv "go.opentelemetry.io/otel/semconv/v1.26 .0"
15+ semconv "go.opentelemetry.io/otel/semconv/v1.37 .0"
1416
1517 "github.com/open-telemetry/opentelemetry-collector-contrib/extension/encoding/googlecloudlogentryencodingextension/internal/shared"
1618)
@@ -19,34 +21,30 @@ const (
1921 CloudDNSQueryLogSuffix = "dns.googleapis.com%2Fdns_queries"
2022
2123 // 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"
2424 // gcpDNSQueryType holds the DNS query type
25- gcpDNSQueryType = "gcp. dns.query .type"
25+ gcpDNSQueryType = "dns.question .type" // TBD in SemConv
2626
2727 // Response related attributes
2828 // gcpDNSResponseCode holds the DNS response code
29- gcpDNSResponseCode = "gcp. dns.response.code"
29+ gcpDNSResponseCode = "dns.response_code" // TBD in SemConv
3030 // gcpDNSAliasQueryResponseCode holds the response code for alias queries
3131 gcpDNSAliasQueryResponseCode = "gcp.dns.alias_query.response.code"
3232 // gcpDNSAuthAnswer indicates whether the response is authoritative
3333 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"
34+ // gcpDNSAnswerData holds DNS answer in presentation format
35+ gcpDNSAnswerData = "dns.answer.data" // TBD in SemConv
3636
3737 // 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"
38+ // gcpDNSClientVPCNetwork holds the client network name where the DNS query originated
39+ gcpDNSClientVPCNetwork = "gcp.dns.client.vpc.name"
40+ // gcpDNSClientType holds the client type of the DNS query
41+ gcpDNSClientType = "gcp.dns.client.type"
42+
43+ // Server related attributes
44+ // gcpDNSServerName holds the name name of Google Cloud instance that is responsible of resolving the query
45+ gcpDNSServerName = "gcp.dns.server.name"
46+ // gcpDNServerType holds the type of target resolving the DNS query
47+ gcpDNServerType = "gcp.dns.server.type"
5048
5149 // Performance and error related attributes
5250 // gcpDNSServerLatency holds the server-side latency in seconds
@@ -63,16 +61,8 @@ const (
6361 gcpDNSDNS64Translated = "gcp.dns.dns64_translated"
6462
6563 // 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"
7264 // gcpDNSVMProjectID holds the Google Cloud project ID of the network from which the query was sent
7365 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"
7666)
7767
7868type dnslog struct {
@@ -97,36 +87,35 @@ type dnslog struct {
9787 TargetType string `json:"target_type"`
9888 UnhealthyIps string `json:"unhealthyIps"`
9989 VMInstanceID * int64 `json:"vmInstanceId"`
100- VMInstanceIDStr string `json:"vmInstanceIdString"`
10190 VMInstanceName string `json:"vmInstanceName"`
10291 VMProjectID string `json:"vmProjectId"`
10392 VMZoneName string `json:"vmZoneName"`
10493}
10594
10695func handleQueryAttributes (log * dnslog , attr pcommon.Map ) {
107- shared .PutStr (gcpDNSQueryName , log .QueryName , attr )
108- shared .PutStr (gcpDNSQueryType , log .QueryType , attr )
96+ shared .PutStr (string ( semconv . DNSQuestionNameKey ) , log .QueryName , attr )
97+ shared .PutStr (gcpDNSQueryType , log .QueryType , attr ) // TBD in SemConv
10998}
11099
111100func handleResponseAttributes (log * dnslog , attr pcommon.Map ) {
112- shared .PutStr (gcpDNSResponseCode , log .ResponseCode , attr )
101+ shared .PutStr (gcpDNSResponseCode , log .ResponseCode , attr ) // TBD in SemConv
113102 shared .PutStr (gcpDNSAliasQueryResponseCode , log .AliasQueryResponseCode , attr )
114103 shared .PutBool (gcpDNSAuthAnswer , log .AuthAnswer , attr )
115- shared .PutStr (gcpDNSRdata , log .Rdata , attr )
104+ shared .PutStr (gcpDNSAnswerData , log .Rdata , attr ) // TBD in SemConv
116105}
117106
118107func 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 )
108+ shared .PutStr (string ( semconv . ServerAddressKey ) , log .DestinationIP , attr )
109+ shared .PutStr (gcpDNSClientVPCNetwork , log .SourceNetwork , attr )
110+ shared .PutStr (gcpDNSClientType , log .SourceType , attr )
122111 shared .PutStr (string (semconv .ClientAddressKey ), log .SourceIP , attr )
123- shared .PutStr (string (semconv .NetworkTransportKey ), log .Protocol , attr )
112+ shared .PutStr (string (semconv .NetworkTransportKey ), strings . ToLower ( log .Protocol ) , attr )
124113 shared .PutStr (string (semconv .CloudRegionKey ), log .Location , attr )
125114}
126115
127116func handleTargetAttributes (log * dnslog , attr pcommon.Map ) {
128- shared .PutStr (gcpDNSTargetName , log .TargetName , attr )
129- shared .PutStr (gcpDNSTargetType , log .TargetType , attr )
117+ shared .PutStr (gcpDNSServerName , log .TargetName , attr )
118+ shared .PutStr (gcpDNServerType , log .TargetType , attr )
130119}
131120
132121func handlePerformanceAndErrorAttributes (log * dnslog , attr pcommon.Map ) {
@@ -141,26 +130,29 @@ func handleDNSFeatureAttributes(log *dnslog, attr pcommon.Map) {
141130}
142131
143132func 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 )
133+ shared .PutInt (string (semconv .HostIDKey ), log .VMInstanceID , attr )
134+ shared .PutStr (string (semconv .HostNameKey ), log .VMInstanceName , attr )
147135 shared .PutStr (gcpDNSVMProjectID , log .VMProjectID , attr )
148- shared .PutStr (gcpDNSVMZoneName , log .VMZoneName , attr )
136+ shared .PutStr (string ( semconv . CloudAvailabilityZoneKey ) , log .VMZoneName , attr )
149137}
150138
151139func ParsePayloadIntoAttributes (payload []byte , attr pcommon.Map ) error {
152- var log dnslog
140+ var log * dnslog
153141 if err := gojson .Unmarshal (payload , & log ); err != nil {
154142 return fmt .Errorf ("failed to unmarshal DNS log: %w" , err )
155143 }
156144
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 )
145+ if log == nil {
146+ return errors .New ("DNS cannot be nil after detecting payload as DNS log" )
147+ }
148+
149+ handleQueryAttributes (log , attr )
150+ handleResponseAttributes (log , attr )
151+ handleNetworkAttributes (log , attr )
152+ handleTargetAttributes (log , attr )
153+ handlePerformanceAndErrorAttributes (log , attr )
154+ handleDNSFeatureAttributes (log , attr )
155+ handleVMInstanceAttributes (log , attr )
164156
165157 return nil
166158}
0 commit comments