You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by GitBox <gi...@apache.org> on 2022/05/25 23:50:40 UTC

[GitHub] [beam] youngoli commented on a diff in pull request #17748: [BEAM-14513] Add read transform and initial healthcare client

youngoli commented on code in PR #17748:
URL: https://github.com/apache/beam/pull/17748#discussion_r882199263


##########
sdks/go/pkg/beam/io/healthcare/fhirio/read.go:
##########
@@ -0,0 +1,87 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You 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 fhirio

Review Comment:
   Please add a package comment. If the IO is still unfinished/experimental you should also specify that. This package and any exported functionality within it will be visible to users on the next beam release, so keeping it documented is important, even while it's a work-in-progress.



##########
sdks/go/pkg/beam/io/healthcare/fhirio/read.go:
##########
@@ -0,0 +1,87 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You 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 fhirio
+
+import (
+	"context"

Review Comment:
   style nit: In the Go SDK we try to put standard library packages separate from third party ones. [Example](https://github.com/apache/beam/blob/master/sdks/go/examples/wordcount/wordcount.go#L69).



##########
sdks/go/pkg/beam/io/healthcare/fhirio/read.go:
##########
@@ -0,0 +1,87 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You 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 fhirio
+
+import (
+	"context"
+	"github.com/apache/beam/sdks/v2/go/pkg/beam"
+	"github.com/apache/beam/sdks/v2/go/pkg/beam/core/metrics"
+	"github.com/apache/beam/sdks/v2/go/pkg/beam/internal/errors"
+	"io"
+	"reflect"
+	"time"
+)
+
+func init() {
+	beam.RegisterType(reflect.TypeOf((*readResourceFn)(nil)))
+}
+
+type readResourceFn struct {
+	client                fhirStoreClient
+	readResourceErrors    *metrics.Counter
+	readResourceSuccess   *metrics.Counter
+	readResourceLatencyMs *metrics.Distribution
+}
+
+func (fn readResourceFn) String() string {
+	return "readResourceFn"
+}
+
+func (fn *readResourceFn) Setup() {
+	if fn.client == nil {
+		fn.client = newFhirStoreClient()
+	}
+	fn.readResourceErrors = metrics.NewCounter(fn.String(), baseMetricPrefix+"read_resource_error_count")
+	fn.readResourceSuccess = metrics.NewCounter(fn.String(), baseMetricPrefix+"read_resource_success_count")
+	fn.readResourceLatencyMs = metrics.NewDistribution(fn.String(), baseMetricPrefix+"read_resource_latency_ms")
+}
+
+func (fn *readResourceFn) ProcessElement(ctx context.Context, resourceId string, emitResource func(string), emitDeadLetter func(string)) {
+	timeBeforeReadRequest := time.Now()
+	response, err := fn.client.readResource(resourceId)
+	fn.readResourceLatencyMs.Update(ctx, time.Now().Sub(timeBeforeReadRequest).Milliseconds())
+
+	if err != nil {
+		fn.readResourceErrors.Inc(ctx, 1)
+		emitDeadLetter(errors.Wrapf(err, "Failed to fetch resource [%s].", resourceId).Error())
+		return
+	}
+
+	if response.StatusCode != 200 {
+		fn.readResourceErrors.Inc(ctx, 1)
+		emitDeadLetter(errors.Errorf("Fetched resource [%s] returned bad status [%d].", resourceId, response.StatusCode).Error())
+		return
+	}
+
+	bytes, err := io.ReadAll(response.Body)
+	if err != nil {
+		fn.readResourceErrors.Inc(ctx, 1)
+		emitDeadLetter(errors.Wrapf(err, "Error while reading response body of resource [%s]", resourceId).Error())
+		return
+	}
+
+	fn.readResourceSuccess.Inc(ctx, 1)
+	emitResource(string(bytes))
+}
+
+func Read(s beam.Scope, resourceIds beam.PCollection) (beam.PCollection, beam.PCollection) {

Review Comment:
   Please add a comment documenting Read.



##########
sdks/go/pkg/beam/io/healthcare/fhirio/fakes.go:
##########
@@ -0,0 +1,40 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You 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 fhirio
+
+import (
+	"net/http"
+)
+
+type fakeFhirStoreClient struct {

Review Comment:
   Since this is only used for tests, I would put it in some file ending in `_test.go` so it doesn't get compiled into non-test binaries. Maybe in read_test.go for now, but even renaming this to fakes_test.go works.



##########
sdks/go/pkg/beam/io/healthcare/fhirio/read_test.go:
##########
@@ -0,0 +1,83 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You 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 fhirio
+
+import (
+	"errors"
+	"github.com/apache/beam/sdks/v2/go/pkg/beam/testing/passert"
+	"github.com/apache/beam/sdks/v2/go/pkg/beam/testing/ptest"
+	"net/http"
+	"strings"
+	"testing"
+)
+
+func Test_readFailures(t *testing.T) {

Review Comment:
   Style nit: Test names usually use underscores to separate variations on testing the same "unit". In this situation, I would just go with `TestRead` because that's the only function being tested and there aren't multiple tests of Read that need to be distinguished.



##########
sdks/go/pkg/beam/io/healthcare/gcpcommon.go:
##########
@@ -0,0 +1,34 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You 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 healthcare
+
+import (
+	"context"
+	"github.com/apache/beam/sdks/v2/go/pkg/beam/core"
+	healthcarex "google.golang.org/api/healthcare/v1"
+	"google.golang.org/api/option"
+)
+
+const userAgent = "apache-beam-io-google-cloud-platform-healthcare/" + core.SdkVersion
+
+func NewGcpHealthcareService() *healthcarex.Service {

Review Comment:
   Question: Is this function intended to be user-facing? Or is it only exported for developer convenience? If it's the latter, we should hide it from users by putting it in an `internal/`subdirectory. [More info](https://dave.cheney.net/2019/10/06/use-internal-packages-to-reduce-your-public-api-surface)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org