builtin.go (3478B)
1 // Copyright The OpenTelemetry Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package resource // import "go.opentelemetry.io/otel/sdk/resource" 16 17 import ( 18 "context" 19 "fmt" 20 "os" 21 "path/filepath" 22 23 "go.opentelemetry.io/otel/attribute" 24 "go.opentelemetry.io/otel/sdk" 25 semconv "go.opentelemetry.io/otel/semconv/v1.17.0" 26 ) 27 28 type ( 29 // telemetrySDK is a Detector that provides information about 30 // the OpenTelemetry SDK used. This Detector is included as a 31 // builtin. If these resource attributes are not wanted, use 32 // the WithTelemetrySDK(nil) or WithoutBuiltin() options to 33 // explicitly disable them. 34 telemetrySDK struct{} 35 36 // host is a Detector that provides information about the host 37 // being run on. This Detector is included as a builtin. If 38 // these resource attributes are not wanted, use the 39 // WithHost(nil) or WithoutBuiltin() options to explicitly 40 // disable them. 41 host struct{} 42 43 stringDetector struct { 44 schemaURL string 45 K attribute.Key 46 F func() (string, error) 47 } 48 49 defaultServiceNameDetector struct{} 50 ) 51 52 var ( 53 _ Detector = telemetrySDK{} 54 _ Detector = host{} 55 _ Detector = stringDetector{} 56 _ Detector = defaultServiceNameDetector{} 57 ) 58 59 // Detect returns a *Resource that describes the OpenTelemetry SDK used. 60 func (telemetrySDK) Detect(context.Context) (*Resource, error) { 61 return NewWithAttributes( 62 semconv.SchemaURL, 63 semconv.TelemetrySDKName("opentelemetry"), 64 semconv.TelemetrySDKLanguageGo, 65 semconv.TelemetrySDKVersion(sdk.Version()), 66 ), nil 67 } 68 69 // Detect returns a *Resource that describes the host being run on. 70 func (host) Detect(ctx context.Context) (*Resource, error) { 71 return StringDetector(semconv.SchemaURL, semconv.HostNameKey, os.Hostname).Detect(ctx) 72 } 73 74 // StringDetector returns a Detector that will produce a *Resource 75 // containing the string as a value corresponding to k. The resulting Resource 76 // will have the specified schemaURL. 77 func StringDetector(schemaURL string, k attribute.Key, f func() (string, error)) Detector { 78 return stringDetector{schemaURL: schemaURL, K: k, F: f} 79 } 80 81 // Detect returns a *Resource that describes the string as a value 82 // corresponding to attribute.Key as well as the specific schemaURL. 83 func (sd stringDetector) Detect(ctx context.Context) (*Resource, error) { 84 value, err := sd.F() 85 if err != nil { 86 return nil, fmt.Errorf("%s: %w", string(sd.K), err) 87 } 88 a := sd.K.String(value) 89 if !a.Valid() { 90 return nil, fmt.Errorf("invalid attribute: %q -> %q", a.Key, a.Value.Emit()) 91 } 92 return NewWithAttributes(sd.schemaURL, sd.K.String(value)), nil 93 } 94 95 // Detect implements Detector. 96 func (defaultServiceNameDetector) Detect(ctx context.Context) (*Resource, error) { 97 return StringDetector( 98 semconv.SchemaURL, 99 semconv.ServiceNameKey, 100 func() (string, error) { 101 executable, err := os.Executable() 102 if err != nil { 103 return "unknown_service:go", nil 104 } 105 return "unknown_service:" + filepath.Base(executable), nil 106 }, 107 ).Detect(ctx) 108 }