Debian Codegraph @

service/internal/graph/host.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package graph // import "go.opentelemetry.io/collector/service/internal/graph"
5
6 import (
7 "net/http"
8 "path"
9 "runtime"
10 "time"
11
12 "go.opentelemetry.io/collector/component"
13 "go.opentelemetry.io/collector/component/componentstatus"
14 "go.opentelemetry.io/collector/featuregate"
15 "go.opentelemetry.io/collector/pipeline"
16 "go.opentelemetry.io/collector/service/extensions"
17 "go.opentelemetry.io/collector/service/hostcapabilities"
18 "go.opentelemetry.io/collector/service/internal/builders"
19 "go.opentelemetry.io/collector/service/internal/moduleinfo"
20 "go.opentelemetry.io/collector/service/internal/status"
21 "go.opentelemetry.io/collector/service/internal/zpages"
22 )
23
24 var (
25 _ component.Host = (*Host)(nil)
26 _ hostcapabilities.ModuleInfo = (*Host)(nil)
27 _ hostcapabilities.ExposeExporters = (*Host)(nil) //nolint:staticcheck // SA1019
28 _ hostcapabilities.ComponentFactory = (*Host)(nil)
29 )
30
31 type Host struct {
32 AsyncErrorChannel chan error
33 Receivers *builders.ReceiverBuilder
34 Processors *builders.ProcessorBuilder
35 Exporters *builders.ExporterBuilder
36 Connectors *builders.ConnectorBuilder
37 Extensions *builders.ExtensionBuilder
38
39 ModuleInfos moduleinfo.ModuleInfos
40 BuildInfo component.BuildInfo
41
42 Pipelines *Graph
43 ServiceExtensions *extensions.Extensions
44
45 Reporter status.Reporter
46 }
47
48 func (host *Host) GetFactory(kind component.Kind, componentType component.Type) component.Factory {
49 switch kind {
50 case component.KindReceiver:
51 return host.Receivers.Factory(componentType)
52 case component.KindProcessor:
53 return host.Processors.Factory(componentType)
54 case component.KindExporter:
55 return host.Exporters.Factory(componentType)
56 case component.KindConnector:
57 return host.Connectors.Factory(componentType)
58 case component.KindExtension:
59 return host.Extensions.Factory(componentType)
60 }
61 return nil
62 }
63
64 func (host *Host) GetExtensions() map[component.ID]component.Component {
65 return host.ServiceExtensions.GetExtensions()
66 }
67
68 func (host *Host) GetModuleInfos() moduleinfo.ModuleInfos {
69 return host.ModuleInfos
70 }
71
72 // Deprecated: [0.79.0] This function will be removed in the future.
73 // Several components in the contrib repository use this function so it cannot be removed
74 // before those cases are removed. In most cases, use of this function can be replaced by a
75 // connector. See https://github.com/open-telemetry/opentelemetry-collector/issues/7370 and
76 // https://github.com/open-telemetry/opentelemetry-collector/pull/7390#issuecomment-1483710184
77 // for additional information.
78 func (host *Host) GetExporters() map[pipeline.Signal]map[component.ID]component.Component {
79 return host.Pipelines.GetExporters()
80 }
81
82 func (host *Host) NotifyComponentStatusChange(source *componentstatus.InstanceID, event *componentstatus.Event) {
83 host.ServiceExtensions.NotifyComponentStatusChange(source, event)
84 if event.Status() == componentstatus.StatusFatalError {
85 host.AsyncErrorChannel <- event.Err()
86 }
87 }
88
89 const (
90 // Paths
91 zServicePath = "servicez"
92 zPipelinePath = "pipelinez"
93 zExtensionPath = "extensionz"
94 zFeaturePath = "featurez"
95 )
96
97 // InfoVar is a singleton instance of the Info struct.
98 var runtimeInfoVar [][2]string
99
100 func init() {
101 runtimeInfoVar = [][2]string{
102 {"StartTimestamp", time.Now().String()},
103 {"Go", runtime.Version()},
104 {"OS", runtime.GOOS},
105 {"Arch", runtime.GOARCH},
106 // Add other valuable runtime information here.
107 }
108 }
109
110 func (host *Host) RegisterZPages(mux *http.ServeMux, pathPrefix string) {
111 mux.HandleFunc(path.Join(pathPrefix, zServicePath), host.zPagesRequest)
112 mux.HandleFunc(path.Join(pathPrefix, zPipelinePath), host.Pipelines.HandleZPages)
113 mux.HandleFunc(path.Join(pathPrefix, zExtensionPath), host.ServiceExtensions.HandleZPages)
114 mux.HandleFunc(path.Join(pathPrefix, zFeaturePath), handleFeaturezRequest)
115 }
116
117 func (host *Host) zPagesRequest(w http.ResponseWriter, _ *http.Request) {
118 w.Header().Set("Content-Type", "text/html; charset=utf-8")
119 zpages.WriteHTMLPageHeader(w, zpages.HeaderData{Title: "Service " + host.BuildInfo.Command})
120 zpages.WriteHTMLPropertiesTable(w, zpages.PropertiesTableData{Name: "Build Info", Properties: getBuildInfoProperties(host.BuildInfo)})
121 zpages.WriteHTMLPropertiesTable(w, zpages.PropertiesTableData{Name: "Runtime Info", Properties: runtimeInfoVar})
122 zpages.WriteHTMLComponentHeader(w, zpages.ComponentHeaderData{
123 Name: "Pipelines",
124 ComponentEndpoint: zPipelinePath,
125 Link: true,
126 })
127 zpages.WriteHTMLComponentHeader(w, zpages.ComponentHeaderData{
128 Name: "Extensions",
129 ComponentEndpoint: zExtensionPath,
130 Link: true,
131 })
132 zpages.WriteHTMLComponentHeader(w, zpages.ComponentHeaderData{
133 Name: "Features",
134 ComponentEndpoint: zFeaturePath,
135 Link: true,
136 })
137 zpages.WriteHTMLPageFooter(w)
138 }
139
140 func handleFeaturezRequest(w http.ResponseWriter, _ *http.Request) {
141 w.Header().Set("Content-Type", "text/html; charset=utf-8")
142 zpages.WriteHTMLPageHeader(w, zpages.HeaderData{Title: "Feature Gates"})
143 zpages.WriteHTMLFeaturesTable(w, getFeaturesTableData())
144 zpages.WriteHTMLPageFooter(w)
145 }
146
147 func getFeaturesTableData() zpages.FeatureGateTableData {
148 data := zpages.FeatureGateTableData{}
149 featuregate.GlobalRegistry().VisitAll(func(gate *featuregate.Gate) {
150 data.Rows = append(data.Rows, zpages.FeatureGateTableRowData{
151 ID: gate.ID(),
152 Enabled: gate.IsEnabled(),
153 Description: gate.Description(),
154 Stage: gate.Stage().String(),
155 FromVersion: gate.FromVersion(),
156 ToVersion: gate.ToVersion(),
157 ReferenceURL: gate.ReferenceURL(),
158 })
159 })
160 return data
161 }
162
163 func getBuildInfoProperties(buildInfo component.BuildInfo) [][2]string {
164 return [][2]string{
165 {"Command", buildInfo.Command},
166 {"Description", buildInfo.Description},
167 {"Version", buildInfo.Version},
168 }
169 }