github.com/sacloud/libsacloud/v2@v2.32.3/examples/otel/main.go (about) 1 // Copyright 2016-2022 The Libsacloud 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 main 16 17 import ( 18 "context" 19 "log" 20 "net/http" 21 "os" 22 "time" 23 24 "github.com/sacloud/libsacloud/v2" 25 "github.com/sacloud/libsacloud/v2/helper/api" 26 "github.com/sacloud/libsacloud/v2/sacloud" 27 "github.com/sacloud/libsacloud/v2/sacloud/ostype" 28 "github.com/sacloud/libsacloud/v2/sacloud/types" 29 "go.opentelemetry.io/otel" 30 "go.opentelemetry.io/otel/attribute" 31 "go.opentelemetry.io/otel/exporters/jaeger" 32 "go.opentelemetry.io/otel/sdk/resource" 33 tracesdk "go.opentelemetry.io/otel/sdk/trace" 34 semconv "go.opentelemetry.io/otel/semconv/v1.7.0" 35 ) 36 37 // ref: https://github.com/open-telemetry/opentelemetry-go/blob/v1.2.0/example/jaeger/main.go 38 39 // Example ローカルのJaegerを利用する例 40 func main() { 41 ctx := context.Background() 42 43 tp, err := tracerProvider("http://localhost:14268/api/traces") 44 if err != nil { 45 log.Fatal(err) 46 } 47 48 otel.SetTracerProvider(tp) 49 50 ctx, cancel := context.WithCancel(context.Background()) 51 defer cancel() 52 53 // Cleanly shutdown and flush telemetry when the application exits. 54 defer func(ctx context.Context) { 55 // Do not make the application hang when it is shutdown. 56 ctx, cancel = context.WithTimeout(ctx, time.Second*5) 57 defer cancel() 58 if err := tp.Shutdown(ctx); err != nil { 59 log.Fatal(err) 60 } 61 }(ctx) 62 63 // サンプルAPIリクエスト 64 op(ctx) 65 66 // Jaeger UI( http://localhost:16686/search など)を開くとトレースが確認できるはず 67 } 68 69 func tracerProvider(url string) (*tracesdk.TracerProvider, error) { 70 // Create the Jaeger exporter 71 exp, err := jaeger.New(jaeger.WithCollectorEndpoint(jaeger.WithEndpoint(url))) 72 if err != nil { 73 return nil, err 74 } 75 tp := tracesdk.NewTracerProvider( 76 // Always be sure to batch in production. 77 tracesdk.WithBatcher(exp), 78 // Record information about this application in an Resource. 79 tracesdk.WithResource(resource.NewWithAttributes( 80 semconv.SchemaURL, 81 semconv.ServiceNameKey.String("libsacloud"), 82 attribute.String("version", libsacloud.Version), 83 )), 84 ) 85 return tp, nil 86 } 87 88 func op(ctx context.Context) { 89 caller := api.NewCaller(&api.CallerOptions{ 90 AccessToken: os.Getenv("SAKURACLOUD_ACCESS_TOKEN"), 91 AccessTokenSecret: os.Getenv("SAKURACLOUD_ACCESS_TOKEN_SECRET"), 92 HTTPClient: &http.Client{}, 93 OpenTelemetry: true, // enable tracing 94 }) 95 archiveOp := sacloud.NewArchiveOp(caller) 96 97 // normal operation 98 archiveOp.Find(ctx, "is1a", &sacloud.FindCondition{ // nolint 99 Count: 1, 100 From: 0, 101 Filter: ostype.ArchiveCriteria[ostype.Ubuntu], 102 }) 103 104 // invalid operation(not foundエラーになるはず) 105 archiveOp.Read(ctx, "is1a", types.ID(1)) // nolint 106 }