Skip to content
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

Add an example, link it in the README #219

Merged
merged 4 commits into from
Jul 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/metrics/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
)

func TestMinimumConfiguration(t *testing.T) {
server := test.NewServer(t)
server := test.NewServer()
defer server.Stop()

os.Setenv("LS_SERVICE_NAME", "test-service-name")
Expand Down
2 changes: 1 addition & 1 deletion examples/tracing/trace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
)

func TestMinimumConfiguration(t *testing.T) {
server := test.NewServer(t)
server := test.NewServer()
defer server.Stop()

os.Setenv("LS_SERVICE_NAME", "test-service-name")
Expand Down
2 changes: 1 addition & 1 deletion launcher/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type testSuite struct {
}

func (suite *testSuite) SetupSuite() {
suite.Server = test.NewServer(suite.T())
suite.Server = test.NewServer()
}

func (suite *testSuite) SetupTest() {
Expand Down
6 changes: 6 additions & 0 deletions lightstep/sdk/metric/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ Moreover, Lightstep expects to make several optimizations in this SDK
to further optimize the synchronous instrument fast path and continue
improving memory performance.

### Using the Lightstep Metrics SDK directly

See the example in
[examples/example_test.go](./examples/example_test.go) for an example
using this SDK with minimum configuration.

### Metric instrument "Hints" API

There is a standing feature request in OpenTelemetry for a "Hints" API
Expand Down
20 changes: 20 additions & 0 deletions lightstep/sdk/metric/example/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// 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.

// example demonstrates how to use the Lightstep metrics SDK with a
// minimum of configuration.
package example

// MinimumConfig is tested in example_test.go.
const MinimumConfig = ""
72 changes: 72 additions & 0 deletions lightstep/sdk/metric/example/example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// 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 example

import (
"context"
"fmt"
"time"

// Note the SDK, exporter, and test appratus are from this repository
lightstep "github.com/lightstep/otel-launcher-go/lightstep/sdk/metric"
exporter "github.com/lightstep/otel-launcher-go/lightstep/sdk/metric/exporters/otlp"
otlptest "github.com/lightstep/otel-launcher-go/pipelines/test"

// Note the gRPC/OTLP exporter and protocol are from the community SDK.
otlpmetricgrpc "go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc"
otlpproto "go.opentelemetry.io/proto/otlp/metrics/v1"
)

// ExampleMinimumConfig tests the minimum configuration for a single point.
func ExampleMinimumConfig() {
ctx := context.Background()
server := otlptest.NewServer()

// Configure an exporter.
exp, _ := exporter.New(ctx, otlpmetricgrpc.NewClient(

// In a real scenario, replace the following three lines with, for example:
// WithEndpoint("ingest.lightstep.com:443").
// WithHeaders(map[string]string{"lightstep-access-token": "${YOUR_ACCESS_TOKEN}"}),
otlpmetricgrpc.WithInsecure(),
otlpmetricgrpc.WithEndpoint(fmt.Sprint(otlptest.ServerName, ":", server.InsecureMetricsPort)),
otlpmetricgrpc.WithHeaders(map[string]string{"lightstep-access-token": "${TOKEN}"}),
))

// Configure the SDK.
sdk := lightstep.NewMeterProvider(
lightstep.WithReader(lightstep.NewPeriodicReader(exp, 30*time.Second)),
)

// Configure a Meter and instrument.
meter := sdk.Meter("meter")
counter, _ := meter.SyncInt64().Counter("how-many")
jmacd marked this conversation as resolved.
Show resolved Hide resolved

// Count once and shutdown.
counter.Add(ctx, 1)
sdk.Shutdown(ctx)

oneScope := server.MetricsRequests()[0].ResourceMetrics[0].ScopeMetrics[0]
oneHeader := server.MetricsMDs()[0]

fmt.Println(
oneScope.Scope.Name,
oneScope.Metrics[0].Name,
oneScope.Metrics[0].Data.(*otlpproto.Metric_Sum).Sum.DataPoints[0].Value.(*otlpproto.NumberDataPoint_AsInt).AsInt,
oneHeader["lightstep-access-token"][0],
)

// Output: meter how-many 1 ${TOKEN}
}
37 changes: 37 additions & 0 deletions lightstep/sdk/metric/example/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module github.com/lightstep/otel-launcher-go/lightstep/sdk/metric/example

go 1.18

require (
github.com/lightstep/otel-launcher-go/lightstep/sdk/metric v1.8.0
github.com/lightstep/otel-launcher-go/pipelines v1.8.0
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v0.31.0
go.opentelemetry.io/proto/otlp v0.18.0
)

require (
github.com/cenkalti/backoff/v4 v4.1.3 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0 // indirect
go.opentelemetry.io/otel v1.8.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.8.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlpmetric v0.31.0 // indirect
go.opentelemetry.io/otel/metric v0.31.0 // indirect
go.opentelemetry.io/otel/sdk v1.8.0 // indirect
go.opentelemetry.io/otel/sdk/metric v0.31.0 // indirect
go.opentelemetry.io/otel/trace v1.8.0 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.8.0 // indirect
golang.org/x/net v0.0.0-20220111093109-d55c255bac03 // indirect
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/genproto v0.0.0-20220112215332-a9c7c0acf9f2 // indirect
google.golang.org/grpc v1.46.2 // indirect
google.golang.org/protobuf v1.28.0 // indirect
)

replace github.com/lightstep/otel-launcher-go/lightstep/sdk/metric => ../

replace github.com/lightstep/otel-launcher-go/pipelines => ../../../../pipelines
Loading