-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(logging): revise automatic resource detection for ingested logs (#…
…6022) align resource detection with heuristics in logging libraries for other languages: - use /sys/class/dmi/id/product_name to read a product name of the resource on Linux - change resource detection heuristics in a way that the order of validation is not important - reduce timeout and retrying logic when querying metadata server - demand active metadata server for detecting GCP resources (for GCE, GAE, GKE, CR and CF) - add test to validate resource detection heuristics
- Loading branch information
Showing
3 changed files
with
466 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// Copyright 2022 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package internal | ||
|
||
import ( | ||
"io/ioutil" | ||
"net" | ||
"net/http" | ||
"os" | ||
"strings" | ||
"time" | ||
|
||
"cloud.google.com/go/compute/metadata" | ||
) | ||
|
||
// ResourceAtttributesGetter abstracts environment lookup methods to query for environment variables, metadata attributes and file content. | ||
type ResourceAtttributesGetter interface { | ||
EnvVar(name string) string | ||
Metadata(path string) string | ||
ReadAll(path string) string | ||
} | ||
|
||
var getter ResourceAtttributesGetter = &defaultResourceGetter{ | ||
metaClient: metadata.NewClient(&http.Client{ | ||
Transport: &http.Transport{ | ||
Dial: (&net.Dialer{ | ||
Timeout: 1 * time.Second, | ||
KeepAlive: 10 * time.Second, | ||
}).Dial, | ||
}, | ||
})} | ||
|
||
// ResourceAttributes provides read-only access to the ResourceAtttributesGetter interface implementation. | ||
func ResourceAttributes() ResourceAtttributesGetter { | ||
return getter | ||
} | ||
|
||
type defaultResourceGetter struct { | ||
metaClient *metadata.Client | ||
} | ||
|
||
// EnvVar uses os.LookupEnv() to lookup for environment variable by name. | ||
func (g *defaultResourceGetter) EnvVar(name string) string { | ||
return os.Getenv(name) | ||
} | ||
|
||
// Metadata uses metadata package Client.Get() to lookup for metadata attributes by path. | ||
func (g *defaultResourceGetter) Metadata(path string) string { | ||
val, err := g.metaClient.Get(path) | ||
if err != nil { | ||
return "" | ||
} | ||
return strings.TrimSpace(val) | ||
} | ||
|
||
// ReadAll reads all content of the file as a string. | ||
func (g *defaultResourceGetter) ReadAll(path string) string { | ||
bytes, err := ioutil.ReadFile(path) | ||
if err != nil { | ||
return "" | ||
} | ||
return string(bytes) | ||
} |
Oops, something went wrong.