gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

os.go (3022B)


      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 	"strings"
     20 
     21 	"go.opentelemetry.io/otel/attribute"
     22 	semconv "go.opentelemetry.io/otel/semconv/v1.17.0"
     23 )
     24 
     25 type osDescriptionProvider func() (string, error)
     26 
     27 var defaultOSDescriptionProvider osDescriptionProvider = platformOSDescription
     28 
     29 var osDescription = defaultOSDescriptionProvider
     30 
     31 func setDefaultOSDescriptionProvider() {
     32 	setOSDescriptionProvider(defaultOSDescriptionProvider)
     33 }
     34 
     35 func setOSDescriptionProvider(osDescriptionProvider osDescriptionProvider) {
     36 	osDescription = osDescriptionProvider
     37 }
     38 
     39 type osTypeDetector struct{}
     40 type osDescriptionDetector struct{}
     41 
     42 // Detect returns a *Resource that describes the operating system type the
     43 // service is running on.
     44 func (osTypeDetector) Detect(ctx context.Context) (*Resource, error) {
     45 	osType := runtimeOS()
     46 
     47 	osTypeAttribute := mapRuntimeOSToSemconvOSType(osType)
     48 
     49 	return NewWithAttributes(
     50 		semconv.SchemaURL,
     51 		osTypeAttribute,
     52 	), nil
     53 }
     54 
     55 // Detect returns a *Resource that describes the operating system the
     56 // service is running on.
     57 func (osDescriptionDetector) Detect(ctx context.Context) (*Resource, error) {
     58 	description, err := osDescription()
     59 
     60 	if err != nil {
     61 		return nil, err
     62 	}
     63 
     64 	return NewWithAttributes(
     65 		semconv.SchemaURL,
     66 		semconv.OSDescription(description),
     67 	), nil
     68 }
     69 
     70 // mapRuntimeOSToSemconvOSType translates the OS name as provided by the Go runtime
     71 // into an OS type attribute with the corresponding value defined by the semantic
     72 // conventions. In case the provided OS name isn't mapped, it's transformed to lowercase
     73 // and used as the value for the returned OS type attribute.
     74 func mapRuntimeOSToSemconvOSType(osType string) attribute.KeyValue {
     75 	// the elements in this map are the intersection between
     76 	// available GOOS values and defined semconv OS types
     77 	osTypeAttributeMap := map[string]attribute.KeyValue{
     78 		"darwin":    semconv.OSTypeDarwin,
     79 		"dragonfly": semconv.OSTypeDragonflyBSD,
     80 		"freebsd":   semconv.OSTypeFreeBSD,
     81 		"linux":     semconv.OSTypeLinux,
     82 		"netbsd":    semconv.OSTypeNetBSD,
     83 		"openbsd":   semconv.OSTypeOpenBSD,
     84 		"solaris":   semconv.OSTypeSolaris,
     85 		"windows":   semconv.OSTypeWindows,
     86 	}
     87 
     88 	var osTypeAttribute attribute.KeyValue
     89 
     90 	if attr, ok := osTypeAttributeMap[osType]; ok {
     91 		osTypeAttribute = attr
     92 	} else {
     93 		osTypeAttribute = semconv.OSTypeKey.String(strings.ToLower(osType))
     94 	}
     95 
     96 	return osTypeAttribute
     97 }