Skip to content

Commit

Permalink
Add cloud functions e2e test (#552)
Browse files Browse the repository at this point in the history
* detector checks cloud functions before cloud run

* split scenarios from endtoendserver

* add cloud functions e2e test
  • Loading branch information
dashpole committed Jan 13, 2023
1 parent 03a1d7b commit cc4a77e
Show file tree
Hide file tree
Showing 13 changed files with 991 additions and 126 deletions.
43 changes: 43 additions & 0 deletions cloudbuild-e2e-cloud-functions-gen2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Copyright 2023 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
#
# 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.

steps:
# Vendor dependencies, and zip the code.
- name: golang:1.18
id: zip-code
entrypoint: /bin/bash
args:
- '-c'
- |
apt-get update && \
apt-get install zip --assume-yes && \
cd e2e-test-server/cloud_functions/ && \
go mod vendor && \
zip -r function-source *
# Run the test
- name: $_TEST_RUNNER_IMAGE
id: run-tests-cloudfunction
dir: /
env: ["PROJECT_ID=$PROJECT_ID"]
args:
- cloud-functions-gen2
- --runtime=go118
- --functionsource=/workspace/e2e-test-server/cloud_functions/function-source.zip
- --entrypoint=HandleCloudFunction

logsBucket: gs://opentelemetry-ops-e2e-cloud-build-logs
substitutions:
_TEST_RUNNER_IMAGE: gcr.io/${PROJECT_ID}/opentelemetry-operations-e2e-testing:0.16.0
_TEST_SERVER_IMAGE: gcr.io/${PROJECT_ID}/opentelemetry-operations-go-e2e-test-server:${SHORT_SHA}
4 changes: 2 additions & 2 deletions detectors/gcp/detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ func (d *Detector) CloudPlatform() Platform {
switch {
case d.onGKE():
return GKE
case d.onCloudRun():
return CloudRun
case d.onCloudFunctions():
return CloudFunctions
case d.onCloudRun():
return CloudRun
case d.onAppEngineStandard():
return AppEngineStandard
case d.onAppEngine():
Expand Down
1 change: 1 addition & 0 deletions e2e-test-server/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ RUN go mod download

# Copy the go source
COPY e2e-test-server/app.go ./
COPY e2e-test-server/scenarios/ scenarios/
COPY e2e-test-server/endtoendserver/ endtoendserver/

# Build
Expand Down
65 changes: 65 additions & 0 deletions e2e-test-server/cloud_functions/cloud_functions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright 2023 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
//
// 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 cloudfunctions

import (
"context"
"encoding/json"
"fmt"
"log"
"os"

"cloud.google.com/go/pubsub"
// funcframework is required to make this build on cloud functions. See
// https://github.com/GoogleCloudPlatform/functions-framework-go/issues/78
_ "github.com/GoogleCloudPlatform/functions-framework-go/funcframework"
"github.com/GoogleCloudPlatform/functions-framework-go/functions"
"github.com/cloudevents/sdk-go/v2/event"

"github.com/GoogleCloudPlatform/opentelemetry-operations-go/e2e-test-server/scenarios"
)

var projectID string

func init() {
// Register a CloudEvent function with the Functions Framework
functions.CloudEvent("HandleCloudFunction", handleCloudFunction)
projectID = os.Getenv("PROJECT_ID")
if projectID == "" {
log.Fatalf("environment variable PROJECT_ID must be set")
}
}

// PubSubMessage is the payload of a Pub/Sub event.
type PubSubMessage struct {
Message struct {
Attributes map[string]string `json:"attributes,omitempty"`
} `json:"message"`
}

// handleCloudFunction accepts and handles a CloudEvent object. It decodes the
// pubsub message contained within the event and handles it.
func handleCloudFunction(ctx context.Context, e event.Event) error {
var m PubSubMessage
if err := json.Unmarshal(e.Data(), &m); err != nil {
return fmt.Errorf("json.Unmarshal: %w", err)
}
pubsubClient, err := pubsub.NewClient(context.Background(), projectID)
if err != nil {
return fmt.Errorf("error creating pubsub client: %v", err)
}
defer pubsubClient.Close()
return scenarios.HandleMessage(ctx, pubsubClient, m.Message.Attributes)
}
61 changes: 61 additions & 0 deletions e2e-test-server/cloud_functions/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
module github.com/GoogleCloudPlatform/opentelemetry-operations-go/e2e-test-server/cloudfunctions

go 1.18

require (
cloud.google.com/go/pubsub v1.28.0
github.com/GoogleCloudPlatform/functions-framework-go v1.6.1
github.com/GoogleCloudPlatform/opentelemetry-operations-go/e2e-test-server v0.0.0-00010101000000-000000000000
github.com/cloudevents/sdk-go/v2 v2.13.0
)

require (
cloud.google.com/go v0.105.0 // indirect
cloud.google.com/go/compute v1.13.0 // indirect
cloud.google.com/go/compute/metadata v0.2.2 // indirect
cloud.google.com/go/functions v1.9.0 // indirect
cloud.google.com/go/iam v0.8.0 // indirect
cloud.google.com/go/trace v1.4.0 // indirect
github.com/GoogleCloudPlatform/opentelemetry-operations-go/detectors/gcp v0.34.2 // indirect
github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/trace v1.10.2 // indirect
github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/resourcemapping v0.34.2 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.2.0 // indirect
github.com/googleapis/gax-go/v2 v2.7.0 // indirect
github.com/json-iterator/go v1.1.10 // indirect
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/contrib/detectors/gcp v1.12.0 // indirect
go.opentelemetry.io/otel v1.11.2 // indirect
go.opentelemetry.io/otel/sdk v1.11.2 // indirect
go.opentelemetry.io/otel/trace v1.11.2 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.8.0 // indirect
go.uber.org/zap v1.10.0 // indirect
golang.org/x/net v0.0.0-20221017152216-f25eb7ecb193 // indirect
golang.org/x/oauth2 v0.0.0-20221014153046-6fdb5e3db783 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.1.0 // indirect
golang.org/x/text v0.4.0 // indirect
google.golang.org/api v0.103.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20230110181048-76db0878b65f // indirect
google.golang.org/grpc v1.51.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
)

replace github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/trace => ../../exporter/trace

replace github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/resourcemapping => ../../internal/resourcemapping

replace github.com/GoogleCloudPlatform/opentelemetry-operations-go/detectors/gcp => ../../detectors/gcp

replace github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/cloudmock => ../../internal/cloudmock

replace github.com/GoogleCloudPlatform/opentelemetry-operations-go/e2e-test-server => ./..
Loading

0 comments on commit cc4a77e

Please sign in to comment.