Debian Codegraph @

service/internal/attribute/attribute.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package attribute // import "go.opentelemetry.io/collector/service/internal/attribute"
5
6 import (
7 "hash/fnv"
8 "strings"
9
10 "go.opentelemetry.io/otel/attribute"
11
12 "go.opentelemetry.io/collector/component"
13 "go.opentelemetry.io/collector/internal/telemetry"
14 "go.opentelemetry.io/collector/pipeline"
15 )
16
17 const (
18 capabiltiesKind = "capabilities"
19 fanoutKind = "fanout"
20 )
21
22 type Attributes struct {
23 set attribute.Set
24 id int64
25 }
26
27 func newAttributes(attrs ...attribute.KeyValue) Attributes {
28 h := fnv.New64a()
29 for _, kv := range attrs {
30 h.Write([]byte("(" + string(kv.Key) + "|" + kv.Value.AsString() + ")"))
31 }
32 return Attributes{
33 set: attribute.NewSet(attrs...),
34
35 // The graph identifies nodes by an int64 ID, but fnv gives us a uint64.
36 // It is safe to cast because the meaning of the number is irrelevant.
37 // We only care that each node has a unique 64 bit ID, which is unaltered by this cast.
38 id: int64(h.Sum64()), // #nosec G115
39 }
40 }
41
42 func (a Attributes) Set() *attribute.Set {
43 return &a.set
44 }
45
46 func (a Attributes) ID() int64 {
47 return a.id
48 }
49
50 func Receiver(pipelineType pipeline.Signal, id component.ID) Attributes {
51 return newAttributes(
52 attribute.String(telemetry.ComponentKindKey, strings.ToLower(component.KindReceiver.String())),
53 attribute.String(telemetry.SignalKey, pipelineType.String()),
54 attribute.String(telemetry.ComponentIDKey, id.String()),
55 )
56 }
57
58 func Processor(pipelineID pipeline.ID, id component.ID) Attributes {
59 return newAttributes(
60 attribute.String(telemetry.ComponentKindKey, strings.ToLower(component.KindProcessor.String())),
61 attribute.String(telemetry.SignalKey, pipelineID.Signal().String()),
62 attribute.String(telemetry.PipelineIDKey, pipelineID.String()),
63 attribute.String(telemetry.ComponentIDKey, id.String()),
64 )
65 }
66
67 func Exporter(pipelineType pipeline.Signal, id component.ID) Attributes {
68 return newAttributes(
69 attribute.String(telemetry.ComponentKindKey, strings.ToLower(component.KindExporter.String())),
70 attribute.String(telemetry.SignalKey, pipelineType.String()),
71 attribute.String(telemetry.ComponentIDKey, id.String()),
72 )
73 }
74
75 func Connector(exprPipelineType, rcvrPipelineType pipeline.Signal, id component.ID) Attributes {
76 return newAttributes(
77 attribute.String(telemetry.ComponentKindKey, strings.ToLower(component.KindConnector.String())),
78 attribute.String(telemetry.SignalKey, exprPipelineType.String()),
79 attribute.String(telemetry.SignalOutputKey, rcvrPipelineType.String()),
80 attribute.String(telemetry.ComponentIDKey, id.String()),
81 )
82 }
83
84 func Extension(id component.ID) Attributes {
85 return newAttributes(
86 attribute.String(telemetry.ComponentKindKey, strings.ToLower(component.KindExtension.String())),
87 attribute.String(telemetry.ComponentIDKey, id.String()),
88 )
89 }
90
91 func Capabilities(pipelineID pipeline.ID) Attributes {
92 return newAttributes(
93 attribute.String(telemetry.ComponentKindKey, capabiltiesKind),
94 attribute.String(telemetry.PipelineIDKey, pipelineID.String()),
95 )
96 }
97
98 func Fanout(pipelineID pipeline.ID) Attributes {
99 return newAttributes(
100 attribute.String(telemetry.ComponentKindKey, fanoutKind),
101 attribute.String(telemetry.PipelineIDKey, pipelineID.String()),
102 )
103 }