/
resource.go
94 lines (78 loc) · 2.21 KB
/
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
package tracer
import (
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/sdk/resource"
semconv "go.opentelemetry.io/otel/semconv/v1.12.0"
)
// alias, for other structs, the following code does not need to change the names of the resourceOptions
type resourceOptions = resourceConfig
// ResourceOption modifying struct field values by means of an interface
type ResourceOption interface {
apply(*resourceOptions)
}
type resourceOptionFunc func(*resourceOptions)
func (o resourceOptionFunc) apply(cfg *resourceOptions) {
o(cfg)
}
// set obj fields value
func apply(obj *resourceOptions, opts ...ResourceOption) {
for _, opt := range opts {
opt.apply(obj)
}
}
// WithServiceName set service name
func WithServiceName(name string) ResourceOption {
return resourceOptionFunc(func(o *resourceOptions) {
o.serviceName = name
})
}
// WithServiceVersion set service version
func WithServiceVersion(version string) ResourceOption {
return resourceOptionFunc(func(o *resourceOptions) {
o.serviceVersion = version
})
}
// WithEnvironment set service environment
func WithEnvironment(environment string) ResourceOption {
return resourceOptionFunc(func(o *resourceOptions) {
o.environment = environment
})
}
// WithAttributes set service attributes
func WithAttributes(attributes map[string]string) ResourceOption {
return resourceOptionFunc(func(o *resourceOptions) {
o.attributes = attributes
})
}
type resourceConfig struct {
serviceName string
serviceVersion string
environment string
attributes map[string]string
}
// NewResource returns a resource describing this application.
func NewResource(opts ...ResourceOption) *resource.Resource {
// default values
rc := &resourceConfig{
serviceName: "demo-service",
serviceVersion: "v0.0.0",
environment: "dev",
}
apply(rc, opts...)
kvs := []attribute.KeyValue{
semconv.ServiceNameKey.String(rc.serviceName),
semconv.ServiceVersionKey.String(rc.serviceVersion),
attribute.String("env", rc.environment),
}
for k, v := range rc.attributes {
kvs = append(kvs, attribute.String(k, v))
}
r, err := resource.Merge(
resource.Default(),
resource.NewWithAttributes(semconv.SchemaURL, kvs...),
)
if err != nil {
panic(err)
}
return r
}