-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
105 lines (84 loc) · 2.73 KB
/
main.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
package main
import (
"fmt"
"go/build"
"log"
"os"
"github.com/iancoleman/orderedmap"
"github.com/knadh/koanf/parsers/json"
"github.com/knadh/koanf/v2"
"go.opentelemetry.io/collector/service"
"github.com/srikanthccv/otel/collector/schema/components"
"github.com/srikanthccv/otel/collector/schema/jsonschema"
)
func modCacheLoc() string {
return build.Default.GOPATH + "/pkg/mod"
}
func debug(r *jsonschema.Reflector) {
sCfg := components.KoanfLoad(r.Reflect(&service.Config{}).MarshalJSON())
final := koanf.New(".")
final.Merge(sCfg)
s, _ := final.Marshal(json.Parser())
fmt.Println(string(s))
}
func collectorService(r *jsonschema.Reflector) ([]*koanf.Koanf, *jsonschema.Schema) {
service := components.KoanfLoad(r.Reflect(&service.Config{}).MarshalJSON())
serviceSchema := jsonschema.Schema{
Ref: "#/$defs/go.opentelemetry.io.collector.service.Config",
}
return []*koanf.Koanf{service}, &serviceSchema
}
func main() {
r := new(jsonschema.Reflector)
r.Anonymous = true
if err := r.AddGoComments("", fmt.Sprintf("%s/go.opentelemetry.io/collector", modCacheLoc())); err != nil {
log.Print("error adding go comments for core repo")
}
if err := r.AddGoComments("", fmt.Sprintf("%s/github.com/open-telemetry/opentelemetry-collector-contrib", modCacheLoc())); err != nil {
log.Print("error adding go comments for contrib repo")
}
final := koanf.New(".")
exporters, exportersTopLevel := components.Exporters(r)
extensions, extensionsTopLevel := components.Extensions(r)
receivers, receiversTopLevel := components.Receivers(r)
processors, processorsTopLevel := components.Processors(r)
service, serviceSchema := collectorService(r)
for _, e := range exporters {
final.Merge(e)
}
for _, e := range extensions {
final.Merge(e)
}
for _, r := range receivers {
final.Merge(r)
}
for _, p := range processors {
final.Merge(p)
}
for _, s := range service {
final.Merge(s)
}
mainSchemaProperties := orderedmap.New()
mainSchemaProperties.Set("receivers", receiversTopLevel)
mainSchemaProperties.Set("exporters", exportersTopLevel)
mainSchemaProperties.Set("processors", processorsTopLevel)
mainSchemaProperties.Set("extensions", extensionsTopLevel)
mainSchemaProperties.Set("service", serviceSchema)
mainSchema := jsonschema.Schema{
Version: "https://json-schema.org/draft/2020-12/schema",
Title: "OpenTelemetry Collector Config Schema",
Type: "object",
Required: []string{"receivers", "exporters", "service"},
Properties: mainSchemaProperties,
}
final.Merge(components.KoanfLoad(mainSchema.MarshalJSON()))
final.Delete("$ref")
s, _ := final.Marshal(json.Parser())
// write to schema.json
fd, err := os.Create("schema.json")
if err != nil {
log.Fatal(err)
}
defer fd.Close()
fd.Write(s)
}