New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added google cloud function resource detector #1584
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
9eecabd
adding cloud function detector
patil-kshitij bc17e7e
Merge branch 'open-telemetry:main' into main
patil-kshitij c64cd2a
linting changes
patil-kshitij 0edacc4
Merge branch 'main' of https://github.com/patil-kshitij/opentelemetry…
patil-kshitij a2de50a
changed a function name
patil-kshitij 289405d
using cloudrun implementation for getting projectID and region
patil-kshitij 29088d3
using provided context
patil-kshitij 99ee757
adding unit test cases
patil-kshitij 31995f5
ran make pre-commit
patil-kshitij e36628b
incorporated review comment
patil-kshitij a24189f
Merge branch 'main' into main
Aneurysm9 02b313a
Merge branch 'open-telemetry:main' into main
patil-kshitij 42d53c2
incorporating review comments.
patil-kshitij 25d54bc
Merge branch 'main' of https://github.com/patil-kshitij/opentelemetry…
patil-kshitij ef027ae
Merge branch 'open-telemetry:main' into main
patil-kshitij a306e77
added changelog
patil-kshitij bf4a657
Making cloudFunction struct private
patil-kshitij a329bfd
incorporating review comments
patil-kshitij cc29e15
Merge branch 'open-telemetry:main' into main
patil-kshitij 5fd26ff
Merge branch 'main' into main
MrAlias File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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,71 @@ | ||||||
// Copyright The OpenTelemetry Authors | ||||||
// | ||||||
// 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 | ||||||
// | ||||||
// http://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 gcp | ||||||
|
||||||
import ( | ||||||
"context" | ||||||
"os" | ||||||
|
||||||
"go.opentelemetry.io/otel/attribute" | ||||||
"go.opentelemetry.io/otel/sdk/resource" | ||||||
semconv "go.opentelemetry.io/otel/semconv/v1.7.0" | ||||||
) | ||||||
|
||||||
const ( | ||||||
gcpFunctionNameKey = "K_SERVICE" | ||||||
) | ||||||
|
||||||
// NewCloudFunction will return a GCP Cloud Function resource detector. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
func NewCloudFunction() resource.Detector { | ||||||
return &cloudFunction{ | ||||||
cloudRun: NewCloudRun(), | ||||||
} | ||||||
} | ||||||
|
||||||
// cloudFunction collects resource information of GCP Cloud Function | ||||||
type cloudFunction struct { | ||||||
cloudRun *CloudRun | ||||||
} | ||||||
|
||||||
// Detect detects associated resources when running in GCP Cloud Function. | ||||||
func (f *cloudFunction) Detect(ctx context.Context) (*resource.Resource, error) { | ||||||
functionName, ok := f.googleCloudFunctionName() | ||||||
if !ok { | ||||||
return nil, nil | ||||||
} | ||||||
|
||||||
projectID, err := f.cloudRun.mc.ProjectID() | ||||||
if err != nil { | ||||||
return nil, err | ||||||
} | ||||||
region, err := f.cloudRun.cloudRegion(ctx) | ||||||
if err != nil { | ||||||
return nil, err | ||||||
} | ||||||
|
||||||
attributes := []attribute.KeyValue{ | ||||||
semconv.CloudProviderGCP, | ||||||
semconv.CloudPlatformGCPCloudFunctions, | ||||||
semconv.FaaSNameKey.String(functionName), | ||||||
semconv.CloudAccountIDKey.String(projectID), | ||||||
semconv.CloudRegionKey.String(region), | ||||||
} | ||||||
return resource.NewWithAttributes(semconv.SchemaURL, attributes...), nil | ||||||
|
||||||
} | ||||||
|
||||||
func (f *cloudFunction) googleCloudFunctionName() (string, bool) { | ||||||
return os.LookupEnv(gcpFunctionNameKey) | ||||||
} |
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,175 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// 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 | ||
// | ||
// http://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 gcp | ||
MrAlias marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import ( | ||
"context" | ||
"errors" | ||
"os" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
|
||
"go.opentelemetry.io/otel/attribute" | ||
"go.opentelemetry.io/otel/sdk/resource" | ||
semconv "go.opentelemetry.io/otel/semconv/v1.4.0" | ||
) | ||
|
||
var ( | ||
errTest = errors.New("testError") | ||
) | ||
|
||
const ( | ||
projectIDValue = "some-projectID" | ||
regionValue = "some-region" | ||
functionName = "sample-function" | ||
) | ||
|
||
type metaDataClientImpl struct { | ||
projectID func() (string, error) | ||
get func(string) (string, error) | ||
instanceID func() (string, error) | ||
} | ||
|
||
func (mock *metaDataClientImpl) ProjectID() (string, error) { | ||
if mock.projectID != nil { | ||
return mock.projectID() | ||
} | ||
return "", nil | ||
} | ||
|
||
func (mock *metaDataClientImpl) Get(key string) (string, error) { | ||
if mock.get != nil { | ||
return mock.get(key) | ||
} | ||
return "", nil | ||
} | ||
|
||
func (mock *metaDataClientImpl) InstanceID() (string, error) { | ||
if mock.instanceID != nil { | ||
return mock.instanceID() | ||
} | ||
return "", nil | ||
} | ||
|
||
type want struct { | ||
res *resource.Resource | ||
err error | ||
} | ||
|
||
func TestCloudFunctionDetect(t *testing.T) { | ||
oldValue, ok := os.LookupEnv(gcpFunctionNameKey) | ||
if !ok { | ||
err := os.Setenv(gcpFunctionNameKey, functionName) | ||
if err != nil { | ||
t.Error("unable to set environment variable ", err) | ||
} | ||
} | ||
defer func() { | ||
if !ok { | ||
os.Unsetenv(gcpFunctionNameKey) | ||
} else { | ||
os.Setenv(gcpFunctionNameKey, oldValue) | ||
} | ||
}() | ||
Aneurysm9 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
tests := []struct { | ||
name string | ||
cr *CloudRun | ||
expected want | ||
}{ | ||
{ | ||
name: "error in reading ProjectID", | ||
cr: &CloudRun{ | ||
mc: &metaDataClientImpl{ | ||
projectID: func() (string, error) { | ||
return "", errTest | ||
}, | ||
}, | ||
}, | ||
expected: want{ | ||
res: nil, | ||
err: errTest, | ||
}, | ||
}, | ||
{ | ||
name: "error in reading region", | ||
cr: &CloudRun{ | ||
mc: &metaDataClientImpl{ | ||
get: func(key string) (string, error) { | ||
return "", errTest | ||
}, | ||
}, | ||
}, | ||
expected: want{ | ||
res: nil, | ||
err: errTest, | ||
}, | ||
}, | ||
{ | ||
name: "success", | ||
cr: &CloudRun{ | ||
mc: &metaDataClientImpl{ | ||
projectID: func() (string, error) { | ||
return projectIDValue, nil | ||
}, | ||
get: func(key string) (string, error) { | ||
return regionValue, nil | ||
}, | ||
}, | ||
}, | ||
expected: want{ | ||
res: resource.NewSchemaless([]attribute.KeyValue{ | ||
semconv.CloudProviderGCP, | ||
semconv.CloudPlatformGCPCloudFunctions, | ||
semconv.FaaSNameKey.String(functionName), | ||
semconv.CloudAccountIDKey.String(projectIDValue), | ||
semconv.CloudRegionKey.String(regionValue), | ||
}...), | ||
err: nil, | ||
}, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
detector := cloudFunction{ | ||
cloudRun: test.cr, | ||
} | ||
res, err := detector.Detect(context.Background()) | ||
if err != test.expected.err { | ||
t.Fatalf("got unexpected failure: %v", err) | ||
} else if diff := cmp.Diff(test.expected.res, res); diff != "" { | ||
t.Errorf("detected resource differ from expected (-want, +got)\n%s", diff) | ||
} | ||
} | ||
} | ||
|
||
func TestNotOnCloudFunction(t *testing.T) { | ||
oldValue, ok := os.LookupEnv(gcpFunctionNameKey) | ||
if ok { | ||
os.Unsetenv(gcpFunctionNameKey) | ||
} | ||
defer func() { | ||
if ok { | ||
os.Setenv(gcpFunctionNameKey, oldValue) | ||
} | ||
}() | ||
detector := NewCloudFunction() | ||
res, err := detector.Detect(context.Background()) | ||
if err != nil { | ||
t.Errorf("expected cloud function detector to return error as nil, but returned %v", err) | ||
} else if res != nil { | ||
t.Errorf("expected cloud function detector to return resource as nil, but returned %v", res) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.