// Copyright 2018 Istio 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.

syntax = "proto3";

// $title: SolarWinds
// $description: Adapter to deliver logs and metrics to Papertrail and AppOptics backends.
// $location: https://istio.io/docs/reference/config/policy-and-telemetry/adapters/solarwinds.html
// $supported_templates: metric,logentry
// $aliases:
// $  - /docs/reference/config/adapters/solarwinds.html

// The `solarwinds` adapter enables Istio to deliver log and metric data to the
// [Papertrail](https://www.papertrailapp.com) logging backend and the
// [AppOptics](https://www.appoptics.com) monitoring backend.
//
// This adapter supports the [metric template](https://istio.io/docs/reference/config/policy-and-telemetry/templates/metric/)
// and the [logentry template](https://istio.io/docs/reference/config/policy-and-telemetry/templates/metric/).
package adapter.solarwinds.config;

import "gogoproto/gogo.proto";
import "google/protobuf/duration.proto";

option go_package = "config";
option (gogoproto.goproto_getters_all) = false;
option (gogoproto.equal_all) = false;
option (gogoproto.gostring_all) = false;

// Configuration format for the `solarwinds` adapter.
//
// Example config usage:
// ```yaml
// apiVersion: "config.istio.io/v1alpha2"
// kind: handler
// metadata:
//   name: solarwinds
//   namespace: istio-system
// spec:
//   compiledAdapter: solarwinds
//   params:
//     appoptics_access_token: <APPOPTICS SAMPLE TOKEN>
//     papertrail_url: <PAPERTRAIL URL>
//     papertrail_local_retention_duration: <RETENTION PERIOD FOR LOGS LOCALLY, Optional>
//     metrics:
//       requestcount.metric.istio-system:
//         label_names:
//         - source_service
//         - source_version
//         - destination_service
//         - destination_version
//         - response_code
//       requestduration.metric.istio-system:
//         label_names:
//         - source_service
//         - source_version
//         - destination_service
//         - destination_version
//         - response_code
//       requestsize.metric.istio-system:
//         label_names:
//         - source_service
//         - source_version
//         - destination_service
//         - destination_version
//         - response_code
//       responsesize.metric.istio-system:
//         label_names:
//         - source_service
//         - source_version
//         - destination_service
//         - destination_version
//         - response_code
//       tcpbytesent.metric.istio-system:
//         label_names:
//         - source_service
//         - source_version
//         - destination_service
//         - destination_version
//       tcpbytereceived.metric.istio-system:
//         label_names:
//         - source_service
//         - source_version
//         - destination_service
//         - destination_version
//     logs:
//       solarwindslogentry.logentry.istio-system:
//         payloadTemplate: '{{or (.originIp) "-"}} - {{or (.sourceUser) "-"}} [{{or (.timestamp.Format "2006-01-02T15:04:05Z07:00") "-"}}] "{{or (.method) "-"}} {{or (.url) "-"}} {{or (.protocol) "-"}}" {{or (.responseCode) "-"}} {{or (.responseSize) "-"}}'
// ```
message Params {
    // AppOptics Access Token needed to send metrics to AppOptics. If no access token is given then metrics
    // will NOT be shipped to AppOptics
    string appoptics_access_token = 1;

    // Optional. Max batch size of metrics to be sent to AppOptics.
    // AppOptics does not allow batch size greater than 1000.
    // If this is unspecified or given a value 0 explicitly, a default batch size of 1000 will be used.
    int32 appoptics_batch_size = 2;

    // Papertrail url to ship logs to. If no papertrail url is given then the logs will NOT be shipped but rather
    // dropped.
    string papertrail_url = 3;

    // This is the duration for which logs will be persisted locally until it is shipped to papertrail in the event
    // of a network failure. Default value is 1 hour.
    google.protobuf.Duration papertrail_local_retention_duration = 4 [(gogoproto.nullable)=true, (gogoproto.stdduration) = true];

    // Describes how to represent an Istio metric in Solarwinds AppOptics
    message MetricInfo {
        // The names of labels to use: these need to match the dimensions of the Istio metric.
        repeated string label_names = 1;
    }

    // A map of Istio metric name to solarwinds metric info.
    map<string, MetricInfo> metrics = 5;

    // Describes how to represent an Istio log entry in Solarwinds AppOptics
    message LogInfo {
        // Optional. A golang text/template template (more details about golang text/template's templating can be
        // found here: https://golang.org/pkg/text/template/) that will be executed to construct the payload for
        // this log entry.
        // An example template that could be used:
        // {{or (.originIp) "-"}} - {{or (.sourceUser) "-"}} [{{or (.timestamp.Format "2006-01-02T15:04:05Z07:00") "-"}}] "{{or (.method) "-"}} {{or (.url) "-"}} {{or (.protocol) "-"}}" {{or (.responseCode) "-"}} {{or (.responseSize) "-"}}
        // A sample log that will be created after parsing the template with appropriate variables will look like this:
        // Jan 23 21:53:02 istio-mixer-57d88dc4b4-rbgmc istio: 10.32.0.15 - kubernetes://istio-ingress-78545c5bc9-wbr6g.istio-system [2018-01-24T02:53:02Z] "GET /productpage http" 200 5599
        // It will be given the full set of variables for the log to use to construct its result.
        // If it is not provided, a default template in place will be used.
        string payload_template = 1;
    }

    // A map of Istio logentry name to solarwinds log info.
    map<string, LogInfo> logs = 6;
}
