-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathoc_to_resource.go
132 lines (118 loc) · 4.44 KB
/
oc_to_resource.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// 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 internaldata
import (
"time"
occommon "github.com/census-instrumentation/opencensus-proto/gen-go/agent/common/v1"
ocresource "github.com/census-instrumentation/opencensus-proto/gen-go/resource/v1"
"go.opentelemetry.io/collector/consumer/pdata"
"go.opentelemetry.io/collector/translator/conventions"
)
var ocLangCodeToLangMap = getOCLangCodeToLangMap()
func getOCLangCodeToLangMap() map[occommon.LibraryInfo_Language]string {
mappings := make(map[occommon.LibraryInfo_Language]string)
mappings[1] = conventions.AttributeSDKLangValueCPP
mappings[2] = conventions.AttributeSDKLangValueDotNET
mappings[3] = conventions.AttributeSDKLangValueErlang
mappings[4] = conventions.AttributeSDKLangValueGo
mappings[5] = conventions.AttributeSDKLangValueJava
mappings[6] = conventions.AttributeSDKLangValueNodeJS
mappings[7] = conventions.AttributeSDKLangValuePHP
mappings[8] = conventions.AttributeSDKLangValuePython
mappings[9] = conventions.AttributeSDKLangValueRuby
mappings[10] = conventions.AttributeSDKLangValueWebJS
return mappings
}
func ocNodeResourceToInternal(ocNode *occommon.Node, ocResource *ocresource.Resource, dest pdata.Resource) {
if ocNode == nil && ocResource == nil {
return
}
// Number of special fields in OC that will be translated to Attributes
const serviceInfoAttrCount = 1 // Number of Node.ServiceInfo fields.
const nodeIdentifierAttrCount = 3 // Number of Node.Identifier fields.
const libraryInfoAttrCount = 3 // Number of Node.LibraryInfo fields.
const specialResourceAttrCount = 1 // Number of Resource fields.
// Calculate maximum total number of attributes for capacity reservation.
maxTotalAttrCount := 0
if ocNode != nil {
maxTotalAttrCount += len(ocNode.Attributes)
if ocNode.ServiceInfo != nil {
maxTotalAttrCount += serviceInfoAttrCount
}
if ocNode.Identifier != nil {
maxTotalAttrCount += nodeIdentifierAttrCount
}
if ocNode.LibraryInfo != nil {
maxTotalAttrCount += libraryInfoAttrCount
}
}
if ocResource != nil {
maxTotalAttrCount += len(ocResource.Labels)
if ocResource.Type != "" {
maxTotalAttrCount += specialResourceAttrCount
}
}
// There are no attributes to be set.
if maxTotalAttrCount == 0 {
return
}
attrs := dest.Attributes()
attrs.InitEmptyWithCapacity(maxTotalAttrCount)
if ocNode != nil {
// Copy all Attributes.
for k, v := range ocNode.Attributes {
attrs.InsertString(k, v)
}
// Add all special fields.
if ocNode.ServiceInfo != nil {
if ocNode.ServiceInfo.Name != "" {
attrs.UpsertString(conventions.AttributeServiceName, ocNode.ServiceInfo.Name)
}
}
if ocNode.Identifier != nil {
if ocNode.Identifier.StartTimestamp != nil {
attrs.UpsertString(conventions.OCAttributeProcessStartTime, ocNode.Identifier.StartTimestamp.AsTime().Format(time.RFC3339Nano))
}
if ocNode.Identifier.HostName != "" {
attrs.UpsertString(conventions.AttributeHostName, ocNode.Identifier.HostName)
}
if ocNode.Identifier.Pid != 0 {
attrs.UpsertInt(conventions.AttributeProcessID, int64(ocNode.Identifier.Pid))
}
}
if ocNode.LibraryInfo != nil {
if ocNode.LibraryInfo.CoreLibraryVersion != "" {
attrs.UpsertString(conventions.AttributeTelemetrySDKVersion, ocNode.LibraryInfo.CoreLibraryVersion)
}
if ocNode.LibraryInfo.ExporterVersion != "" {
attrs.UpsertString(conventions.OCAttributeExporterVersion, ocNode.LibraryInfo.ExporterVersion)
}
if ocNode.LibraryInfo.Language != occommon.LibraryInfo_LANGUAGE_UNSPECIFIED {
if str, ok := ocLangCodeToLangMap[ocNode.LibraryInfo.Language]; ok {
attrs.UpsertString(conventions.AttributeTelemetrySDKLanguage, str)
}
}
}
}
if ocResource != nil {
// Copy resource Labels.
for k, v := range ocResource.Labels {
attrs.InsertString(k, v)
}
// Add special fields.
if ocResource.Type != "" {
attrs.UpsertString(conventions.OCAttributeResourceType, ocResource.Type)
}
}
}