commit 7f606a93cdb6fdb3cd750f2465f15945f843b325 Author: William Perron Date: Sat Mar 2 15:28:55 2024 -0500 initial commit diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..35a7bec --- /dev/null +++ b/LICENSE @@ -0,0 +1,20 @@ +MIT License + +Copyright (c) 2024 William Perron + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..446c261 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# Fly.io Detector For OpenTelemetry + +OpenTelemetry Detector for [fly.io](https://fly.io) in Go. diff --git a/detector.go b/detector.go new file mode 100644 index 0000000..e0739a4 --- /dev/null +++ b/detector.go @@ -0,0 +1,54 @@ +package flydetector + +import ( + "context" + "os" + + "go.opentelemetry.io/otel/attribute" + "go.opentelemetry.io/otel/sdk/resource" + semconv "go.opentelemetry.io/otel/semconv/v1.24.0" +) + +var _ resource.Detector = &FlyDetector{} + +type FlyDetector struct{} + +func NewDetector() resource.Detector { + return &FlyDetector{} +} + +func (d *FlyDetector) Detect(_ context.Context) (*resource.Resource, error) { + a := []attribute.KeyValue{semconv.CloudProviderKey.String("fly")} + + if an, ok := os.LookupEnv("FLY_APP_NAME"); ok { + a = append(a, attribute.String("fly.app_name", an)) + } else { + return resource.Empty(), nil + } + + if mid, ok := os.LookupEnv("FLY_MACHINE_ID"); ok { + a = append(a, attribute.String("fly.machine_id", mid)) + } + + if aid, ok := os.LookupEnv("FLY_ALLOC_ID"); ok { + a = append(a, attribute.String("fly.alloc_id", aid)) + } + + if pg, ok := os.LookupEnv("FLY_PROCESS_GROUP"); ok { + a = append(a, attribute.String("fly.process_group", pg)) + } + + if mv, ok := os.LookupEnv("FLY_MACHINE_VERSION"); ok { + a = append(a, attribute.String("fly.machine_version", mv), semconv.CloudResourceID(mv)) + } + + if reg, ok := os.LookupEnv("FLY_REGION"); ok { + a = append(a, attribute.String("fly.region", reg), semconv.CloudRegionKey.String(reg)) + } + + if ir, ok := os.LookupEnv("FLY_IMAGE_REF"); ok { + a = append(a, attribute.String("fly.image_ref", ir)) + } + + return resource.NewWithAttributes(semconv.SchemaURL, a...), nil +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..da05771 --- /dev/null +++ b/go.mod @@ -0,0 +1,14 @@ +module go.wperron.io/flydetector + +go 1.21.1 + +require go.opentelemetry.io/otel/sdk v1.24.0 + +require ( + github.com/go-logr/logr v1.4.1 // indirect + github.com/go-logr/stdr v1.2.2 // indirect + go.opentelemetry.io/otel v1.24.0 // indirect + go.opentelemetry.io/otel/metric v1.24.0 // indirect + go.opentelemetry.io/otel/trace v1.24.0 // indirect + golang.org/x/sys v0.17.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..dc1df5c --- /dev/null +++ b/go.sum @@ -0,0 +1,15 @@ +github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= +github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ= +github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= +github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= +github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= +go.opentelemetry.io/otel v1.24.0 h1:0LAOdjNmQeSTzGBzduGe/rU4tZhMwL5rWgtp9Ku5Jfo= +go.opentelemetry.io/otel v1.24.0/go.mod h1:W7b9Ozg4nkF5tWI5zsXkaKKDjdVjpD4oAt9Qi/MArHo= +go.opentelemetry.io/otel/metric v1.24.0 h1:6EhoGWWK28x1fbpA4tYTOWBkPefTDQnb8WSGXlc88kI= +go.opentelemetry.io/otel/metric v1.24.0/go.mod h1:VYhLe1rFfxuTXLgj4CBiyz+9WYBA8pNGJgDcSFRKBco= +go.opentelemetry.io/otel/sdk v1.24.0 h1:YMPPDNymmQN3ZgczicBY3B6sf9n62Dlj9pWD3ucgoDw= +go.opentelemetry.io/otel/sdk v1.24.0/go.mod h1:KVrIYw6tEubO9E96HQpcmpTKDVn9gdv35HoYiQWGDFg= +go.opentelemetry.io/otel/trace v1.24.0 h1:CsKnnL4dUAr/0llH9FKuc698G04IrpWV0MQA/Y1YELI= +go.opentelemetry.io/otel/trace v1.24.0/go.mod h1:HPc3Xr/cOApsBI154IU0OI0HJexz+aw5uPdbs3UCjNU= +golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y= +golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=